<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Jython Swing 中的貪食蛇 > 原文: [http://zetcode.com/gui/jythonswing/nibbles/](http://zetcode.com/gui/jythonswing/nibbles/) 在 Jython Swing 編程教程的這一部分中,我們將創建一個貪食蛇游戲克隆。 貪食蛇是較舊的經典視頻游戲。 它最初是在 70 年代后期創建的。 后來它被帶到 PC 上。 在這個游戲中,玩家控制蛇。 目的是盡可能多地吃蘋果。 蛇每次吃一個蘋果,它的身體就會長大。 蛇必須避開墻壁和自己的身體。 ## 開發 蛇的每個關節的大小為 10px。 蛇由光標鍵控制。 最初,蛇具有三個關節。 游戲立即開始。 游戲結束后,我們在窗口中心顯示`"Game Over"`消息。 ```py import random from java.awt import Color from java.awt import Font from java.awt import Toolkit from java.awt.event import ActionListener from java.awt.event import KeyEvent from java.awt.event import KeyListener from javax.swing import ImageIcon from javax.swing import JPanel from javax.swing import Timer WIDTH = 300 HEIGHT = 300 DOT_SIZE = 10 ALL_DOTS = WIDTH * HEIGHT / (DOT_SIZE * DOT_SIZE) RAND_POS = 29 DELAY = 140 x = [0] * ALL_DOTS y = [0] * ALL_DOTS class Board(JPanel, KeyListener, ActionListener): def __init__(self): super(Board, self).__init__() self.initUI() def initUI(self): self.setBackground(Color.black) iid = ImageIcon("dot.png") self.ball = iid.getImage() iia = ImageIcon("apple.png") self.apple = iia.getImage() iih = ImageIcon("head.png") self.head = iih.getImage() self.setFocusable(True) self.addKeyListener(self) self.initGame() def initGame(self): self.left = False self.right = True self.up = False self.down = False self.inGame = True self.dots = 3 for i in range(self.dots): x[i] = 50 - i * 10 y[i] = 50 self.locateApple() self.timer = Timer(DELAY, self) self.timer.start() def paint(self, g): # due to bug, cannot call super() JPanel.paint(self, g) if self.inGame: self.drawObjects(g) else: self.gameOver(g) def drawObjects(self, g): g.drawImage(self.apple, self.apple_x, self.apple_y, self) for z in range(self.dots): if (z == 0): g.drawImage(self.head, x[z], y[z], self) else: g.drawImage(self.ball, x[z], y[z], self) Toolkit.getDefaultToolkit().sync() g.dispose() def gameOver(self, g): msg = "Game Over" small = Font("Helvetica", Font.BOLD, 14) metr = self.getFontMetrics(small) g.setColor(Color.white) g.setFont(small) g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2) def checkApple(self): if x[0] == self.apple_x and y[0] == self.apple_y: self.dots = self.dots + 1 self.locateApple() def move(self): z = self.dots while z > 0: x[z] = x[(z - 1)] y[z] = y[(z - 1)] z = z - 1 if self.left: x[0] -= DOT_SIZE if self.right: x[0] += DOT_SIZE if self.up: y[0] -= DOT_SIZE if self.down: y[0] += DOT_SIZE def checkCollision(self): z = self.dots while z > 0: if z > 4 and x[0] == x[z] and y[0] == y[z]: self.inGame = False z = z - 1 if y[0] > HEIGHT - DOT_SIZE: self.inGame = False if y[0] < 0: self.inGame = False if x[0] > WIDTH - DOT_SIZE: self.inGame = False if x[0] < 0: self.inGame = False def locateApple(self): r = random.randint(0, RAND_POS) self.apple_x = r * DOT_SIZE r = random.randint(0, RAND_POS) self.apple_y = r * DOT_SIZE # public void actionPerformed(ActionEvent e) { def actionPerformed(self, e): if self.inGame: self.checkApple() self.checkCollision() self.move() else: self.timer.stop() self.repaint() def keyPressed(self, e): key = e.getKeyCode() if key == KeyEvent.VK_LEFT and not self.right: self.left = True self.up = False self.down = False if key == KeyEvent.VK_RIGHT and not self.left: self.right = True self.up = False self.down = False if key == KeyEvent.VK_UP and not self.down: self.up = True self.right = False self.left = False if key == KeyEvent.VK_DOWN and not self.up: self.down = True self.right = False self.left = False ``` 首先,我們將定義一些在游戲中使用的常量。 `WIDTH`和`HEIGHT`常數確定電路板的大小。 `DOT_SIZE`是蘋果的大小和蛇的點。 `ALL_DOTS`常數定義了板上可能的最大點數。 `RAND_POS`常數用于計算蘋果的隨機位置。 `DELAY`常數確定游戲的速度。 ```py x = [0] * ALL_DOTS y = [0] * ALL_DOTS ``` 這兩個數組存儲蛇的所有可能關節的 x,y 坐標。 `initGame()`方法初始化變量,加載圖像并啟動超時功能。 ```py def paint(self, g): JPanel.paint(self, g) if self.inGame: self.drawObjects(g) else: self.gameOver(g) ``` 在`paint()`方法內部,我們檢查`inGame`變量。 如果為真,則繪制對象。 蘋果和蛇的關節。 否則,我們顯示`"Game Over"`文本。 ```py def drawObjects(self, g): g.drawImage(self.apple, self.apple_x, self.apple_y, self) for z in range(self.dots): if (z == 0): g.drawImage(self.head, x[z], y[z], self) else: g.drawImage(self.ball, x[z], y[z], self) Toolkit.getDefaultToolkit().sync() g.dispose() ``` `drawObjects()`方法繪制蘋果和蛇的關節。 蛇的第一個關節是其頭部,用紅色圓圈表示。 `Toolkit.getDefaultToolkit().sync()`方法可確保顯示為最新。 這對于動畫很有用。 ```py def checkApple(self): if x[0] == self.apple_x and y[0] == self.apple_y: self.dots = self.dots + 1 self.locateApple() ``` `checkApple()`方法檢查蛇是否擊中了蘋果對象。 如果是這樣,我們添加另一個蛇形關節并調用`locateApple()`方法,該方法將隨機放置一個新的`Apple`對象。 在`move()`方法中,我們有游戲的關鍵算法。 要了解它,請看一下蛇是如何運動的。 您控制蛇的頭。 您可以使用光標鍵更改其方向。 其余關節在鏈上向上移動一個位置。 第二關節移動到第一個關節的位置,第三關節移動到第二個關節的位置,依此類推。 ```py while z > 0: x[z] = x[(z - 1)] y[z] = y[(z - 1)] z = z - 1 ``` 該代碼將關節向上移動。 ```py if self.left: x[0] -= DOT_SIZE ``` 將頭向左移動。 在`checkCollision()`方法中,我們確定蛇是否擊中了自己或撞墻之一。 ```py while z > 0: if z > 4 and x[0] == x[z] and y[0] == y[z]: self.inGame = False z = z - 1 ``` 如果蛇用頭撞到關節之一,我們就結束游戲。 ```py if y[0] > HEIGHT - DOT_SIZE: self.inGame = False ``` 如果蛇擊中了棋盤的底部,我們就結束了游戲。 `locateApple()`方法在板上隨機放置一個蘋果。 ```py r = random.randint(0, RAND_POS) ``` 我們得到一個從 0 到`RAND_POS-1`的隨機數。 ```py self.apple_x = r * DOT_SIZE ... self.apple_y = r * DOT_SIZE ``` 這些行設置了`apple`對象的 x,y 坐標。 ```py def actionPerformed(self, e): if self.inGame: self.checkApple() self.checkCollision() self.move() else: self.timer.stop() self.repaint() ``` 每隔`DELAY` ms,將調用`actionPerformed()`方法。 如果我們參與了游戲,我們將調用三種構建游戲邏輯的方法。 否則,我們將停止計時器。 在`Board`類的`keyPressed()`方法中,我們確定按下的鍵。 ```py if key == KeyEvent.VK_LEFT and not self.right: self.left = True self.up = False self.down = False ``` 如果單擊左光標鍵,則將`left`變量設置為`true`。 在`move()`方法中使用此變量來更改蛇對象的坐標。 還要注意,當蛇向右行駛時,我們不能立即向左轉。 ```py #!/usr/local/bin/jython # -*- coding: utf-8 -*- """ ZetCode Jython Swing tutorial This is a simple Nibbles game clone. author: Jan Bodnar website: zetcode.com last edited: December 2010 """ from java.awt import Dimension from javax.swing import JFrame from Board import Board class Nibbles(JFrame): def __init__(self): super(Nibbles, self).__init__() self.initUI() def initUI(self): self.board = Board() self.board.setPreferredSize(Dimension(300, 300)) self.add(self.board) self.setTitle("Nibbles") self.pack() self.setResizable(False) self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) self.setLocationRelativeTo(None) self.setVisible(True) if __name__ == '__main__': Nibbles() ``` 在這個類中,我們設置了貪食蛇游戲。 ![Nibbles](https://img.kancloud.cn/b3/97/b397a2e75af2c38ada76fa367d94a1d7_302x332.jpg) 圖:貪食蛇 這是使用 Swing 庫和 Jython 編程語言編寫的貪食蛇電腦游戲。
                  <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>

                              哎呀哎呀视频在线观看