贪吃蛇小游戏

 
  1. import java.awt.*;

  2. import java.awt.event.*;

  3. import javax.swing.*;

  4. public class SnakeGame extends JFrame {

  5. private static final int TILE_SIZE = 25;

  6. private static final int GRID_WIDTH = 20;

  7. private static final int GRID_HEIGHT = 20;

  8. private Snake snake;

  9. private Timer gameTimer;

  10. public SnakeGame() {

  11. snake = new Snake();

  12. gameTimer = new Timer(200, new GameLoopListener());

  13. setTitle("贪吃蛇");

  14. setSize(GRID_WIDTH * TILE_SIZE, GRID_HEIGHT * TILE_SIZE);

  15. setDefaultCloseOperation(EXIT_ON_CLOSE);

  16. setResizable(false);

  17. setVisible(true);

  18. addKeyListener(new SnakeKeyListener());

  19. startGame();

  20. }

  21. private void startGame() {

  22. snake.initialize(GRID_WIDTH / 2, GRID_HEIGHT / 2);

  23. gameTimer.start();

  24. }

  25. private void gameOver() {

  26. gameTimer.stop();

  27. JOptionPane.showMessageDialog(this, "游戏结束", "游戏结束", JOptionPane.INFORMATION_MESSAGE);

  28. startGame();

  29. }

  30. private void update() {

  31. if (!snake.move()) {

  32. gameOver();

  33. }

  34. }

  35. private void draw(Graphics g) {

  36. g.clearRect(0, 0, getWidth(), getHeight());

  37. g.setColor(Color.GREEN);

  38. for (Point point : snake.getBody()) {

  39. g.fillRect(point.x * TILE_SIZE, point.y * TILE_SIZE, TILE_SIZE, TILE_SIZE);

  40. }

  41. }

  42. private class GameLoopListener implements ActionListener {

  43. public void actionPerformed(ActionEvent e) {

  44. update();

  45. repaint();

  46. }

  47. }

  48. private class SnakeKeyListener extends KeyAdapter {

  49. public void keyPressed(KeyEvent e) {

  50. int keyCode = e.getKeyCode();

  51. if (keyCode == KeyEvent.VK_UP && snake.getDirection() != Snake.Direction.DOWN) {

  52. snake.setDirection(Snake.Direction.UP);

  53. } else if (keyCode == KeyEvent.VK_DOWN && snake.getDirection() != Snake.Direction.UP) {

  54. snake.setDirection(Snake.Direction.DOWN);

  55. } else if (keyCode == KeyEvent.VK_LEFT && snake.getDirection() != Snake.Direction.RIGHT) {

  56. snake.setDirection(Snake.Direction.LEFT);

  57. } else if (keyCode == KeyEvent.VK_RIGHT && snake.getDirection() != Snake.Direction.LEFT) {

  58. snake.setDirection(Snake.Direction.RIGHT);

  59. }

  60. }

  61. }

  62. private class Snake {

  63. private LinkedList<Point> body;

  64. private Direction direction;

  65. public Snake() {

  66. body = new LinkedList<>();

  67. direction = Direction.RIGHT;

  68. }

  69. public void initialize(int x, int y) {

  70. body.clear();

  71. body.add(new Point(x, y));

  72. }

  73. public LinkedList<Point> getBody() {

  74. return body;

  75. }

  76. public Direction getDirection() {

  77. return direction;

  78. }

  79. public void setDirection(Direction direction) {

  80. this.direction = direction;

  81. }

  82. public boolean move() {

  83. Point head = body.getFirst();

  84. Point newHead;

  85. switch (direction) {

  86. case UP:

  87. newHead = new Point(head.x, head.y - 1);

  88. break;

  89. case DOWN:

  90. newHead = new Point(head.x, head.y + 1);

  91. break;

  92. case LEFT:

  93. newHead = new Point(head.x - 1, head.y);

  94. break;

  95. case RIGHT:

  96. newHead = new Point(head.x + 1, head.y);

  97. break;

  98. default:

  99. return false;

  100. }

  101. if (newHead.x < 0 || newHead.x >= GRID_WIDTH || newHead.y < 0 || newHead.y >= GRID_HEIGHT

  102. || body.contains(newHead)) {

  103. return false;

  104. }

  105. body.addFirst(newHead);

  106. if (body.size() > 1 && !newHead.equals(food)) {

  107. body.removeLast();

  108. }

  109. return true;

  110. }

  111. public enum Direction {

  112. UP, DOWN, LEFT, RIGHT

  113. }

  114. }

  115. public void paint(Graphics g) {

  116. super.paint(g);

  117. draw(g);

  118. }

  119. public static void main(String[] args) {

  120. new SnakeGame();

  121. }

  122. }

这个示例使用了 Java 的 Swing 库来创建游戏界面,并提供了基本的贪吃蛇游戏功能。按下上、下、左或右箭头键来控制贪吃蛇的移动方向。

请注意,这只是一个简单的示例,可能还有很多可以改进和优化的地方。

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值