<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 貪食蛇 > 原文: [http://zetcode.com/gui/qtjambi/nibbles/](http://zetcode.com/gui/qtjambi/nibbles/) 在 QtJambi 編程教程的這一部分中,我們將創建一個貪食蛇游戲克隆。 貪食蛇是較舊的經典視頻游戲。 它最初是在 70 年代后期創建的。 后來它被帶到 PC 上。 在這個游戲中,玩家控制蛇。 目的是盡可能多地吃蘋果。 蛇每次吃一個蘋果,它的身體就會長大。 蛇必須避開墻壁和自己的身體。 ## 開發 蛇的每個關節的大小為 10px。 蛇由光標鍵控制。 最初,蛇具有三個關節。 游戲立即開始。 游戲結束后,我們在窗口中心顯示`"Game Over"`消息。 `Board.java` ```java package com.zetcode; import com.trolltech.qt.core.QBasicTimer; import com.trolltech.qt.core.QPoint; import com.trolltech.qt.core.QTimerEvent; import com.trolltech.qt.core.Qt; import com.trolltech.qt.gui.QColor; import com.trolltech.qt.gui.QFont; import com.trolltech.qt.gui.QFontMetrics; import com.trolltech.qt.gui.QFrame; import com.trolltech.qt.gui.QImage; import com.trolltech.qt.gui.QKeyEvent; import com.trolltech.qt.gui.QPaintEvent; import com.trolltech.qt.gui.QPainter; public class Board extends QFrame { private final int WIDTH = 300; private final int HEIGHT = 300; private final int DOT_SIZE = 10; private final int ALL_DOTS = 900; private final int RAND_POS = 29; private final int DELAY = 140; private int x[] = new int[ALL_DOTS]; private int y[] = new int[ALL_DOTS]; private int dots; private int apple_x; private int apple_y; private boolean left = false; private boolean right = true; private boolean up = false; private boolean down = false; private boolean inGame = true; private QBasicTimer timer; private QImage ball; private QImage apple; private QImage head; public Board() { setStyleSheet("QWidget { background-color: black }"); setFocusPolicy(Qt.FocusPolicy.StrongFocus); ball = new QImage("dot.png"); apple = new QImage("apple.png"); head = new QImage("head.png"); initGame(); } private void initGame() { dots = 3; for (int z = 0; z < dots; z++) { x[z] = 50 - z*10; y[z] = 50; } locateApple(); timer = new QBasicTimer(); timer.start(DELAY, this); } @Override public void paintEvent(QPaintEvent event) { super.paintEvent(event); QPainter painter = new QPainter(); painter.begin(this); if (inGame) { drawObjects(painter); } else { gameOver(painter); } painter.end(); } private void drawObjects(QPainter painter) { painter.drawImage(apple_x, apple_y, apple); for (int z = 0; z < dots; z++) { if (z == 0) painter.drawImage(x[z], y[z], head); else painter.drawImage(x[z], y[z], ball); } } private void gameOver(QPainter painter) { String msg = "Game Over"; QFont small = new QFont("Helvetica", 12, QFont.Weight.Bold.value()); QFontMetrics metr = new QFontMetrics(small); int textWidth = metr.width(msg); int h = height(); int w = width(); painter.setPen(QColor.white); painter.setFont(small); painter.translate(new QPoint(w/2, h/2)); painter.drawText(-textWidth/2, 0, msg); } private void checkApple() { if ((x[0] == apple_x) && (y[0] == apple_y)) { dots++; locateApple(); } } private void move() { for (int z = dots; z > 0; z--) { x[z] = x[(z - 1)]; y[z] = y[(z - 1)]; } if (left) { x[0] -= DOT_SIZE; } if (right) { x[0] += DOT_SIZE; } if (up) { y[0] -= DOT_SIZE; } if (down) { y[0] += DOT_SIZE; } } private void checkCollision() { for (int z = dots; z > 0; z--) { if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { inGame = false; } } if (y[0] > HEIGHT) { inGame = false; } if (y[0] < 0) { inGame = false; } if (x[0] > WIDTH) { inGame = false; } if (x[0] < 0) { inGame = false; } } 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)); } @Override protected void timerEvent(QTimerEvent event) { if (inGame) { checkApple(); checkCollision(); move(); } else { timer.stop(); } repaint(); } @Override public void keyPressEvent(QKeyEvent event) { int key = event.key(); if (key == Qt.Key.Key_Left.value() && !right) { left = true; up = false; down = false; } if ((key == Qt.Key.Key_Right.value()) && !left) { right = true; up = false; down = false; } if ((key == Qt.Key.Key_Up.value()) && !down) { up = true; right = false; left = false; } if ((key == Qt.Key.Key_Down.value()) && !up) { down = true; right = false; left = false; } } } ``` 首先,我們將定義一些在游戲中使用的全局變量。 `WIDTH`和`HEIGHT`常數確定電路板的大小。 `DOT_SIZE`是蘋果的大小和蛇的點。 `ALL_DOTS`常數定義了板上可能的最大點數。 `RAND_POS`常數用于計算蘋果的隨機位置。 `DELAY`常數確定游戲的速度。 ```java private int x[] = new int[ALL_DOTS]; private int y[] = new int[ALL_DOTS]; ``` 這兩個數組存儲蛇的所有可能關節的 x,y 坐標。 `initGame()`方法初始化變量,加載圖像并啟動超時功能。 ```java if (inGame) { drawObjects(painter); } else { gameOver(painter); } ``` 在`paintEvent()`方法內部,我們檢查`inGame`變量。 如果為真,則繪制對象。 蘋果和蛇的關節。 否則,我們顯示`"Game Over"`文本。 ```java private void drawObjects(QPainter painter) { painter.drawImage(apple_x, apple_y, apple); for (int z = 0; z < dots; z++) { if (z == 0) painter.drawImage(x[z], y[z], head); else painter.drawImage(x[z], y[z], ball); } } ``` `drawObjects()`方法繪制蘋果和蛇的關節。 蛇的第一個關節是其頭部,用紅色圓圈表示。 ```java private void checkApple() { if ((x[0] == apple_x) && (y[0] == apple_y)) { dots++; locateApple(); } } ``` `checkApple()`方法檢查蛇是否擊中了蘋果對象。 如果是這樣,我們添加另一個蛇形關節并調用`locateApple()`方法,該方法將隨機放置一個新的`Apple`對象。 在`move()`方法中,我們有游戲的關鍵算法。 要了解它,請看一下蛇是如何運動的。 您控制蛇的頭。 您可以使用光標鍵更改其方向。 其余關節在鏈上向上移動一個位置。 第二關節移動到第一個關節的位置,第三關節移動到第二個關節的位置,依此類推。 ```java for (int z = dots; z > 0; z--) { x[z] = x[(z - 1)]; y[z] = y[(z - 1)]; } ``` 該代碼將關節向上移動。 ```java if (left) { x[0] -= DOT_SIZE; } ``` 將頭向左移動。 在`checkCollision()`方法中,我們確定蛇是否擊中了自己或撞墻之一。 ```java for (int z = dots; z > 0; z--) { if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { inGame = false; } } ``` 如果蛇用頭撞到關節之一,我們就結束游戲。 ```java if (y[0] > HEIGHT) { inGame = false; } ``` 如果蛇擊中了棋盤的底部,則游戲結束。 `locateApple()`方法在板上隨機放置一個蘋果。 ```java int r = (int) (Math.random() * RAND_POS); ``` 我們得到一個從 0 到`RAND_POS-1`的隨機數。 ```java apple_x = ((r * DOT_SIZE)); ... apple_y = ((r * DOT_SIZE)); ``` 這些行設置了`apple`對象的 x,y 坐標。 ```java if (inGame) { checkApple(); checkCollision(); move(); } else { timer.stop(); } ``` 每 140 毫秒,將調用`timerEvent()`方法。 如果我們參與了游戲,我們將調用三種構建游戲邏輯的方法。 否則,我們將停止計時器。 在`Board`類的`onKeyPressEvent()`方法中,我們確定按下的鍵。 ```java if (key == Qt.Key.Key_Left.value() && !right) { left = true; up = false; down = false; } ``` 如果單擊左光標鍵,則將`left`變量設置為`true`。 在`move()`方法中使用此變量來更改蛇對象的坐標。 還要注意,當蛇向右行駛時,我們不能立即向左轉。 `Nibbles.java` ```java package com.zetcode; import com.trolltech.qt.gui.QApplication; import com.trolltech.qt.gui.QMainWindow; /** * ZetCode QtJambi tutorial * * In this program, we create * a Nibbles game clone. * * @author jan bodnar * website zetcode.com * last modified April 2009 */ public class Nibbles extends QMainWindow { public Nibbles() { setWindowTitle("Nibbles"); resize(310, 310); setCentralWidget(new Board()); move(300, 300); show(); } public static void main(String[] args) { QApplication.initialize(args); new Nibbles(); QApplication.exec(); } } ``` 在這個類中,我們設置了貪食蛇游戲。 ![Nibbles](https://img.kancloud.cn/9f/ea/9fea697acadb191bb7b10ef50a653782_316x335.jpg) 圖:貪食蛇 這是使用 QtJambi 庫編程的貪食蛇電腦游戲。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看