帮我利用java写一款贪吃蛇游戏
时间: 2023-07-01 17:21:59 浏览: 73
好的,下面是一个简单的贪吃蛇游戏的Java代码,希望能帮到你:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SnakeGame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel gamePanel;
private JButton startButton;
private Timer timer;
private final int WIDTH = 500;
private final int HEIGHT = 500;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RAND_POS = 29;
private int DELAY = 140;
private int[] x = new int[ALL_DOTS];
private int[] y = new int[ALL_DOTS];
private int dots;
private int apple_x;
private int apple_y;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
public SnakeGame() {
initUI();
}
private void initUI() {
setTitle("Snake Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
gamePanel = new JPanel();
gamePanel.setBackground(Color.black);
gamePanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
add(gamePanel, BorderLayout.CENTER);
startButton = new JButton("Start");
startButton.addActionListener(this);
add(startButton, BorderLayout.NORTH);
timer = new Timer(DELAY, this);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void startGame() {
dots = 3;
for (int i = 0; i < dots; i++) {
x[i] = 50 - i * DOT_SIZE;
y[i] = 50;
}
locateApple();
timer.start();
}
private void locateApple() {
int r = (int) (Math.random() * RAND_POS);
apple_x = r * DOT_SIZE;
r = (int) (Math.random() * RAND_POS);
apple_y = r * DOT_SIZE;
}
private void checkApple() {
if (x[0] == apple_x && y[0] == apple_y) {
dots++;
locateApple();
}
}
private void checkCollision() {
for (int i = dots; i > 0; i--) {
if (i > 4 && x[0] == x[i] && y[0] == y[i]) {
inGame = false;
}
}
if (y[0] >= HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] >= WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if (!inGame) {
timer.stop();
}
}
private void move() {
for (int i = dots; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
if (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
}
private void paint(Graphics g) {
super.paint(g);
if (inGame) {
g.setColor(Color.red);
g.fillOval(apple_x, apple_y, DOT_SIZE, DOT_SIZE);
for (int i = 0; i < dots; i++) {
if (i == 0) {
g.setColor(Color.green);
} else {
g.setColor(Color.white);
}
g.fillRect(x[i], y[i], DOT_SIZE, DOT_SIZE);
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
String msg = "Game Over";
Font font = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(font);
g.setColor(Color.white);
g.setFont(font);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2);
}
@Override
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
SnakeGame game = new SnakeGame();
game.setVisible(true);
});
}
}
```
这是一个比较简单的实现,可以根据需要自行修改和完善。
阅读全文