用java帮我写一个贪吃蛇
时间: 2023-08-20 12:16:42 浏览: 81
好的,下面是一个简单的贪吃蛇游戏的Java代码实现,你可以根据自己的需要进行修改和完善:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Snake extends JFrame implements KeyListener, ActionListener {
private final int WIDTH = 500;
private final int HEIGHT = 500;
private final int CELL_SIZE = 20;
private final int MAX_DOTS = (WIDTH * HEIGHT) / (CELL_SIZE * CELL_SIZE);
private final int RAND_POS = WIDTH / CELL_SIZE;
private final int DELAY = 140;
private int dots;
private int apple_x;
private int apple_y;
private int[] x = new int[MAX_DOTS];
private int[] y = new int[MAX_DOTS];
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private Timer timer;
private Image ball;
private Image apple;
private Image head;
public Snake() {
setTitle("Snake");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setResizable(false);
setLocationRelativeTo(null);
addKeyListener(this);
initGame();
}
public void initGame() {
dots = 3;
for (int i = 0; i < dots; i++) {
x[i] = 50 - i * CELL_SIZE;
y[i] = 50;
}
locateApple();
timer = new Timer(DELAY, this);
timer.start();
loadImage();
}
public void loadImage() {
ImageIcon iid = new ImageIcon(getClass().getResource("dot.png"));
ball = iid.getImage();
ImageIcon iia = new ImageIcon(getClass().getResource("apple.png"));
apple = iia.getImage();
ImageIcon iih = new ImageIcon(getClass().getResource("head.png"));
head = iih.getImage();
}
public void move() {
for (int i = dots; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
if (leftDirection) {
x[0] -= CELL_SIZE;
}
if (rightDirection) {
x[0] += CELL_SIZE;
}
if (upDirection) {
y[0] -= CELL_SIZE;
}
if (downDirection) {
y[0] += CELL_SIZE;
}
}
public 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();
}
}
public void locateApple() {
int r = (int) (Math.random() * RAND_POS);
apple_x = r * CELL_SIZE;
r = (int) (Math.random() * RAND_POS);
apple_y = r * CELL_SIZE;
}
public void paint(Graphics g) {
super.paint(g);
if (inGame) {
g.drawImage(apple, apple_x, apple_y, this);
for (int i = 0; i < dots; i++) {
if (i == 0) {
g.drawImage(head, x[i], y[i], this);
} else {
g.drawImage(ball, x[i], y[i], this);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
public void gameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2);
}
public void checkApple() {
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
}
}
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
leftDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
rightDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_UP) && (!downDirection)) {
upDirection = true;
rightDirection = false;
leftDirection = false;
}
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
downDirection = true;
rightDirection = false;
leftDirection = false;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
new Snake().setVisible(true);
}
}
```
上面的代码实现了一个简单的贪吃蛇游戏,在窗口中显示出贪吃蛇和苹果,并且可以用键盘控制贪吃蛇的移动方向。你可以自己尝试运行一下,看看效果如何。
阅读全文