<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國際加速解決方案。 廣告
                # IronPython Mono Winforms 中的貪食蛇 > 原文: [http://zetcode.com/tutorials/ironpythontutorial/snake/](http://zetcode.com/tutorials/ironpythontutorial/snake/) 在 Mono IronPython Winforms 編程教程的這一部分中,我們將創建一個貪食蛇游戲克隆。 ## 貪食蛇游戲 貪食蛇是較舊的經典視頻游戲。 它最初是在 70 年代后期創建的。 后來它被帶到 PC 上。 在這個游戲中,玩家控制蛇。 目的是盡可能多地吃蘋果。 蛇每次吃一個蘋果,它的身體就會長大。 蛇必須避開墻壁和自己的身體。 該游戲有時稱為 Nibbles 。 ## 開發 蛇的每個關節的大小為 10px。 蛇由光標鍵控制。 最初,蛇具有三個關節。 游戲立即開始。 如果游戲結束,我們將在棋盤中間顯示`Game Over`消息。 `board.py` ```py import clr clr.AddReference("System.Drawing") clr.AddReference("System") from System.Windows.Forms import UserControl, Keys, Timer from System.Drawing import Size, Color, Bitmap, Brushes, RectangleF from System.Drawing import Font, StringAlignment, StringFormat, PointF from System import Random from System.ComponentModel import Container WIDTH = 300 HEIGHT = 300 DOT_SIZE = 10 ALL_DOTS = 900 RAND_POS = 27 x = [0] * ALL_DOTS y = [0] * ALL_DOTS class Board(UserControl): def __init__(self): self.Text = 'Snake' self.components = Container() self.BackColor = Color.Black self.DoubleBuffered = True self.ClientSize = Size(WIDTH, HEIGHT) self.left = False self.right = True self.up = False self.down = False self.inGame = True try: self.dot = Bitmap("dot.png") self.apple = Bitmap("apple.png") self.head = Bitmap("head.png") except Exception, e: print e.Message self.initGame() def OnTick(self, sender, event): if self.inGame: self.checkApple() self.checkCollision() self.move() self.Refresh() def initGame(self): self.dots = 3 for i in range(self.dots): x[i] = 50 - i * 10 y[i] = 50 self.locateApple() self.KeyUp += self.OnKeyUp self.timer = Timer(self.components) self.timer.Enabled = True self.timer.Interval = 100 self.timer.Tick += self.OnTick self.Paint += self.OnPaint def OnPaint(self, event): g = event.Graphics if (self.inGame): g.DrawImage(self.apple, self.apple_x, self.apple_y) for i in range(self.dots): if i == 0: g.DrawImage(self.head, x[i], y[i]) else: g.DrawImage(self.dot, x[i], y[i]) else: self.gameOver(g) def gameOver(self, g): msg = "Game Over" format = StringFormat() format.Alignment = StringAlignment.Center format.LineAlignment = StringAlignment.Center width = float(self.ClientSize.Width) height = float(self.ClientSize.Height) rectf = RectangleF(0.0, 0.0, width, height) g.DrawString(msg, self.Font, Brushes.White, rectf, format) self.timer.Stop() 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.TITLEBAR_HEIGHT: self.inGame = False if y[0] < 0: self.inGame = False if x[0] >= WIDTH - DOT_SIZE - self.BORDER_WIDTH: self.inGame = False if x[0] < 0: self.inGame = False def locateApple(self): rand = Random() r = rand.Next(RAND_POS) self.apple_x = r * DOT_SIZE r = rand.Next(RAND_POS) self.apple_y = r * DOT_SIZE def OnKeyUp(self, event): key = event.KeyCode if key == Keys.Left and not self.right: self.left = True self.up = False self.down = False if key == Keys.Right and not self.left: self.right = True self.up = False self.down = False if key == Keys.Up and not self.down: self.up = True self.right = False self.left = False if key == Keys.Down and not self.up: self.down = True self.right = False self.left = False ``` 首先,我們將定義游戲中使用的常量。 `WIDTH`和`HEIGHT`常數確定電路板的大小。 `DOT_SIZE`是蘋果的大小和蛇的點。 `ALL_DOTS`常數定義了板上可能的最大點數。 (`900 = 300 * 300 / 10 * 10`)`RAND_POS`常數用于計算蘋果的隨機位置。 `DELAY`常數確定游戲的速度。 ```py x = [0] * ALL_DOTS y = [0] * ALL_DOTS ``` 這兩個列表存儲蛇的所有可能關節的 x,y 坐標。 在`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.TITLEBAR_HEIGHT: self.inGame = False ``` 如果蛇擊中了棋盤的底部,則游戲結束。 下圖有助于了解蛇形物體與棋盤底部的碰撞。 ![Collision](https://img.kancloud.cn/70/96/7096a8593326685be86eb4fc2092d7ea_300x307.jpg) 圖:碰撞 `locateApple()`方法在表格上隨機定位一個蘋果。 ```py rand = Random() r = rand.Next(RAND_POS) ``` 我們得到一個從 0 到`RAND_POS-1`的隨機數。 ```py self.apple_x = r * DOT_SIZE ... self.apple_y = r * DOT_SIZE ``` 這些行設置了`apple`對象的 x,y 坐標。 在`OnKeyUp()`方法中,我們確定了鍵擊玩家擊鍵的時間。 ```py if key == Keys.Left and not self.right: self.left = True self.up = False self.down = False ``` 如果我們按左光標鍵,則將`self.left`變量設置為`True`。 在`move()`方法中使用此變量來更改蛇對象的坐標。 還要注意,當蛇向右行駛時,我們不能立即向左轉。 `snake.py` ```py #!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") from System.Windows.Forms import Application, Form, FormBorderStyle from board import Board class IForm(Form): def __init__(self): self.Text = 'Snake' self.FormBorderStyle = FormBorderStyle.FixedSingle borderWidth = (self.Width - self.ClientSize.Width) / 2 titleBarHeight = self.Height - self.ClientSize.Height - borderWidth board = Board() board.BORDER_WIDTH = borderWidth board.TITLEBAR_HEIGHT = titleBarHeight self.Controls.Add(board) self.CenterToScreen() Application.Run(IForm()) ``` 這是主要的類。 ```py borderWidth = (self.Width - self.ClientSize.Width) / 2 titleBarHeight = self.Height - self.ClientSize.Height - borderWidth ``` 在這里,我們獲得窗體控件的邊框寬度和標題欄高度。 這些值對于蛇與邊界的碰撞檢測是必需的。 ```py board.BORDER_WIDTH = borderWidth board.TITLEBAR_HEIGHT = titleBarHeight ``` 我們將它們提供給董事會。 ![Snake](https://img.kancloud.cn/fa/be/fabe454b692b90d01058c2c22e417cbd_302x303.jpg) 圖:貪食蛇 這是使用 Iron Win 編程語言的 Mono Winforms 庫編程的貪食蛇游戲。
                  <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>

                              哎呀哎呀视频在线观看