用java语言编写一个贪吃蛇游戏代码
时间: 2023-09-20 14:03:40 浏览: 86
JAVA贪吃蛇游戏代码
以下是Java语言编写的简单贪吃蛇游戏代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JFrame implements ActionListener {
private final int WIDTH = 300;
private final int HEIGHT = 300;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RAND_POS = 29;
private final 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;
private Timer timer;
private Image ball;
private Image apple;
private Image head;
public SnakeGame() {
setTitle("Snake Game");
setBackground(Color.gray);
setResizable(false);
setSize(WIDTH, HEIGHT);
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();
addKeyListener(new TAdapter());
timer = new Timer(DELAY, this);
timer.start();
initGame();
}
public void initGame() {
dots = 3;
for (int i = 0; i < dots; i++) {
x[i] = 50 - i * 10;
y[i] = 50;
}
locateApple();
inGame = true;
}
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 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;
}
}
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 * DOT_SIZE;
r = (int) (Math.random() * RAND_POS);
apple_y = r * DOT_SIZE;
}
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
private class TAdapter extends KeyAdapter {
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 static void main(String[] args) {
new SnakeGame().setVisible(true);
}
}
```
在代码中,我们使用了Java Swing库,创建了一个JFrame窗口,通过继承JFrame类实现,利用paint()方法绘制游戏画面,并且使用了Timer类来控制游戏帧率,使用ImageIcon读取了游戏素材。同时,我们还实现了一些游戏逻辑,比如蛇的移动、碰撞检测、吃苹果等。最后,我们在main()方法中新建了一个SnakeGame对象并可见。
阅读全文