用Java写贪吃蛇
时间: 2023-08-17 14:05:13 浏览: 58
好的,以下是一个简单的Java贪吃蛇游戏的代码示例:
```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.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SnakeGame extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private Thread thread;
private boolean running;
private boolean right = true, left = false, up = false, down = false;
private BodyPart b;
private ArrayList<BodyPart> snake;
private int xCoor = 10, yCoor = 10, size = 5;
private int ticks = 0;
private Point apple;
private Random random;
private int score;
public SnakeGame() {
setFocusable(true);
addKeyListener(this);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
random = new Random();
snake = new ArrayList<BodyPart>();
start();
}
public void start() {
running = true;
thread = new Thread(this);
thread.start();
}
public void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void tick() {
if (snake.size() == 0) {
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
}
ticks++;
if (ticks > 250000) {
if (right) xCoor++;
if (left) xCoor--;
if (up) yCoor--;
if (down) yCoor++;
ticks = 0;
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
if (snake.size() > size) {
snake.remove(0);
}
}
if (apple != null) {
if (xCoor == apple.x && yCoor == apple.y) {
size++;
score++;
apple.setLocation(random.nextInt(49) * 10, random.nextInt(49) * 10);
}
} else {
apple = new Point(random.nextInt(49) * 10, random.nextInt(49) * 10);
}
for (int i = 0; i < snake.size(); i++) {
if (xCoor == snake.get(i).getxCoor() && yCoor == snake.get(i).getyCoor()) {
if (i != snake.size() - 1) {
stop();
}
}
}
if (xCoor < 0 || xCoor > 49 || yCoor < 0 || yCoor > 49) {
stop();
}
}
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.WHITE);
g.drawString("Score: " + score, 10, 20);
for (int i = 0; i < snake.size(); i++) {
snake.get(i).draw(g);
}
g.setColor(Color.RED);
g.fillRect(apple.x, apple.y, 10, 10);
g.setColor(Color.WHITE);
g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
}
public void run() {
while (running) {
tick();
repaint();
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT && !left) {
right = true;
up = false;
down = false;
}
if (key == KeyEvent.VK_LEFT && !right) {
left = true;
up = false;
down = false;
}
if (key == KeyEvent.VK_UP && !down) {
up = true;
left = false;
right = false;
}
if (key == KeyEvent.VK_DOWN && !up) {
down = true;
left = false;
right = false;
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame();
SnakeGame snakeGame = new SnakeGame();
frame.add(snakeGame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Snake Game");
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class BodyPart {
private int xCoor, yCoor, width, height;
public BodyPart(int xCoor, int yCoor, int tileSize) {
this.xCoor = xCoor;
this.yCoor = yCoor;
width = tileSize;
height = tileSize;
}
public void tick() {}
public void draw(Graphics g) {
g.setColor(Color.GREEN);
g.fillRect(xCoor * width, yCoor * height, width, height);
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
this.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
this.yCoor = yCoor;
}
}
```
这个代码中,我们用到了Java的Swing库来创建游戏窗口和绘制游戏界面。SnakeGame类继承了JPanel类,并实现了Runnable和KeyListener接口。在start()方法中,我们启动了一个新的线程来运行游戏逻辑。在tick()方法中,我们处理了蛇的移动、吃苹果、撞墙、碰到自己等情况,并更新游戏状态。在paint()方法中,我们绘制了游戏界面。在keyPressed()方法中,我们处理了用户的方向键输入,并更新蛇的移动方向。最后,在main()方法中,我们创建了一个JFrame对象,并将SnakeGame添加到其中,然后显示窗口。