用Java写一个贪吃蛇单机版游戏
时间: 2023-03-19 09:25:11 浏览: 87
很好!以下是一个简单的Java贪吃蛇游戏实现示例,仅供参考。它是基于Java Swing和AWT库构建的,使用了多个类来处理不同的游戏逻辑和GUI组件。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SnakeGame extends JFrame implements ActionListener {
private JPanel gamePanel;
private JButton startButton;
private JLabel scoreLabel;
private Timer timer;
private Snake snake;
private Food food;
private int score;
private boolean isGameOver;
public SnakeGame() {
setTitle("贪吃蛇游戏");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
gamePanel = new JPanel();
gamePanel.setLayout(null);
gamePanel.setBackground(Color.BLACK);
add(gamePanel);
startButton = new JButton("开始游戏");
startButton.addActionListener(this);
startButton.setBounds(150, 150, 100, 50);
gamePanel.add(startButton);
scoreLabel = new JLabel("得分:0");
scoreLabel.setForeground(Color.WHITE);
scoreLabel.setBounds(10, 10, 100, 20);
gamePanel.add(scoreLabel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
startGame();
} else if (!isGameOver) {
snake.move();
checkCollision();
gamePanel.repaint();
}
}
private void startGame() {
startButton.setEnabled(false);
gamePanel.requestFocus();
score = 0;
isGameOver = false;
snake = new Snake();
food = new Food();
timer = new Timer(100, this);
timer.start();
}
private void checkCollision() {
if (snake.hitWall()) {
gameOver();
} else if (snake.hitBody()) {
gameOver();
} else if (snake.head().equals(food.position())) {
snake.eat(food.position());
food.randomizePosition();
score += 10;
scoreLabel.setText("得分:" + score);
}
}
private void gameOver() {
isGameOver = true;
timer.stop();
startButton.setEnabled(true);
JOptionPane.showMessageDialog(this, "游戏结束!得分:" + score);
}
private class Snake {
private LinkedList<Point> body;
private int dx, dy;
public Snake() {
body = new LinkedList<Point>();
body.add(new Point(5, 5));
body.add(new Point(5, 6));
body.add(new Point(5, 7));
dx = 0;
dy = -1;
}
public void move() {
Point head = head();
Point newHead = new Point(head.x + dx, head.y + dy);
body.addFirst(newHead);
body.removeLast();
}
public void eat(Point foodPosition) {
body.addFirst(foodPosition);
}
public boolean hitWall() {
Point head = head();
return head.x < 0 || head.x >= 20 || head.y < 0 || head.y >= 20;
}
public boolean hitBody() {
Point head = head();
return body.indexOf(head) != body.lastIndexOf(head);
}
public Point head() {
return body.getFirst();
}
public LinkedList<Point> body() {
return body;
}
public void setDx(int dx) {
this.dx =
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)