作者:zvvq博客网
导读一个简单的贪吃蛇游戏代码
简单的贪吃蛇是一款经典的游戏,它以一个蛇在一个有边界的区域内移动、吃食物并不断增长身体长度为主要玩法。这个游戏在很多平台上都有版本,而下面是一个简单的贪吃蛇的Java代码示例。
首先,我们需要创建一个Snake类来表示贪吃蛇。这个类中包含了蛇的位置、移动方向和身体长度等信息。在构造方法中,我们可以初始化蛇的初始位置和长度。
接下来,我们需要实现游戏的主循环。在每一次循环中,我们首先判断用户是否输入了移动方向。如果有输入,我们可以根据输入来改变蛇的移动方向。然后,我们根据当前的移动方向来更新蛇的位置。如果蛇吃到了食物,我们需要增加蛇的身体长度,并在随机位置生成新的食物。如果蛇碰到了边界或者自己的身体,游戏结束。
下面是一个简单的贪吃蛇的Java代码示例:
```java
import java.awt.;
import java.awt.event.;
import javax.swing.;
public class Snake extends JFrame implements ActionListener {
private SnakePanel panel;
private Timer timer;
public Snake() {
panel = new SnakePanel();
timer = new Timer(00, this);
add(panel);
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
panel.keyPressed(e);
}
});
setResizable(false);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
timer.start();
}
public void actionPerformed(ActionEvent e) {
panel.actionPerformed(e);
}
public static void main(String[] args) {
new Snake();
}
}
class SnakePanel extends JPanel {
private static final int WIDTH = 00;
private static final int HEIGHT = 00;
private static final int UNIT_SIZE = 0;
private static final int GAME_UNITS = (WIDTH HEIGHT) / UNIT_SIZE;
private static final int DELAY = 00;
private final int[] x = new int[GAME_UNITS];
private final int[] y = new int[GAME_UNITS];
private int bodyParts = ;
private int foodEaten;
private int foodX;
private int foodY;
private char direction = &;R&;;
private boolean running = false;
private Timer timer;
public SnakePanel() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(new MyKeyAdapter());
startGame();
}
public void startGame() {
newFood();
running = true;
timer = new Timer(DELAY, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkFood();
checkCollisions();
}
repaint();
}
});
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
if (running) {
g.setColor(Color.RED);
g.fillOval(foodX, foodY, UNIT_SIZE, UNIT_SIZE);
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(, 0, 0));
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
}
g.setColor(Color.RED);
g.setFont(new Font("Ink Free", Font.BOLD, 0));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Score: " + foodEaten, (WIDTH - metrics.stringWidth("Score: " + foodEaten)) / , g.getFont().getSize());
} else {
gameOver(g);
}
}
public void move() {
for (int i = bodyParts; i > 0; i--) {
x[i] = x[i - ];
y[i] = y[i - ];
}
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;
}
}
public void checkFood() {
if ((x[0] == foodX) && (y[0] == foodY)) {
bodyParts++;
foodEaten++;
newFood();
}
}
public void checkCollisions() {
for (int i = bodyParts; i > 0; i--) {
if ((x[0] == x[i]) && (y[0] == y[i])) {
running = false;
break;
}
}
if (x[0] < 0 || x[0] >= WIDTH || y[0] < 0 || y[0] >= HEIGHT) {
running = false;
}
if (!running) {
timer.stop();
}
}
public void gameOver(Graphics g) {
g.setColor(Color.RED);
g.setFont(new Font("Ink Free", Font.BOLD, ));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Game Over", (WIDTH - metrics.stringWidth("Game Over")) / , HEIGHT / );
g.setColor(Color.RED);
g.setFont(new Font("Ink Free", Font.BOLD, 0));
FontMetrics metrics= getFontMetrics(g.getFont());
g.drawString("Score: " + foodEaten, (WIDTH - metrics.stringWidth("Score: " + foodEaten)) / , g.getFont().getSize());
}
public void newFood() {
foodX = (int) (Math.random() ((WIDTH / UNIT_SIZE) - )) UNIT_SIZE;
foodY = (int) (Math.random() ((HEIGHT / UNIT_SIZE) - )) UNIT_SIZE;
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if (direction != &;R&;) {
direction = &;L&;;
}
break;
case KeyEvent.VK_RIGHT:
if (direction != &;L&;) {
direction = &;R&;;
}
break;
case KeyEvent.VK_UP:
if (direction != &;D&;) {
direction = &;U&;;
}
break;
case KeyEvent.VK_DOWN:
if (direction != &;U&;) {
direction = &;D&;;
}
break;
}
}
private class MyKeyAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
keyPressed(e);
}
}
public void actionPerformed(ActionEvent e) {
}
}
```
以上就是一个简单的贪吃蛇的Java代码示例。这个示例中使用了Java的Swing库来实现游戏界面,并通过定时器来控制游戏的主循环。玩家可以通过键盘输入来控制蛇的移动方向,通过吃食物来增加分数和身体长度。当蛇碰到边界或者自己的身体时,游戏结束。
这个示例只是一个简单的贪吃蛇游戏代码,你可以根据自己的需求和喜好进行扩展和修改。希望这个示例对你有所帮助!
免责声明:本文来源于网络,如有侵权请联系我们!
上一篇:一个简单的Java小游戏的代码
下一篇:在线运行Java代码的好处有哪些