用java写贪吃蛇游戏,用上下左右键盘控制,吃完蛇身变长
时间: 2023-05-23 09:04:16 浏览: 80
A:
以下是Java实现贪吃蛇游戏的步骤:
1.创建一个Snake类,表示贪吃蛇。该类中包括:
a.表示蛇身体的数组;
b.蛇头的位置;
c.蛇身的长度;
d.蛇的成长速度;
e.蛇的方向;
f.蛇是否死亡。
2.创建一个Food类,表示食物。该类中包括:
a.表示食物的位置。
3.在窗口中绘制贪吃蛇和食物。
4.实现键盘控制。通过键盘可以改变蛇的方向。
5.监听贪吃蛇与食物的碰撞。如果碰撞了,则加分并增加蛇身长度。
以下是一个简单的Java代码实现:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class SnakeGame extends JFrame implements KeyListener {
private Snake snake;
private Food food;
private int score;
private JLabel scoreLabel;
public static void main(String[] args) {
new SnakeGame();
}
SnakeGame() {
setTitle("贪吃蛇");
setSize(400, 400);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 400));
panel.setBackground(Color.white);
add(panel);
scoreLabel = new JLabel("得分:" + score);
panel.add(scoreLabel);
snake = new Snake();
food = new Food();
draw();
setVisible(true);
addKeyListener(this);
}
private void draw() {
Graphics g = getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 400, 400);
food.draw(g);
snake.draw(g);
g.dispose();
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_UP:
snake.changeDirection(SnakeDirection.UP);
break;
case KeyEvent.VK_DOWN:
snake.changeDirection(SnakeDirection.DOWN);
break;
case KeyEvent.VK_LEFT:
snake.changeDirection(SnakeDirection.LEFT);
break;
case KeyEvent.VK_RIGHT:
snake.changeDirection(SnakeDirection.RIGHT);
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
private class Snake {
private int length;
private int growSpeed;
private SnakeDirection direction;
private int[][] body;
private boolean dead;
Snake() {
length = 1;
growSpeed = 2;
direction = SnakeDirection.RIGHT;
body = new int[100][2];
body[0][0] = 10;
body[0][1] = 10;
dead = false;
}
void draw(Graphics g) {
g.setColor(Color.green);
for (int i = 0; i < length; i++) {
g.fillRect(body[i][0], body[i][1], 10, 10);
}
}
void changeDirection(SnakeDirection newDirection) {
if (direction == SnakeDirection.UP && newDirection != SnakeDirection.DOWN) {
direction = newDirection;
} else if (direction == SnakeDirection.DOWN && newDirection != SnakeDirection.UP) {
direction = newDirection;
} else if (direction == SnakeDirection.LEFT && newDirection != SnakeDirection.RIGHT) {
direction = newDirection;
} else if (direction == SnakeDirection.RIGHT && newDirection != SnakeDirection.LEFT) {
direction = newDirection;
}
}
void move() {
if (dead) {
return;
}
for (int i = length - 1; i > 0; i--) {
body[i][0] = body[i - 1][0];
body[i][1] = body[i - 1][1];
}
switch (direction) {
case UP:
body[0][1] -= 10;
break;
case DOWN:
body[0][1] += 10;
break;
case LEFT:
body[0][0] -= 10;
break;
case RIGHT:
body[0][0] += 10;
break;
}
if (body[0][0] < 0 || body[0][0] >= 400 || body[0][1] < 0 || body[0][1] >= 400) {
dead = true;
}
for (int i = 1; i < length; i++) {
if (body[0][0] == body[i][0] && body[0][1] == body[i][1]) {
dead = true;
break;
}
}
if (body[0][0] == food.x && body[0][1] == food.y) {
score += growSpeed;
scoreLabel.setText("得分:" + score);
length += growSpeed;
Random random = new Random();
food.x = random.nextInt(39) * 10;
food.y = random.nextInt(39) * 10;
}
}
}
private class Food {
int x;
int y;
Food() {
Random random = new Random();
x = random.nextInt(39) * 10;
y = random.nextInt(39) * 10;
}
void draw(Graphics g) {
g.setColor(Color.red);
g.fillOval(x, y, 10, 10);
}
}
private enum SnakeDirection {
UP,
DOWN,
LEFT,
RIGHT
}
}
```
以上是用Java语言实现贪吃蛇游戏的基本步骤,你可以和小伙伴一起挑战自己的最高分数。
阅读全文