当前位置:   article > 正文

java实现贪吃蛇游戏(附源码)_java贪吃蛇游戏源代码

java贪吃蛇游戏源代码

实现一个简单的贪吃蛇游戏需要使用Java的图形库,通常可以使用Swing或JavaFX。下面是一个使用Swing库来创建一个简单贪吃蛇游戏的示例代码:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.KeyAdapter;
  6. import java.awt.event.KeyEvent;
  7. import java.util.LinkedList;
  8. import java.util.Random;
  9. public class SnakeGame extends JPanel implements ActionListener {
  10. private final int CELL_SIZE = 20;
  11. private final int GRID_WIDTH = 20;
  12. private final int GRID_HEIGHT = 20;
  13. private final int GAME_SPEED = 150;
  14. private LinkedList<Point> snake;
  15. private Point food;
  16. private int direction;
  17. private boolean isMoving;
  18. private boolean isGameOver;
  19. public SnakeGame() {
  20. setPreferredSize(new Dimension(CELL_SIZE * GRID_WIDTH, CELL_SIZE * GRID_HEIGHT));
  21. setBackground(Color.BLACK);
  22. setFocusable(true);
  23. addKeyListener(new KeyAdapter() {
  24. @Override
  25. public void keyPressed(KeyEvent e) {
  26. int key = e.getKeyCode();
  27. if ((key == KeyEvent.VK_LEFT) && (direction != 1)) direction = 0;
  28. if ((key == KeyEvent.VK_RIGHT) && (direction != 0)) direction = 1;
  29. if ((key == KeyEvent.VK_UP) && (direction != 3)) direction = 2;
  30. if ((key == KeyEvent.VK_DOWN) && (direction != 2)) direction = 3;
  31. }
  32. });
  33. snake = new LinkedList<>();
  34. generateFood();
  35. isMoving = true;
  36. isGameOver = false;
  37. Timer timer = new Timer(GAME_SPEED, this);
  38. timer.start();
  39. }
  40. @Override
  41. public void actionPerformed(ActionEvent e) {
  42. if (isMoving && !isGameOver) {
  43. move();
  44. checkCollision();
  45. repaint();
  46. }
  47. }
  48. private void generateFood() {
  49. Random rand = new Random();
  50. int x, y;
  51. do {
  52. x = rand.nextInt(GRID_WIDTH);
  53. y = rand.nextInt(GRID_HEIGHT);
  54. } while (snake.contains(new Point(x, y));
  55. food = new Point(x, y);
  56. }
  57. private void move() {
  58. Point head = snake.peekFirst();
  59. Point newHead;
  60. switch (direction) {
  61. case 0:
  62. newHead = new Point(head.x - 1, head.y);
  63. break;
  64. case 1:
  65. newHead = new Point(head.x + 1, head.y);
  66. break;
  67. case 2:
  68. newHead = new Point(head.x, head.y - 1);
  69. break;
  70. case 3:
  71. newHead = new Point(head.x, head.y + 1);
  72. break;
  73. default:
  74. newHead = head;
  75. }
  76. snake.addFirst(newHead);
  77. if (newHead.equals(food)) {
  78. generateFood();
  79. } else {
  80. snake.removeLast();
  81. }
  82. }
  83. private void checkCollision() {
  84. if (snake.size() == GRID_WIDTH * GRID_HEIGHT) {
  85. isMoving = false;
  86. isGameOver = true;
  87. }
  88. Point head = snake.peekFirst();
  89. if (head.x < 0 || head.x >= GRID_WIDTH || head.y < 0 || head.y >= GRID_HEIGHT) {
  90. isMoving = false;
  91. isGameOver = true;
  92. }
  93. if (snake.size() > 1 && snake.contains(head)) {
  94. isMoving = false;
  95. isGameOver = true;
  96. }
  97. }
  98. @Override
  99. protected void paintComponent(Graphics g) {
  100. super.paintComponent(g);
  101. if (isGameOver) {
  102. g.setColor(Color.WHITE);
  103. g.setFont(new Font("SansSerif", Font.BOLD, 30));
  104. g.drawString("Game Over!", CELL_SIZE * 5, CELL_SIZE * 10);
  105. } else {
  106. // 绘制食物
  107. g.setColor(Color.RED);
  108. g.fillRect(food.x * CELL_SIZE, food.y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
  109. // 绘制蛇
  110. g.setColor(Color.GREEN);
  111. for (Point p : snake) {
  112. g.fillRect(p.x * CELL_SIZE, p.y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
  113. }
  114. }
  115. }
  116. public static void main(String[] args) {
  117. JFrame frame = new JFrame("贪吃蛇游戏");
  118. SnakeGame game = new SnakeGame();
  119. frame.add(game);
  120. frame.pack();
  121. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  122. frame.setLocationRelativeTo(null);
  123. frame.setVisible(true);
  124. }
  125. }

这个示例创建了一个简单的贪吃蛇游戏,使用了Swing库来构建图形用户界面。玩家可以使用方向键控制蛇的移动,目标是吃到食物,躲避碰撞和自身。游戏会在一定条件下结束,显示"Game Over"。你可以根据需要对游戏进行扩展和改进。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/699470
推荐阅读
相关标签
  

闽ICP备14008679号