帮我用Java实现一个贪吃蛇游戏
时间: 2023-03-21 10:01:13 浏览: 82
好的,以下是一个简单的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 KeyListener {
private static final long serialVersionUID = 1L;
private static final int BOX_WIDTH = 10;
private static final int WIDTH = 60;
private static final int HEIGHT = 40;
private ArrayList<Point> snake = new ArrayList<Point>();
private Point food;
private int direction;
private Random random = new Random();
private boolean gameOver;
public SnakeGame() {
addKeyListener(this);
setFocusable(true);
newGame();
}
private void newGame() {
snake.clear();
snake.add(new Point(WIDTH / 2, HEIGHT / 2));
food = new Point(random.nextInt(WIDTH), random.nextInt(HEIGHT));
direction = KeyEvent.VK_RIGHT;
gameOver = false;
}
private void move() {
Point head = snake.get(0);
switch (direction) {
case KeyEvent.VK_UP:
snake.add(0, new Point(head.x, head.y - 1));
break;
case KeyEvent.VK_DOWN:
snake.add(0, new Point(head.x, head.y + 1));
break;
case KeyEvent.VK_LEFT:
snake.add(0, new Point(head.x - 1, head.y));
break;
case KeyEvent.VK_RIGHT:
snake.add(0, new Point(head.x + 1, head.y));
break;
}
if (snake.get(0).equals(food)) {
food = new Point(random.nextInt(WIDTH), random.nextInt(HEIGHT));
} else {
snake.remove(snake.size() - 1);
}
if (snake.get(0).x < 0 || snake.get(0).x >= WIDTH || snake.get(0).y < 0 || snake.get(0).y >= HEIGHT) {
gameOver = true;
}
for (int i = 1; i < snake.size(); i++) {
if (snake.get(0).equals(snake.get(i))) {
gameOver = true;
break;
}
}
}
private void paintSnake(Graphics g) {
g.setColor(Color.GREEN);
for (Point p : snake) {
g.fillRect(p.x * BOX_WIDTH, p.y * BOX_WIDTH, BOX_WIDTH, BOX_WIDTH);
}
}
private void paintFood(Graphics g) {
g.setColor(Color.RED);
g.fillRect(food.x * BOX_WIDTH, food.y * BOX_WIDTH, BOX_WIDTH, BOX_WIDTH);
}
private void paintGameOver(Graphics g) {
g.setColor(Color.BLACK);
g.drawString("GAME OVER", WIDTH * BOX_WIDTH / 2 - 30, HEIGHT * BOX_WIDTH / 2);
}
private void paintScore(Graphics g) {
g.setColor(Color.BLACK);
g.drawString("SCORE: " + (snake.size() - 1), 5, 15);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (gameOver) {
paintGameOver(g);
return;
}
paintSnake(g);
paintFood(g);
paintScore(g);
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP && direction != KeyEvent.VK_DOWN) {
direction = KeyEvent
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![](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)