使用Java实现贪吃蛇
时间: 2024-05-16 09:12:38 浏览: 16
好的,以下是使用Java实现贪吃蛇的简单代码示例:
```java
import java.awt.Color;
import java.awt.Dimension;
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 static final int SIZE = 10;
private static final int SPEED = 40;
// 游戏是否结束标志
private boolean gameOver = false;
// 蛇的初始位置和方向
private ArrayList<Point> snake = new ArrayList<Point>();
private int direction = KeyEvent.VK_RIGHT;
// 食物的位置
private Point food;
// 随机数生成器
private Random random = new Random();
public SnakeGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
// 初始化蛇的位置
snake.add(new Point(100, 100));
snake.add(new Point(90, 100));
snake.add(new Point(80, 100));
// 初始化食物的位置
food = new Point(random.nextInt(WIDTH / SIZE) * SIZE, random.nextInt(HEIGHT / SIZE) * SIZE);
}
public void run() {
while (!gameOver) {
try {
Thread.sleep(SPEED);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 移动蛇的位置
Point head = snake.get(0);
Point newHead = new Point(head);
switch (direction) {
case KeyEvent.VK_UP:
newHead.y -= SIZE;
break;
case KeyEvent.VK_DOWN:
newHead.y += SIZE;
break;
case KeyEvent.VK_LEFT:
newHead.x -= SIZE;
break;
case KeyEvent.VK_RIGHT:
newHead.x += SIZE;
break;
}
snake.add(0, newHead);
snake.remove(snake.size() - 1);
// 判断是否吃到食物
if (newHead.equals(food)) {
food.setLocation(random.nextInt(WIDTH / SIZE) * SIZE, random.nextInt(HEIGHT / SIZE) * SIZE);
} else {
snake.remove(snake.size() - 1);
}
// 判断游戏是否结束
if (newHead.x < 0 || newHead.x >= WIDTH || newHead.y < 0 || newHead.y >= HEIGHT || snake.contains(newHead)) {
gameOver = true;
}
// 重新绘制游戏界面
repaint();
}
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
direction = e.getKeyCode();
break;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 绘制食物
g.setColor(Color.RED);
g.fillRect(food.x, food.y, SIZE, SIZE);
// 绘制蛇
g.setColor(Color.GREEN);
for (Point p : snake) {
g.fillRect(p.x, p.y, SIZE, SIZE);
}
// 绘制游戏结束信息
if (gameOver) {
g.setColor(Color.WHITE);
g.drawString("Game Over!", WIDTH / 2 - 40, HEIGHT / 2);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new SnakeGame());
frame.pack();
frame.setVisible(true);
Thread thread = new Thread((SnakeGame)frame.getContentPane());
thread.start();
}
}
```
运行该代码,即可开始玩贪吃蛇游戏。
相关推荐
![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_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)