21xrx.com
2024-11-08 22:12:06 Friday
登录
文章检索 我的文章 写文章
《用Java编写贪吃蛇游戏》
2023-06-15 11:10:08 深夜i     --     --
Java 贪吃蛇 游戏编程

贪吃蛇游戏是一款经典的游戏,在此我们将使用Java语言来编写一个简单的贪吃蛇游戏。以下是基本的实现代码:


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Snake extends JFrame implements ActionListener {

  private JPanel panel;

  private JButton startButton;

  private JButton stopButton;

  private JButton pauseButton;

  private Timer timer;

  private int DELAY = 150;

  private SnakeCanvas canvas;

  public Snake()

   // ...

  

  public void actionPerformed(ActionEvent e)

   // ...

  

  private void setupGUI()

   // ...

  

  public static void main(String[] args) {

    Snake game = new Snake();

    game.setVisible(true);

  }

}

在这个代码框架中,我们创建了一个窗口,然后在里面添加了三个按钮,分别是“开始”、“停止”和“暂停”。我们还创建了一个继承了JComponent的类SnakeCanvas来表示蛇的身体,以及使用了Java中的定时器Timer类,每隔一定时间会调用actionPerformed方法,使蛇前进一步。

接下来,我们需要实现键盘监听,使得当玩家点击方向键时,蛇会按照相应的方向前进。同样,在SnakeCanvas类中添加下面代码:


public void keyPressed(KeyEvent e) {

  int key = e.getKeyCode();

  if ((key == KeyEvent.VK_LEFT) && (direction != RIGHT))

    direction = LEFT;

  

  if ((key == KeyEvent.VK_RIGHT) && (direction != LEFT))  

    direction = RIGHT;

  

  if ((key == KeyEvent.VK_UP) && (direction != DOWN))

    direction = UP;

  

  if ((key == KeyEvent.VK_DOWN) && (direction != UP))

    direction = DOWN;

  

}

现在玩家可以通过方向键控制蛇的方向了。不过,蛇还不能吃食物,所以我们需要添加一些代码让蛇能够检测是否与食物碰撞,从而增加身体长度。

另外,我们需要处理蛇头碰到墙壁或自己的身体的时候,游戏应该结束。在SnakeCanvas类中添加以下代码:


if ((x[0] >= B_WIDTH) || (x[0] < 0) || (y[0] >= B_HEIGHT) || (y[0] < 0))

  inGame = false;

for (int z = snakeDots; z > 0; z--) {

  if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z]))

    inGame = false;

  

}

if (!inGame) {

  timer.stop();

}

现在,当蛇碰到边界或自己的身体时,游戏会结束。最后,我们添加一些纪录分数和重新开始的代码,使得游戏更加完善。


private void checkApple() {

  if ((x[0] == apple_x) && (y[0] == apple_y)) {

    snakeDots++;

    locateApple();

  }

}

private 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) && (direction != RIGHT))

      left = true;

      up = false;

      down = false;

    

    if ((key == KeyEvent.VK_RIGHT) && (direction != LEFT))  

      right = true;

      up = false;

      down = false;

    

    if ((key == KeyEvent.VK_UP) && (direction != DOWN))

      up = true;

      right = false;

      left = false;

    

    if ((key == KeyEvent.VK_DOWN) && (direction != UP))

      down = true;

      right = false;

      left = false;

    

    if ((key == KeyEvent.VK_SPACE) && (inGame == false)) {

      startGame();

    }

  }

}

public static void main(String[] args) {

  Snake game = new Snake();

  game.setVisible(true);

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复