用java写一个贪吃蛇的游戏
时间: 2023-12-06 12:03:10 浏览: 28
好的,以下是一个简单的贪吃蛇游戏的 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.LinkedList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SnakeGame extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private static final int DOT_SIZE = 10;
private static final int ALL_DOTS = WIDTH * HEIGHT / DOT_SIZE / DOT_SIZE;
private static final int RAND_POS = WIDTH / DOT_SIZE;
private static final int DELAY = 100;
private static final int[][] DIRECTIONS = { { 0, -1 }, { 0, 1 }, { -1, 0 }, { 1, 0 } };
private static final Color COLOR_SNAKE = Color.GREEN;
private static final Color COLOR_FOOD = Color.RED;
private LinkedList<Point> snake;
private Point food;
private int direction;
private boolean inGame;
private Random rand;
public SnakeGame() {
this.setBackground(Color.BLACK);
this.setFocusable(true);
this.addKeyListener(this);
this.setPreferredSize(new java.awt.Dimension(WIDTH, HEIGHT));
initGame();
}
private void initGame() {
snake = new LinkedList<Point>();
direction = 2;
inGame = true;
rand = new Random();
for (int i = 0; i < 3; i++) {
snake.add(new Point(WIDTH / 2, HEIGHT / 2 + i * DOT_SIZE));
}
newFood();
new Thread(() -> {
while (inGame) {
move();
checkFood();
checkCollision();
repaint();
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
private void move() {
Point head = snake.getFirst();
int[] direction = DIRECTIONS[this.direction];
Point newHead = new Point(head.x + direction[0] * DOT_SIZE, head.y + direction[1] * DOT_SIZE);
snake.addFirst(newHead);
snake.removeLast();
}
private void checkFood() {
Point head = snake.getFirst();
if (head.equals(food)) {
snake.addLast(food);
newFood();
}
}
private void checkCollision() {
Point head = snake.getFirst();
if (head.x < 0 || head.y < 0 || head.x > WIDTH - DOT_SIZE || head.y > HEIGHT - DOT_SIZE || snake.contains(head, 1)) {
inGame = false;
}
}
private void newFood() {
int x = rand.nextInt(RAND_POS) * DOT_SIZE;
int y = rand.nextInt(RAND_POS) * DOT_SIZE;
food = new Point(x, y);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (inGame) {
g.setColor(COLOR_FOOD);
g.fillRect(food.x, food.y, DOT_SIZE, DOT_SIZE);
g.setColor(COLOR_SNAKE);
for (Point p : snake) {
g.fillRect(p.x, p.y, DOT_SIZE, DOT_SIZE);
}
} else {
g.setColor(Color.WHITE);
g.drawString("Game Over", WIDTH / 2 - 30, HEIGHT / 2);
}
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
direction = 2;
} else if (keyCode == KeyEvent.VK_RIGHT) {
direction = 3;
} else if (keyCode == KeyEvent.VK_UP) {
direction = 0;
} else if (keyCode == KeyEvent.VK_DOWN) {
direction = 1;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SnakeGame());
frame.pack();
frame.setVisible(true);
}
}
```
这是一个基于 Swing 的简单实现,通过继承 JPanel 类并实现 KeyListener 接口,来实现游戏的界面和控制。游戏的核心逻辑在 move、checkFood 和 checkCollision 方法中实现,其中 move 方法控制蛇的移动,checkFood 方法检查是否吃到了食物,checkCollision 方法检查是否碰到了边界或自身。游戏循环通过新开一个线程来实现,每隔一段时间执行一次游戏逻辑和绘制。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)