<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/vbwinforms/nibbles/](http://zetcode.com/gui/vbwinforms/nibbles/) 在 Mono Winforms 編程教程的這一部分中,我們將創建貪食蛇游戲克隆。 ## 貪食蛇游戲 貪食蛇是較舊的經典視頻游戲。 它最初是在 70 年代后期創建的。 后來它被帶到 PC 上。 在這個游戲中,玩家控制蠕蟲。 目的是盡可能多地吃蘋果。 蠕蟲每次吃一個蘋果,它的身體就會長大。 它必須避開墻壁和自己的身體。 ## 開發 蠕蟲每個關節的大小為 10 像素。 蠕蟲由光標鍵控制。 最初,蠕蟲具有三個關節。 通過按下光標鍵之一開始游戲。 如果游戲結束,我們將在棋盤中間顯示`Game Over`消息。 `board.vb` ```vb Imports System Imports System.Collections Imports System.ComponentModel Imports System.Drawing Imports System.Data Imports System.Windows.Forms NameSpace BoardSpace public class Board Inherits UserControl Const WIDTH As Integer = 300 Const HEIGHT As Integer = 300 Const DOT_SIZE As Integer = 10 Const ALL_DOTS As Integer = 900 Const RAND_POS As Integer = 27 Const DELAY As Integer = 140 Dim x(ALL_DOTS) As Integer Dim y(ALL_DOTS) As Integer Dim dots As Integer Dim apple_x As Integer Dim apple_y As Integer Dim left As Boolean = False Dim right As Boolean = True Dim up As Boolean = False Dim down As Boolean = False Dim inGame As Boolean = True Private Dim timer As Timer Private Dim dot As Bitmap Private Dim apple As Bitmap Private Dim head As Bitmap Private Dim components As IContainer Public Dim BORDER_WIDTH As Integer Public Dim TITLEBAR_HEIGHT As Integer Public Sub New components = New Container Me.BackColor = Color.Black Me.DoubleBuffered = True Me.ClientSize = New Size(WIDTH, HEIGHT) Try dot = New Bitmap("dot.png") apple = New Bitmap("apple.png") head = New Bitmap("head.png") Catch e As Exception Console.WriteLine(e.Message) Environment.Exit(1) End Try Me.InitGame End Sub Private Sub InitGame dots = 3 For z As Integer = 0 To dots-1 x(z) = 50 - z*10 y(z) = 50 Next Me.LocateApple AddHandler Me.KeyUp, AddressOf Me.OnKeyUp timer = New Timer(Me.components) timer.Enabled = True timer.Interval = DELAY AddHandler timer.Tick, AddressOf Me.OnTick AddHandler Me.Paint, AddressOf Me.OnPaint End Sub Private Sub OnPaint(ByVal sender As Object, _ ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics If inGame Me.DrawObjects(g) Else Me.GameOver(g) End If End Sub Private Sub DrawObjects(ByVal g As Graphics) g.DrawImage(apple, apple_x, apple_y) For z As Integer = 0 To dots-1 If z = 0 g.DrawImage(head, x(z), y(z)) Else g.DrawImage(dot, x(z), y(z)) End If Next End Sub Private Sub GameOver(ByVal g As Graphics) Dim msg As String = "Game Over" Dim rectF As RectangleF = RectangleF.op_Implicit(Me.ClientRectangle) Dim format As New StringFormat format.Alignment = StringAlignment.Center format.LineAlignment = StringAlignment.Center g.DrawString(msg, Font, Brushes.White, rectF , format) timer.Stop End Sub Private Sub CheckApple If x(0) = apple_x And y(0) = apple_y dots += 1 Me.LocateApple End If End Sub Private Sub Move For z As Integer = dots To 1 Step -1 x(z) = x(z - 1) y(z) = y(z - 1) Next If left x(0) -= DOT_SIZE End If If right x(0) += DOT_SIZE End If If up y(0) -= DOT_SIZE End If If down y(0) += DOT_SIZE End If End Sub Private Sub CheckCollision For z As Integer = dots To 1 Step -1 If z > 4 And x(0) = x(z) And y(0) = y(z) inGame = False End If Next If y(0) >= HEIGHT - DOT_SIZE - TITLEBAR_HEIGHT inGame = False End If If y(0) < 0 inGame = False End If If x(0) >= WIDTH - DOT_SIZE - BORDER_WIDTH: inGame = False End If If x(0) < 0 inGame = False End If End Sub Private Sub LocateApple Dim rand As New Random Dim r As Integer = rand.Next(RAND_POS) apple_x = r * DOT_SIZE r = rand.Next(RAND_POS) apple_y = r * DOT_SIZE End Sub Private Sub OnTick(ByVal sender As Object, ByVal e As EventArgs) If inGame Me.CheckApple Me.CheckCollision Me.Move End If Me.Refresh End Sub Private Sub OnKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Dim key As Integer = e.KeyCode If key = Keys.Left And Not right left = True up = False down = False End If If key = Keys.Right And Not left right = True up = False down = False End If If key = Keys.Up And Not down up = True right = False left = False End if If key = Keys.Down And Not up down = True right = False left = False End If End Sub End Class End Namespace ``` 首先,我們將定義游戲中使用的常量。 `WIDTH`和`HEIGHT`常數確定電路板的大小。 `DOT_SIZE`是蘋果的大小和蠕蟲的點。 `ALL_DOTS`常數定義了板上可能的最大點數。 (`900 = 300 * 300 / 10 * 10`)`RAND_POS`常數用于計算蘋果的隨機位置。 `DELAY`常數確定游戲的速度。 ```vb Dim x(ALL_DOTS) As Integer Dim y(ALL_DOTS) As Integer ``` 這兩個數組存儲蠕蟲的所有關節的 x,y 坐標。 在`Move`方法中,我們有游戲的關鍵算法。 要了解它,請查看蠕蟲如何移動。 您控制蠕蟲的頭部。 您可以使用光標鍵更改其方向。 其余關節在鏈上向上移動一個位置。 第二關節移動到第一個關節的位置,第三關節移動到第二個關節的位置,依此類推。 ```vb For z As Integer = dots To 1 Step -1 x(z) = x(z - 1) y(z) = y(z - 1) Next ``` 該代碼將關節向上移動。 ```vb If left x(0) -= DOT_SIZE End If ``` 將頭向左移動。 在`CheckCollision`方法中,我們確定蠕蟲是否已經擊中自己或撞墻之一。 ```vb For z As Integer = dots To 1 Step -1 If z > 4 And x(0) = x(z) And y(0) = y(z) inGame = False End If Next ``` 如果蠕蟲用頭撞到關節之一,就結束游戲。 ```vb If y(0) >= HEIGHT - DOT_SIZE - TITLEBAR_HEIGHT inGame = False End If ``` 如果蠕蟲到達了棋盤的底部,我們就結束了游戲。 下圖有助于了解蠕蟲對象與板子底部的碰撞。 ![Collision](https://img.kancloud.cn/70/96/7096a8593326685be86eb4fc2092d7ea_300x307.jpg) 圖:碰撞 `locateApple`方法在表格上隨機定位一個蘋果。 ```vb Dim rand As New Random Dim r As Integer = rand.Next(RAND_POS) ``` 我們得到一個從 0 到`RAND_POS-1`的隨機數。 ```vb apple_x = r * DOT_SIZE ... apple_y = r * DOT_SIZE ``` 這些行設置了`apple`對象的 x,y 坐標。 在`OnKeyUp`方法中,我們確定了鍵擊玩家擊鍵的時間。 ```vb If key = Keys.Left And Not right left = True up = False down = False End If ``` 如果我們按左光標鍵,則將`left`變量設置為`True`。 在`Move`方法中使用此變量來更改蠕蟲對象的坐標。 還要注意,當蠕蟲向右移動時,我們不能立即向左轉。 `nibbles.vb` ```vb ' ZetCode Mono Visual Basic Winforms tutorial ' ' In this program, we create ' a Nibbles game clone ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports System Imports System.Drawing Imports System.Windows.Forms Public Class WinVBApp Inherits Form Public Sub New Me.Text = "Nibbles" Me.FormBorderStyle = FormBorderStyle.FixedSingle Dim borderWidth As Integer = (Me.Width - Me.ClientSize.Width) / 2 Dim titleBarHeight As Integer = Me.Height - Me.ClientSize.Height - borderWidth Dim board As New BoardSpace.Board board.BORDER_WIDTH = borderWidth board.TITLEBAR_HEIGHT = titleBarHeight Me.Controls.Add(board) Me.CenterToScreen End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class ``` 這是主要的類。 ![Nibbles](https://img.kancloud.cn/3c/14/3c14b7c06627e7cfb7a6ac238b4c78e4_302x303.jpg) 圖:貪食蛇 這是使用 Mono Winforms 庫和 Visual Basic 語言編寫的貪食蛇游戲。
                  <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>

                              哎呀哎呀视频在线观看