JAVA课程设计贪吃蛇代码
时间: 2023-07-09 07:49:20 浏览: 188
好的,以下是一个简单的JAVA贪吃蛇游戏的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class SnakeGame extends JFrame implements ActionListener {
private static final long serialVersionUID = 6678292058307426314L;
private JPanel gamePanel;
private JPanel scorePanel;
private JLabel scoreLabel;
private Timer timer;
private int score = 0;
private int[][] snake = new int[100][2];
private int foodX, foodY;
private int length = 3;
private int direction = KeyEvent.VK_RIGHT;
public SnakeGame() {
// 设置游戏界面
setTitle("贪吃蛇游戏");
setSize(new Dimension(400, 400));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置游戏面板
gamePanel = new JPanel();
gamePanel.setBackground(Color.WHITE);
gamePanel.setLayout(null);
gamePanel.setPreferredSize(new Dimension(300, 300));
add(gamePanel, BorderLayout.CENTER);
// 设置分数面板
scorePanel = new JPanel();
scorePanel.setLayout(new FlowLayout());
scoreLabel = new JLabel("得分:0");
scorePanel.add(scoreLabel);
add(scorePanel, BorderLayout.SOUTH);
// 初始化蛇的位置
snake[0][0] = 100;
snake[0][1] = 100;
snake[1][0] = 80;
snake[1][1] = 100;
snake[2][0] = 60;
snake[2][1] = 100;
// 随机生成食物的位置
generateFood();
// 设置定时器
timer = new Timer(100, this);
timer.start();
// 添加键盘监听器,控制方向
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT && direction != KeyEvent.VK_RIGHT) {
direction = KeyEvent.VK_LEFT;
} else if (key == KeyEvent.VK_RIGHT && direction != KeyEvent.VK_LEFT) {
direction = KeyEvent.VK_RIGHT;
} else if (key == KeyEvent.VK_UP && direction != KeyEvent.VK_DOWN) {
direction = KeyEvent.VK_UP;
} else if (key == KeyEvent.VK_DOWN && direction != KeyEvent.VK_UP) {
direction = KeyEvent.VK_DOWN;
}
}
});
// 显示游戏界面
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// 移动蛇的身体
for (int i = length - 1; i > 0; i--) {
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
// 根据方向移动蛇头
if (direction == KeyEvent.VK_LEFT) {
snake[0][0] -= 20;
} else if (direction == KeyEvent.VK_RIGHT) {
snake[0][0] += 20;
} else if (direction == KeyEvent.VK_UP) {
snake[0][1] -= 20;
} else if (direction == KeyEvent.VK_DOWN) {
snake[0][1] += 20;
}
// 判断蛇是否吃到食物
if (snake[0][0] == foodX && snake[0][1] == foodY) {
score += 10;
scoreLabel.setText("得分:" + score);
length++;
generateFood();
}
// 判断蛇是否撞墙或自咬
for (int i = 1; i < length; i++) {
if (snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1]) {
gameOver();
}
}
if (snake[0][0] < 0 || snake[0][0] >= 300 || snake[0][1] < 0 || snake[0][1] >= 300) {
gameOver();
}
// 重新绘制游戏面板
gamePanel.repaint();
}
private void generateFood() {
Random random = new Random();
foodX = random.nextInt(15) * 20;
foodY = random.nextInt(15) * 20;
}
private void gameOver() {
JOptionPane.showMessageDialog(this, "游戏结束,得分:" + score);
timer.stop();
}
public void paint(Graphics g) {
super.paint(g);
// 绘制食物
g.setColor(Color.RED);
g.fillOval(foodX, foodY, 20, 20);
// 绘制蛇
g.setColor(Color.BLACK);
for (int i = 0; i < length; i++) {
g.fillRect(snake[i][0], snake[i][1], 20, 20);
}
}
public static void main(String[] args) {
new SnakeGame();
}
}
```
代码中使用了JFrame、JPanel、JLabel、Timer等Swing组件和KeyEvent、KeyAdapter等AWT事件,实现了一个简单的贪吃蛇游戏。其中,蛇的运动、食物的生成、得分的计算等逻辑由 actionPerformed 方法实现,游戏界面的绘制由 paint 方法实现。在 main 方法中创建了一个 SnakeGame 对象,启动了游戏。
阅读全文