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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Ruby GTK 中的貪食蛇 > 原文: [http://zetcode.com/gui/rubygtk/nibbles/](http://zetcode.com/gui/rubygtk/nibbles/) 在 Ruby GTK 編程教程的這一部分中,我們將創建一個貪食蛇游戲克隆。 貪食蛇是較舊的經典視頻游戲。 它最初是在 70 年代后期創建的。 后來它被帶到 PC 上。 在這個游戲中,玩家控制蛇。 目的是盡可能多地吃蘋果。 蛇每次吃一個蘋果,它的身體就會長大。 蛇必須避開墻壁和自己的身體。 ## 開發 蛇的每個關節的大小為 10px。 蛇由光標鍵控制。 最初,蛇具有三個關節。 游戲立即開始。 游戲結束后,我們在窗口中心顯示`"Game Over"`消息。 `board.rb` ```rb WIDTH = 300 HEIGHT = 270 DOT_SIZE = 10 ALL_DOTS = WIDTH * HEIGHT / (DOT_SIZE * DOT_SIZE) RAND_POS = 26 DELAY = 100 $x = [0] * ALL_DOTS $y = [0] * ALL_DOTS class Board < Gtk::DrawingArea def initialize super override_background_color :normal, Gdk::RGBA.new(0, 0, 0, 1) signal_connect "draw" do on_draw end init_game end def on_timer if @inGame check_apple check_collision move queue_draw return true else return false end end def init_game @left = false @right = true @up = false @down = false @inGame = true @dots = 3 for i in 0..@dots $x[i] = 50 - i * 10 $y[i] = 50 end begin @dot = Cairo::ImageSurface.from_png "dot.png" @head = Cairo::ImageSurface.from_png "head.png" @apple = Cairo::ImageSurface.from_png "apple.png" rescue Exception => e puts "cannot load images" exit end locate_apple GLib::Timeout.add(DELAY) { on_timer } end def on_draw cr = window.create_cairo_context if @inGame draw_objects cr else game_over cr end end def draw_objects cr cr.set_source_rgb 0, 0, 0 cr.paint cr.set_source @apple, @apple_x, @apple_y cr.paint for z in 0..@dots if z == 0 cr.set_source @head, $x[z], $y[z] cr.paint else cr.set_source @dot, $x[z], $y[z] cr.paint end end end def game_over cr w = allocation.width / 2 h = allocation.height / 2 cr.set_font_size 15 te = cr.text_extents "Game Over" cr.set_source_rgb 65535, 65535, 65535 cr.move_to w - te.width/2, h cr.show_text "Game Over" end def check_apple if $x[0] == @apple_x and $y[0] == @apple_y @dots = @dots + 1 locate_apple end end def move z = @dots while z > 0 $x[z] = $x[(z - 1)] $y[z] = $y[(z - 1)] z = z - 1 end if @left $x[0] -= DOT_SIZE end if @right $x[0] += DOT_SIZE end if @up $y[0] -= DOT_SIZE end if @down $y[0] += DOT_SIZE end end def check_collision z = @dots while z > 0 if z > 4 and $x[0] == $x[z] and $y[0] == $y[z] @inGame = false end z = z - 1 end if $y[0] > HEIGHT - DOT_SIZE @inGame = false end if $y[0] < 0 @inGame = false end if $x[0] > WIDTH - DOT_SIZE @inGame = false end if $x[0] < 0 @inGame = false end end def locate_apple r = rand RAND_POS @apple_x = r * DOT_SIZE r = rand RAND_POS @apple_y = r * DOT_SIZE end def on_key_down event key = event.keyval if key == Gdk::Keyval::GDK_KEY_Left and not @right @left = true @up = false @down = false end if key == Gdk::Keyval::GDK_KEY_Right and not @left @right = true @up = false @down = false end if key == Gdk::Keyval::GDK_KEY_Up and not @down @up = true @right = false @left = false end if key == Gdk::Keyval::GDK_KEY_Down and not @up @down = true @right = false @left = false end end end ``` 首先,我們將定義一些在游戲中使用的全局變量。 `WIDTH`和`HEIGHT`常數確定`Board`的大小。 `DOT_SIZE`是蘋果的大小和蛇的點。 `ALL_DOTS`常數定義`Board`上可能的最大點數。 `RAND_POS`常數用于計算蘋果的隨機位置。 `DELAY`常數確定游戲的速度。 ```rb $x = [0] * ALL_DOTS $y = [0] * ALL_DOTS ``` 這兩個數組存儲蛇的所有可能關節的 x,y 坐標。 `init_game`方法初始化游戲。 ```rb @left = false @right = true @up = false @down = false @inGame = true @dots = 3 ``` 我們初始化我們在游戲中使用的變量。 ```rb for i in 0..@dots $x[i] = 50 - i * 10 $y[i] = 50 end ``` 我們給蛇關節初始坐標。 它總是從同一位置開始。 ```rb begin @dot = Cairo::ImageSurface.from_png "dot.png" @head = Cairo::ImageSurface.from_png "head.png" @apple = Cairo::ImageSurface.from_png "apple.png" rescue Exception => e puts "cannot load images" exit end ``` 加載了必要的圖像。 ```rb locate_apple ``` 蘋果進入初始隨機位置。 ```rb GLib::Timeout.add(DELAY) { on_timer } ``` `GLib::Timeout.add`方法將`on_timer`方法設置為每`DELAY`毫秒調用一次。 ```rb if @inGame draw_objects cr else game_over cr end ``` 在`on_draw`方法內部,我們檢查`@inGame`變量。 如果是真的,我們繪制對象:蘋果和蛇關節。 否則,我們顯示`"Game Over"`文本。 ```rb def draw_objects cr cr.set_source_rgb 0, 0, 0 cr.paint cr.set_source @apple, @apple_x, @apple_y cr.paint for z in 0..@dots if z == 0 cr.set_source @head, $x[z], $y[z] cr.paint else cr.set_source @dot, $x[z], $y[z] cr.paint end end end ``` `draw_objects`方法繪制蘋果和蛇的關節。 蛇的第一個關節是其頭部,用紅色圓圈表示。 ```rb def check_apple if $x[0] == @apple_x and $y[0] == @apple_y @dots = @dots + 1 locate_apple end end ``` `check_apple`方法檢查蛇是否擊中了蘋果對象。 如果是這樣,我們添加另一個蛇形關節并調用`locate_apple`方法,該方法隨機放置一個新的`Apple`對象。 在`move`方法中,我們有游戲的關鍵算法。 要了解它,我們需要看看蛇是如何運動的。 我們控制蛇的頭。 我們可以使用光標鍵更改其方向。 其余關節在鏈上向上移動一個位置。 第二關節移動到第一個關節的位置,第三關節移動到第二個關節的位置,依此類推。 ```rb while z > 0 $x[z] = $x[(z - 1)] $y[z] = $y[(z - 1)] z = z - 1 end ``` 該代碼將關節向上移動。 ```rb if @left $x[0] -= DOT_SIZE end ``` 如果向左移動,則磁頭向左移動。 在`check_collision`方法中,我們確定蛇是否擊中了自己或撞墻之一。 ```rb while z > 0 if z > 4 and $x[0] == $x[z] and $y[0] == $y[z] @inGame = false end z = z - 1 end ``` 如果蛇用頭撞到關節之一,我們就結束游戲。 ```rb if $y[0] > HEIGHT - DOT_SIZE @inGame = false end ``` 如果蛇擊中了棋盤的底部,則游戲結束。 `locate_apple`方法在板上隨機放置一個蘋果。 ```rb r = rand RAND_POS ``` 我們得到一個從 0 到`RAND_POS-1`的隨機數。 ```rb @apple_x = r * DOT_SIZE ... @apple_y = r * DOT_SIZE ``` 這些行設置了`apple`對象的 x,y 坐標。 ```rb if @inGame check_apple check_collision move queue_draw return true else return false end ``` 每`DELAY` ms 會調用一次`on_timer`方法。 如果我們參與了游戲,我們將調用三種構建游戲邏輯的方法。 否則,我們返回`false`,它將停止計時器事件。 在`Board`類的`on_key_down`方法中,我們確定按下的鍵。 ```rb if key == Gdk::Keyval::GDK_KEY_Left and not @right @left = true @up = false @down = false end ``` 如果單擊左光標鍵,則將`left`變量設置為`true`。 在`move`方法中使用此變量來更改蛇對象的坐標。 還要注意,當蛇向右行駛時,我們不能立即向左轉。 `nibbles.rb` ```rb #!/usr/bin/ruby ''' ZetCode Ruby GTK tutorial This is a simple Nibbles game clone. Author: Jan Bodnar Website: www.zetcode.com Last modified: May 2014 ''' require 'gtk3' require './board' class RubyApp < Gtk::Window def initialize super set_title "Nibbles" signal_connect "destroy" do Gtk.main_quit end @board = Board.new signal_connect "key-press-event" do |w, e| on_key_down w, e end add @board set_default_size WIDTH, HEIGHT set_window_position :center show_all end def on_key_down widget, event key = event.keyval @board.on_key_down event end end Gtk.init window = RubyApp.new Gtk.main ``` 在這個類中,我們設置了貪食蛇游戲。 ```rb def on_key_down widget, event key = event.keyval @board.on_key_down event end ``` 我們捕獲按鍵事件,并將處理委托給電路板類的`on_key_down`方法。 ![Nibbles](https://img.kancloud.cn/87/e6/87e6038e705f06a348c71ec03854809f_308x298.jpg) 圖:貪食蛇 這是使用 GTK 庫和 Ruby 編程語言編程的貪食蛇電腦游戲。
                  <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>

                              哎呀哎呀视频在线观看