原文: http://zetcode.com/gui/qt5/snake/

在 Qt5 教程的这一部分中,我们创建一个贪食蛇游戏克隆。

贪食蛇

贪食蛇是较旧的经典视频游戏。 它最初是在 70 年代后期创建的。 后来它被带到 PC 上。 在这个游戏中,玩家控制蛇。 目的是尽可能多地吃苹果。 蛇每次吃一个苹果,它的身体就会长大。 蛇必须避开墙壁和自己的身体。 该游戏有时称为 Nibbles。

开发

蛇的每个关节的大小为 10 像素。 蛇由光标键控制。 最初,蛇具有三个关节。 如果游戏结束,则在面板中间显示"Game Over"消息。

Snake.h

  1. #pragma once
  2. #include <QWidget>
  3. #include <QKeyEvent>
  4. class Snake : public QWidget {
  5. public:
  6. Snake(QWidget *parent = 0);
  7. protected:
  8. void paintEvent(QPaintEvent *);
  9. void timerEvent(QTimerEvent *);
  10. void keyPressEvent(QKeyEvent *);
  11. private:
  12. QImage dot;
  13. QImage head;
  14. QImage apple;
  15. static const int B_WIDTH = 300;
  16. static const int B_HEIGHT = 300;
  17. static const int DOT_SIZE = 10;
  18. static const int ALL_DOTS = 900;
  19. static const int RAND_POS = 29;
  20. static const int DELAY = 140;
  21. int timerId;
  22. int dots;
  23. int apple_x;
  24. int apple_y;
  25. int x[ALL_DOTS];
  26. int y[ALL_DOTS];
  27. bool leftDirection;
  28. bool rightDirection;
  29. bool upDirection;
  30. bool downDirection;
  31. bool inGame;
  32. void loadImages();
  33. void initGame();
  34. void locateApple();
  35. void checkApple();
  36. void checkCollision();
  37. void move();
  38. void doDrawing();
  39. void gameOver(QPainter &);
  40. };

这是头文件。

  1. static const int B_WIDTH = 300;
  2. static const int B_HEIGHT = 300;
  3. static const int DOT_SIZE = 10;
  4. static const int ALL_DOTS = 900;
  5. static const int RAND_POS = 29;
  6. static const int DELAY = 140;

B_WIDTHB_HEIGHT常数确定电路板的大小。 DOT_SIZE是苹果的大小和蛇的点。 ALL_DOTS常数定义了板上可能的最大点数(900 = (300 * 300) / (10 * 10))。 RAND_POS常数用于计算苹果的随机位置。 DELAY常数确定游戏的速度。

  1. int x[ALL_DOTS];
  2. int y[ALL_DOTS];

这两个数组保存着蛇所有关节的 x 和 y 坐标。

snake.cpp

  1. #include <QPainter>
  2. #include <QTime>
  3. #include "snake.h"
  4. Snake::Snake(QWidget *parent) : QWidget(parent) {
  5. setStyleSheet("background-color:black;");
  6. leftDirection = false;
  7. rightDirection = true;
  8. upDirection = false;
  9. downDirection = false;
  10. inGame = true;
  11. resize(B_WIDTH, B_HEIGHT);
  12. loadImages();
  13. initGame();
  14. }
  15. void Snake::loadImages() {
  16. dot.load("dot.png");
  17. head.load("head.png");
  18. apple.load("apple.png");
  19. }
  20. void Snake::initGame() {
  21. dots = 3;
  22. for (int z = 0; z < dots; z++) {
  23. x[z] = 50 - z * 10;
  24. y[z] = 50;
  25. }
  26. locateApple();
  27. timerId = startTimer(DELAY);
  28. }
  29. void Snake::paintEvent(QPaintEvent *e) {
  30. Q_UNUSED(e);
  31. doDrawing();
  32. }
  33. void Snake::doDrawing() {
  34. QPainter qp(this);
  35. if (inGame) {
  36. qp.drawImage(apple_x, apple_y, apple);
  37. for (int z = 0; z < dots; z++) {
  38. if (z == 0) {
  39. qp.drawImage(x[z], y[z], head);
  40. } else {
  41. qp.drawImage(x[z], y[z], dot);
  42. }
  43. }
  44. } else {
  45. gameOver(qp);
  46. }
  47. }
  48. void Snake::gameOver(QPainter &qp) {
  49. QString message = "Game over";
  50. QFont font("Courier", 15, QFont::DemiBold);
  51. QFontMetrics fm(font);
  52. int textWidth = fm.width(message);
  53. qp.setFont(font);
  54. int h = height();
  55. int w = width();
  56. qp.translate(QPoint(w/2, h/2));
  57. qp.drawText(-textWidth/2, 0, message);
  58. }
  59. void Snake::checkApple() {
  60. if ((x[0] == apple_x) && (y[0] == apple_y)) {
  61. dots++;
  62. locateApple();
  63. }
  64. }
  65. void Snake::move() {
  66. for (int z = dots; z > 0; z--) {
  67. x[z] = x[(z - 1)];
  68. y[z] = y[(z - 1)];
  69. }
  70. if (leftDirection) {
  71. x[0] -= DOT_SIZE;
  72. }
  73. if (rightDirection) {
  74. x[0] += DOT_SIZE;
  75. }
  76. if (upDirection) {
  77. y[0] -= DOT_SIZE;
  78. }
  79. if (downDirection) {
  80. y[0] += DOT_SIZE;
  81. }
  82. }
  83. void Snake::checkCollision() {
  84. for (int z = dots; z > 0; z--) {
  85. if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
  86. inGame = false;
  87. }
  88. }
  89. if (y[0] >= B_HEIGHT) {
  90. inGame = false;
  91. }
  92. if (y[0] < 0) {
  93. inGame = false;
  94. }
  95. if (x[0] >= B_WIDTH) {
  96. inGame = false;
  97. }
  98. if (x[0] < 0) {
  99. inGame = false;
  100. }
  101. if(!inGame) {
  102. killTimer(timerId);
  103. }
  104. }
  105. void Snake::locateApple() {
  106. QTime time = QTime::currentTime();
  107. qsrand((uint) time.msec());
  108. int r = qrand() % RAND_POS;
  109. apple_x = (r * DOT_SIZE);
  110. r = qrand() % RAND_POS;
  111. apple_y = (r * DOT_SIZE);
  112. }
  113. void Snake::timerEvent(QTimerEvent *e) {
  114. Q_UNUSED(e);
  115. if (inGame) {
  116. checkApple();
  117. checkCollision();
  118. move();
  119. }
  120. repaint();
  121. }
  122. void Snake::keyPressEvent(QKeyEvent *e) {
  123. int key = e->key();
  124. if ((key == Qt::Key_Left) && (!rightDirection)) {
  125. leftDirection = true;
  126. upDirection = false;
  127. downDirection = false;
  128. }
  129. if ((key == Qt::Key_Right) && (!leftDirection)) {
  130. rightDirection = true;
  131. upDirection = false;
  132. downDirection = false;
  133. }
  134. if ((key == Qt::Key_Up) && (!downDirection)) {
  135. upDirection = true;
  136. rightDirection = false;
  137. leftDirection = false;
  138. }
  139. if ((key == Qt::Key_Down) && (!upDirection)) {
  140. downDirection = true;
  141. rightDirection = false;
  142. leftDirection = false;
  143. }
  144. QWidget::keyPressEvent(e);
  145. }

snake.cpp文件中,我们有游戏的逻辑。

  1. void Snake::loadImages() {
  2. dot.load("dot.png");
  3. head.load("head.png");
  4. apple.load("apple.png");
  5. }

loadImages()方法中,我们获得了游戏的图像。 ImageIcon类用于显示 PNG 图像。

  1. void Snake::initGame() {
  2. dots = 3;
  3. for (int z = 0; z < dots; z++) {
  4. x[z] = 50 - z * 10;
  5. y[z] = 50;
  6. }
  7. locateApple();
  8. timerId = startTimer(DELAY);
  9. }

initGame()方法中,我们创建蛇,在板上随机放置一个苹果,然后启动计时器。

  1. void Snake::checkApple() {
  2. if ((x[0] == apple_x) && (y[0] == apple_y)) {
  3. dots++;
  4. locateApple();
  5. }
  6. }

如果苹果与头部碰撞,我们会增加蛇的关节数。 我们称locateApple()方法为随机放置一个新的Apple对象。

move()方法中,我们有游戏的关键算法。 要了解它,请看一下蛇是如何运动的。 我们控制蛇的头。 我们可以使用光标键更改其方向。 其余关节在链上向上移动一个位置。 第二关节移动到第一个关节的位置,第三关节移动到第二个关节的位置,依此类推。

  1. for (int z = dots; z > 0; z--) {
  2. x[z] = x[(z - 1)];
  3. y[z] = y[(z - 1)];
  4. }

该代码将关节向上移动。

  1. if (leftDirection) {
  2. x[0] -= DOT_SIZE;
  3. }

这条线将头向左移动。

checkCollision()方法中,我们确定蛇是否击中了自己或撞墙之一。

  1. for (int z = dots; z > 0; z--) {
  2. if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
  3. inGame = false;
  4. }
  5. }

如果蛇用头撞到其关节之一,则游戏结束。

  1. if (y[0] >= B_HEIGHT) {
  2. inGame = false;
  3. }

如果蛇击中了棋盘的底部,则游戏结束。

  1. void Snake::timerEvent(QTimerEvent *e) {
  2. Q_UNUSED(e);
  3. if (inGame) {
  4. checkApple();
  5. checkCollision();
  6. move();
  7. }
  8. repaint();
  9. }

timerEvent()方法形成游戏周期。 假设游戏尚未结束,我们将执行碰撞检测并进行移动。 repaint()使窗口重新绘制。

  1. if ((key == Qt::Key_Left) && (!rightDirection)) {
  2. leftDirection = true;
  3. upDirection = false;
  4. downDirection = false;
  5. }

如果单击左光标键,则将leftDirection变量设置为truemove()函数中使用此变量来更改蛇对象的坐标。 还要注意,当蛇向右行驶时,我们不能立即向左转。

Snake.java

  1. #include <QApplication>
  2. #include "snake.h"
  3. int main(int argc, char *argv[]) {
  4. QApplication app(argc, argv);
  5. Snake window;
  6. window.setWindowTitle("Snake");
  7. window.show();
  8. return app.exec();
  9. }

这是主要的类。

Qt5 中的贪食蛇 - 图1

图:贪食蛇

这是 Qt5 中的贪食蛇游戏。