怎么用java做贪吃蛇
时间: 2023-09-21 11:06:50 浏览: 45
下面是一个简单的 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 Runnable, KeyListener {
private static final long serialVersionUID = 1L;
private static final int BOX_WIDTH = 10;
private static final int BOX_HEIGHT = 10;
private static final int BOARD_WIDTH = 500;
private static final int BOARD_HEIGHT = 500;
private static final int START_SNAKE_LENGTH = 5;
private static final int SLEEP_TIME = 100;
private static final int MOVE_DIST = 10;
private static final int FOOD_SCORE = 10;
private LinkedList<Point> snake;
private Point food;
private int score;
private boolean gameOver;
private boolean paused;
public SnakeGame() {
this.snake = new LinkedList<Point>();
this.addKeyListener(this);
this.setFocusable(true);
this.requestFocus();
this.reset();
}
public void reset() {
this.snake.clear();
for (int i = START_SNAKE_LENGTH - 1; i >= 0; --i) {
this.snake.add(new Point(i * BOX_WIDTH, 0));
}
this.food = this.generateFood();
this.score = 0;
this.gameOver = false;
this.paused = false;
}
private Point generateFood() {
Random rand = new Random();
int x = rand.nextInt(BOARD_WIDTH / BOX_WIDTH) * BOX_WIDTH;
int y = rand.nextInt(BOARD_HEIGHT / BOX_HEIGHT) * BOX_HEIGHT;
return new Point(x, y);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.fillRect(this.food.x, this.food.y, BOX_WIDTH, BOX_HEIGHT);
g.setColor(Color.BLACK);
for (Point p : this.snake) {
g.fillRect(p.x, p.y, BOX_WIDTH, BOX_HEIGHT);
}
g.drawString("Score: " + this.score, 5, 15);
if (this.gameOver) {
g.drawString("Game Over. Press R to restart.", BOARD_WIDTH / 2 - 50, BOARD_HEIGHT / 2);
}
if (this.paused) {
g.drawString("Paused. Press P to resume.", BOARD_WIDTH / 2 - 50, BOARD_HEIGHT / 2);
}
}
public void run() {
while (!this.gameOver) {
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!this.paused) {
Point head = this.snake.getFirst();
Point newHead = new Point(head.x, head.y);
switch (this.direction) {
case KeyEvent.VK_LEFT:
newHead.x -= MOVE_DIST;
break;
case KeyEvent.VK_UP:
newHead.y -= MOVE_DIST;
break;
case KeyEvent.VK_RIGHT:
newHead.x += MOVE_DIST;
break;
case KeyEvent.VK_DOWN:
newHead.y += MOVE_DIST;
break;
}
if (newHead.x < 0 || newHead.x >= BOARD_WIDTH || newHead.y < 0 || newHead.y >= BOARD_HEIGHT) {
this.gameOver = true;
} else if (this.snake.contains(newHead)) {
this.gameOver = true;
} else {
this.snake.addFirst(newHead);
if (this.food.equals(newHead)) {
this.score += FOOD_SCORE;
this.food = this.generateFood();
} else {
this.snake.removeLast();
}
}
this.repaint();
}
}
}
private int direction;
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if (this.direction != KeyEvent.VK_RIGHT) {
this.direction = KeyEvent.VK_LEFT;
}
break;
case KeyEvent.VK_UP:
if (this.direction != KeyEvent.VK_DOWN) {
this.direction = KeyEvent.VK_UP;
}
break;
case KeyEvent.VK_RIGHT:
if (this.direction != KeyEvent.VK_LEFT) {
this.direction = KeyEvent.VK_RIGHT;
}
break;
case KeyEvent.VK_DOWN:
if (this.direction != KeyEvent.VK_UP) {
this.direction = KeyEvent.VK_DOWN;
}
break;
case KeyEvent.VK_R:
if (this.gameOver) {
this.reset();
new Thread(this).start();
}
break;
case KeyEvent.VK_P:
this.paused = !this.paused;
break;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(BOARD_WIDTH, BOARD_HEIGHT);
frame.add(new SnakeGame());
frame.setVisible(true);
}
}
```
这个例子中,我们使用 Java 的 Swing GUI 库来绘制游戏画面,并使用 Java 的多线程机制来实现游戏逻辑。在 `run()` 方法中,我们在每个循环中判断游戏是否结束,如果没有结束,则根据当前蛇头的位置和方向计算出新蛇头的位置,并根据新蛇头的位置来更新蛇身和食物位置。在 `keyPressed()` 方法中,我们处理玩家的按键操作,包括改变蛇的方向、重新开始游戏和暂停游戏。
相关推荐
![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_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)