用java写一个贪吃蛇小游戏
时间: 2023-12-07 16:37:10 浏览: 85
用java编写的一个贪吃蛇的小游戏
当然可以!这里是一个简单的贪吃蛇小游戏的Java代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SnakeGame extends JFrame {
private Snake snake;
private Timer timer;
private boolean gameOver;
public SnakeGame() {
setTitle("贪吃蛇游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(400, 400);
setLocationRelativeTo(null);
snake = new Snake();
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
snake.keyPressed(e.getKeyCode());
}
});
timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!gameOver) {
snake.move();
if (snake.collidedWithSelf() || snake.collidedWithWall()) {
gameOver = true;
}
repaint();
}
}
});
timer.start();
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (gameOver) {
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("游戏结束", 150, 180);
} else {
snake.draw(g);
}
}
public static void main(String[] args) {
new SnakeGame();
}
}
class Snake {
private static final int UNIT_SIZE = 20;
private static final int GAME_UNITS = (400 * 400) / (UNIT_SIZE * UNIT_SIZE);
private static final int DELAY = 100;
private int[] x;
private int[] y;
private int bodyParts;
private int appleX;
private int appleY;
private char direction;
private boolean moving;
public Snake() {
x = new int[GAME_UNITS];
y = new int[GAME_UNITS];
bodyParts = 6;
direction = 'R';
generateApple();
moving = true;
}
public void generateApple() {
appleX = (int) (Math.random() * (400 / UNIT_SIZE)) * UNIT_SIZE;
appleY = (int) (Math.random() * (400 / UNIT_SIZE)) * UNIT_SIZE;
}
public void draw(Graphics g) {
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.GREEN);
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
} else {
g.setColor(new Color(45, 180, 0));
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
}
g.setColor(Color.RED);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
}
public void move() {
for (int i = bodyParts; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
switch (direction) {
case 'U':
y[0] -= UNIT_SIZE;
break;
case 'D':
y[0] += UNIT_SIZE;
break;
case 'L':
x[0] -= UNIT_SIZE;
break;
case 'R':
x[0] += UNIT_SIZE;
break;
}
if (x[0] == appleX && y[0] == appleY) {
bodyParts++;
generateApple();
}
moving = true;
}
public boolean collidedWithSelf() {
for (int i = bodyParts; i > 0; i--) {
if (x[0] == x[i] && y[0] == y[i]) {
return true;
}
}
return false;
}
public boolean collidedWithWall() {
return x[0] < 0 || x[0] >= 400 || y[0] < 0 || y[0] >= 400;
}
public void keyPressed(int keyCode) {
switch (keyCode) {
case KeyEvent.VK_UP:
if (!moving) {
direction = 'U';
moving = true;
}
break;
case KeyEvent.VK_DOWN:
if (!moving) {
direction = 'D';
moving = true;
}
break;
case KeyEvent.VK_LEFT:
if (!moving) {
direction = 'L';
moving = true;
}
break;
case KeyEvent.VK_RIGHT:
if (!moving) {
direction = 'R';
moving = true;
}
break;
}
}
}
```
这个代码示例使用Java的Swing库来创建窗口和绘制游戏界面。贪吃蛇的移动由定时器控制,当贪吃蛇与自身或墙壁发生碰撞时游戏结束。你可以尝试运行这段代码,体验一下这个简单的贪吃蛇小游戏!
阅读全文