<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/vbgtk/nibbles/](http://zetcode.com/gui/vbgtk/nibbles/) 在 Visual Basic GTK# 編程教程的這一部分中,我們將創建貪食蛇游戲克隆。 貪食蛇是較舊的經典視頻游戲。 它最初是在 70 年代后期創建的。 后來它被帶到 PC 上。 在這個游戲中,玩家控制蛇。 目的是盡可能多地吃蘋果。 蛇每次吃一個蘋果,它的身體就會長大。 蛇必須避開墻壁和自己的身體。 ## 開發 蛇的每個關節的大小為 10px。 蛇由光標鍵控制。 最初,蛇具有三個關節。 游戲立即開始。 游戲結束后,我們在窗口中心顯示`"Game Over"`消息。 `board.vb` ```vb Imports Gtk Imports Cairo NameSpace BoardSpace Public Class Board Inherits DrawingArea 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 = 30 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 Dim dot As ImageSurface Dim apple As ImageSurface Dim head As ImageSurface Public Sub New MyBase.New ModifyBg(StateType.Normal, New Gdk.Color(0, 0, 0)) 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 Try dot = New ImageSurface("dot.png") head = New ImageSurface("head.png") apple = New ImageSurface("apple.png") Catch Console.WriteLine("Images not found") Environment.Exit(1) End Try Me.LocateApple Dim timer As New GLib.TimeoutHandler(AddressOf Me.OnTimer) GLib.Timeout.Add(100, timer) AddHandler Me.ExposeEvent, AddressOf Me.OnExpose End Sub Protected Sub OnExpose(ByVal sender As Object, ByVal e As ExposeEventArgs) Dim cc As Cairo.Context = Gdk.CairoHelper.Create(sender.GdkWindow) If inGame Me.DrawObjects(cc) Else Me.GameOver(cc) End If Dim disposeTarget As IDisposable = CType(cc.Target, IDisposable) disposeTarget.Dispose Dim disposeContext As IDisposable = CType(cc, IDisposable) disposeContext.Dispose End Sub Private Sub DrawObjects(ByVal cc As Cairo.Context) cc.SetSourceSurface(apple, apple_x, apple_y) cc.Paint For z As Integer = 0 to dots - 1 If z = 0 cc.SetSourceSurface(head, x(z), y(z)) cc.Paint Else cc.SetSourceSurface(dot, x(z), y(z)) cc.Paint End If Next End Sub Private Sub GameOver(ByVal cc As Cairo.Context) Dim message As String = "Game Over" Dim x As Integer = Allocation.Width / 2 Dim y As Integer = Allocation.Height / 2 cc.SetSourceRGB(1, 1, 1) cc.SetFontSize(18) Dim extents As TextExtents = cc.TextExtents(message) cc.MoveTo(x - extents.Width/2, y) cc.ShowText(message) inGame = False 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 inGame = False End If If y(0) < 0 inGame = False End If If x(0) > 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 Function OnTimer As Boolean If inGame Me.CheckApple Me.CheckCollision Me.Move Me.QueueDraw Return True Else Return False End If End Function Public Sub OnKeyDown(ByVal e As Gdk.EventKey) Dim key As Integer = e.KeyValue If key = Gdk.Key.Left AndAlso Not right left = True up = False down = False End If If key = Gdk.Key.Right AndAlso Not left right = True up = False down = False End If If key = Gdk.Key.Up AndAlso Not down up = True right = False left = False End If If key = Gdk.Key.Down AndAlso Not up down = True right = False left = False End If End Sub End Class End Namespace ``` 首先,我們將定義一些在游戲中使用的全局變量。 `WIDTH`和`HEIGHT`常數確定電路板的大小。 `DOT_SIZE`是蘋果的大小和蛇的點。 `ALL_DOTS`常數定義了板上可能的最大點數。 `RAND_POS`常數用于計算蘋果的隨機位置。 `DELAY`常數確定游戲的速度。 ```vb Dim x(ALL_DOTS) As Integer Dim y(ALL_DOTS) As Integer ``` 這兩個數組存儲蛇的所有可能關節的 x,y 坐標。 `InitGame`方法初始化變量,加載圖像并啟動超時功能。 ```vb If inGame Me.DrawObjects(cc) Else Me.GameOver(cc) End If ``` 在`OnExpose`方法內部,我們檢查`inGame`變量。 如果為真,則繪制對象。 蘋果和蛇的關節。 否則,我們顯示`"Game Over"`文本。 ```vb Private Sub DrawObjects(ByVal cc As Cairo.Context) cc.SetSourceSurface(apple, apple_x, apple_y) cc.Paint For z As Integer = 0 to dots - 1 If z = 0 cc.SetSourceSurface(head, x(z), y(z)) cc.Paint Else cc.SetSourceSurface(dot, x(z), y(z)) cc.Paint End If Next End Sub ``` `DrawObjects`方法繪制蘋果和蛇的關節。 蛇的第一個關節是其頭部,用紅色圓圈表示。 ```vb Private Sub CheckApple If x(0) = apple_x And y(0) = apple_y dots += 1 Me.LocateApple End If End Sub ``` `CheckApple`方法檢查蛇是否擊中了蘋果對象。 如果是這樣,我們添加另一個蛇形關節并調用`LocateApple`方法,該方法將隨機放置一個新的 Apple 對象。 在`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 inGame = False End If ``` 如果蛇擊中了棋盤的底部,我們就結束了游戲。 `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 坐標。 ```vb If inGame Me.CheckApple Me.CheckCollision Me.Move Me.QueueDraw Return True Else Return False End If ``` 每 140 毫秒,將調用`OnTimer`方法。 如果我們參與了游戲,我們將調用三種構建游戲邏輯的方法。 否則,我們返回`False`,它將停止計時器事件。 在`Board`類的`OnKeyDown`方法中,我們確定按下的鍵。 ```vb If key = Gdk.Key.Left AndAlso Not right left = True up = False down = False End If ``` 如果單擊左光標鍵,則將`left`變量設置為`true`。 在`Move`方法中使用此變量來更改蛇對象的坐標。 還要注意,當蛇向右行駛時,我們不能立即向左轉。 `nibbles.vb` ```vb ' ZetCode Mono Visual Basic GTK# tutorial ' ' In this program, we create ' a Nibbles game clone ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports Gtk Public Class GtkVBApp Inherits Window Dim WIDTH As Integer = 250 Dim HEIGHT As Integer = 150 Dim board As BoardSpace.Board Public Sub New MyBase.New("Nibbles") board = New BoardSpace.Board Me.Add(board) AddHandler Me.DeleteEvent, AddressOf Me.OnDelete Me.Resize(310, 310) Me.Move(300, 300) Me.ShowAll End Sub Private Sub OnDelete(ByVal sender As Object, _ ByVal args As DeleteEventArgs) Application.Quit End Sub Protected Overrides Function OnKeyPressEvent(ByVal e As Gdk.EventKey) As Boolean board.OnKeyDown(e) Return True End Function Public Shared Sub Main Application.Init Dim app As New GtkVBApp Application.Run End Sub End Class ``` 在這個類中,我們設置了貪食蛇游戲。 ```vb Protected Overrides Function OnKeyPressEvent(ByVal e As Gdk.EventKey) As Boolean board.OnKeyDown(e) Return True End Function ``` 在這個類中,我們捕獲按鍵事件。 并將處理委托給板類的`OnKeyDown`方法。 ![Nibbles](https://img.kancloud.cn/cb/3c/cb3c17fd1171a987d4746b3c7bf2da1b_318x338.jpg) 圖:貪食蛇 以下命令編譯游戲。 ```vb vbnc -r:/usr/lib/mono/gtk-sharp-2.0/gtk-sharp.dll -r:/usr/lib/mono/gtk-sharp-2.0/gdk-sharp.dll -r:/usr/lib/mono/2.0/Mono.Cairo.dll -r:/usr/lib/mono/gtk-sharp-2.0/glib-sharp.dll nibbles.vb board.vb ``` 這是使用 GTK# 庫和 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>

                              哎呀哎呀视频在线观看