请以程序员的身份,使用Java语言,写一个AI贪吃蛇
时间: 2023-06-09 09:07:59 浏览: 75
java语言写的贪吃蛇
好的,我可以为您编写一个基于Java语言的AI贪吃蛇。以下是实现该游戏所需的步骤:
1. 创建游戏画面并绘制初始的贪吃蛇和食物。
2. 实现贪吃蛇的移动,这可以通过定义一个Snake类来实现,类中包含蛇的长度、位置和移动方式等属性和方法。
3. 实现贪吃蛇的碰撞检测,当蛇头撞到墙壁或自己的身体时游戏结束。
4. 实现食物的随机生成和与贪吃蛇的碰撞检测。
5. 在界面上显示得分和游戏结束信息。
以下是Java代码示例,希望能够满足您的需求:
```
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AI_Snake extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 300;
private static final int HEIGHT = 300;
private Thread thread;
private boolean running;
private boolean right = true, left = false, up = false, down = false;
private Snake snake;
private List<Point> foods;
private int score;
private boolean gameover;
public AI_Snake() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
addKeyListener(this);
snake = new Snake();
foods = new ArrayList<Point>();
start();
}
private void start() {
running = true;
thread = new Thread(this);
thread.start();
}
private void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void generateFood() {
Random rand = new Random();
int x = rand.nextInt(WIDTH / 10) * 10;
int y = rand.nextInt(HEIGHT / 10) * 10;
foods.add(new Point(x, y));
}
private boolean checkCollision() {
int headX = snake.getHead().x;
int headY = snake.getHead().y;
if (headX < 0 || headX >= WIDTH || headY < 0 || headY >= HEIGHT) {
return true;
}
for (Point p : snake.getBody()) {
if (headX == p.x && headY == p.y) {
return true;
}
}
return false;
}
private boolean checkFood() {
int headX = snake.getHead().x;
int headY = snake.getHead().y;
for (Point p : foods) {
if (headX == p.x && headY == p.y) {
foods.remove(p);
snake.grow();
score++;
return true;
}
}
return false;
}
private void draw(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.drawRect(0, 0, WIDTH, HEIGHT);
g.drawString("Score: " + score, 10, 20);
if (gameover) {
g.drawString("Game Over", WIDTH / 2 - 30, HEIGHT / 2);
} else {
snake.draw(g);
for (Point p : foods) {
g.fillRect(p.x, p.y, Snake.SIZE, Snake.SIZE);
}
}
}
@Override
public void run() {
while (running) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (gameover) {
stop();
break;
}
snake.move(right, left, up, down);
if (checkCollision()) {
gameover = true;
continue;
}
if (checkFood()) {
generateFood();
}
repaint();
}
}
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_RIGHT:
right = true;
left = false;
up = false;
down = false;
break;
case KeyEvent.VK_LEFT:
right = false;
left = true;
up = false;
down = false;
break;
case KeyEvent.VK_UP:
right = false;
left = false;
up = true;
down = false;
break;
case KeyEvent.VK_DOWN:
right = false;
left = false;
up = false;
down = true;
break;
default:
break;
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setTitle("AI Snake");
frame.add(new AI_Snake(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static class Snake {
private static final int SIZE = 10;
private List<Point> body;
public Snake() {
body = new ArrayList<Point>();
body.add(new Point(50, 50));
body.add(new Point(40, 50));
body.add(new Point(30, 50));
}
public List<Point> getBody() {
return body;
}
public Point getHead() {
return body.get(0);
}
public void grow() {
Point last = body.get(body.size() - 1);
body.add(new Point(last.x, last.y));
}
public void move(boolean right, boolean left, boolean up, boolean down) {
for (int i = body.size() - 1; i > 0; i--) {
Point curr = body.get(i);
Point prev = body.get(i - 1);
curr.x = prev.x;
curr.y = prev.y;
}
Point head = getHead();
if (right) head.x += SIZE;
if (left) head.x -= SIZE;
if (up) head.y -= SIZE;
if (down) head.y += SIZE;
}
public void draw(Graphics g) {
for (Point p : body) {
g.fillRect(p.x, p.y, SIZE, SIZE);
}
}
}
}
```
阅读全文