<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 功能強大 支持多語言、二開方便! 廣告
                # 貪食蛇 > 原文: [http://zetcode.com/gui/phpgtktutorial/nibbles/](http://zetcode.com/gui/phpgtktutorial/nibbles/) 在 PHP GTK 編程教程的這一部分中,我們將創建一個貪食蛇游戲克隆。 貪食蛇是較舊的經典視頻游戲。 它最初是在 70 年代后期創建的。 后來它被帶到 PC 上。 在這個游戲中,玩家控制蛇。 目的是盡可能多地吃蘋果。 蛇每次吃一個蘋果,它的身體就會長大。 蛇必須避開墻壁和自己的身體。 ## 開發 蛇的每個關節的大小為 10px。 蛇由光標鍵控制。 最初,蛇具有三個關節。 游戲立即開始。 游戲結束后,我們在窗口中心顯示`"Game Over"`消息。 該代碼分為兩個文件。 `board.php`和`nibbles.php`。 ```php <?php // board.php define("WIDTH", 300); define("HEIGHT", 270); define("DOT_SIZE", 10); define("ALL_DOTS", WIDTH * HEIGHT / (DOT_SIZE * DOT_SIZE)); define("RAND_POS", 26); class Board extends GtkDrawingArea { public function __construct() { parent::__construct(); $this->modify_bg(Gtk::STATE_NORMAL, new GdkColor(6400, 6400, 6440)); $this->connect('expose_event', array($this, 'on_expose')); $this->init_game(); } public function init_game() { $this->x = array_fill(0, ALL_DOTS, 0); $this->y = array_fill(0, ALL_DOTS, 0); $this->left = false; $this->right = true; $this->up = false; $this->down = false; $this->inGame = true; $this->dots = 3; for ($i=0; $i<=$this->dots; $i++) { $this->x[$i] = 50 - $i * 10; $this->y[$i] = 50; } try { $this->dot = CairoImageSurface::createFromPng("dot.png"); $this->head = CairoImageSurface::createFromPng("head.png"); $this->apple = CairoImageSurface::createFromPng("apple.png"); } catch( Exception $e) { echo $e->getMessage(); echo "cannot load images"; exit; } $this->locate_apple(); $this->set_can_focus(true); Gtk::timeout_add(100, array($this, 'on_timer')); } public function on_timer() { if ($this->inGame) { $this->check_apple(); $this->check_collision(); $this->move(); $this->queue_draw(); return true; } else { return false; } } public function on_expose() { $cr = $this->window->cairo_create(); if ($this->inGame) { $this->draw_objects($cr); } else { $this->game_over($cr); } } public function draw_objects($cr) { $cr->SetSourceRgb(0, 0, 0); $cr->paint(); $cr->setSourceSurface($this->apple, $this->apple_x, $this->apple_y); $cr->paint(); for ($z=0; $z<=$this->dots; $z++) { if ($z == 0) { $cr->setSourceSurface($this->head, $this->x[$z], $this->y[$z]); $cr->paint(); } else { $cr->setSourceSurface($this->dot, $this->x[$z], $this->y[$z]); $cr->paint(); } } } public function game_over($cr) { $c_x = $this->get_allocation()->width/2; $c_y = $this->get_allocation()->height/2; $cr->SetFontSize(15); $cr->SetSourceRgb(65535, 65535, 65535); $te = $cr->TextExtents("Game Over"); $cr->MoveTo($c_x - $te['width']/2, $c_y); $cr->ShowText("Game Over"); } public function check_apple() { if ($this->x[0] == $this->apple_x and $this->y[0] == $this->apple_y) { $this->dots = $this->dots + 1; $this->locate_apple(); } } public function move() { $z = $this->dots; while ($z > 0) { $this->x[$z] = $this->x[($z - 1)]; $this->y[$z] = $this->y[($z - 1)]; $z--; } if ($this->left) { $this->x[0] -= DOT_SIZE; } if ($this->right) { $this->x[0] += DOT_SIZE; } if ($this->up) { $this->y[0] -= DOT_SIZE; } if ($this->down) { $this->y[0] += DOT_SIZE; } } public function check_collision() { $z = $this->dots; while ($z > 0) { if ($z > 4 and $this->x[0] == $this->x[$z] and $this->y[0] == $this->y[$z]) { $this->inGame = false; } $z--; } if ($this->y[0] > HEIGHT - DOT_SIZE) { $this->inGame = false; } if ($this->y[0] < 0) { $this->inGame = false; } if ($this->x[0] > WIDTH - DOT_SIZE) { $this->inGame = false; } if ($this->x[0] < 0) { $this->inGame = false; } } public function locate_apple() { $r = rand(0, RAND_POS); $this->apple_x = $r * DOT_SIZE; $r = rand(0, RAND_POS); $this->apple_y = $r * DOT_SIZE; } public function on_key_down($event) { $key = $event->keyval; if ($key == Gdk::KEY_Left and !$this->right) { $this->left = true; $this->up = false; $this->down = false; } if ($key == Gdk::KEY_Right and !$this->left) { $this->right = true; $this->up = false; $this->down = false; } if ($key == Gdk::KEY_Up and !$this->down) { $this->up = true; $this->right = false; $this->left = false; } if ($key == Gdk::KEY_Down and !$this->up) { $this->down = true; $this->right = false; $this->left = false; } } } ?> ``` 這是 board.php 文件。 ```php define("WIDTH", 300); define("HEIGHT", 270); define("DOT_SIZE", 10); define("ALL_DOTS", WIDTH * HEIGHT / (DOT_SIZE * DOT_SIZE)); define("RAND_POS", 26); ``` `WIDTH`和`HEIGHT`常數確定電路板的大小。 `DOT_SIZE`是蘋果的大小和蛇的點。 `ALL_DOTS`常數定義了板上可能的最大點數。 `RAND_POS`常數用于計算蘋果的隨機位置。 ```php $this->x = array_fill(0, ALL_DOTS, 0); $this->y = array_fill(0, ALL_DOTS, 0); ``` 這兩個數組存儲蛇的所有可能關節的 x,y 坐標。 `init_game()`方法初始化變量,加載圖像并啟動超時功能。 ```php if ($this->inGame) { $this->check_apple(); $this->check_collision(); $this->move(); $this->queue_draw(); return true; } else { return false; } ``` 每 140 毫秒,將調用`on_timer()`方法。 如果我們參與了游戲,我們將調用三種構建游戲邏輯的方法。 `queue_draw()`方法強制重新繪制窗口小部件。 這將反映游戲板上的變化。 否則,我們返回`false`,它將停止計時器事件。 ```php $cr = $this->window->cairo_create(); if ($this->inGame) { $this->draw_objects($cr); } else { $this->game_over($cr); } ``` 在`on_expose()`方法內部,我們檢查`$this->inGame`變量。 如果為真,則繪制對象。 蘋果和蛇的關節。 否則,我們顯示`"Game Over"`文本。 ```php public function draw_objects($cr) { $cr->SetSourceRgb(0, 0, 0); $cr->paint(); $cr->setSourceSurface($this->apple, $this->apple_x, $this->apple_y); $cr->paint(); for ($z=0; $z<=$this->dots; $z++) { if ($z == 0) { $cr->setSourceSurface($this->head, $this->x[$z], $this->y[$z]); $cr->paint(); } else { $cr->setSourceSurface($this->dot, $this->x[$z], $this->y[$z]); $cr->paint(); } } } ``` `draw_objects()`方法繪制蘋果和蛇的關節。 蛇的第一個關節是其頭部,用紅色圓圈表示。 如果游戲結束,則調用`game_over()`方法。 此方法在窗口中心顯示`"Game Over"`。 ```php $c_x = $this->get_allocation()->width/2; $c_y = $this->get_allocation()->height/2; ``` 在這里,我們獲得窗口的中心點。 ```php $cr->SetFontSize(15); $cr->SetSourceRgb(65535, 65535, 65535); ``` 我們設置文本的字體大小和顏色。 背景為黑色,因此字體將為白色。 ```php $te = $cr->TextExtents("Game Over"); ``` 我們得到字符串的文本范圍。 為了將文本放置在窗口的中央,這是必需的。 ```php $cr->MoveTo($c_x - $te['width']/2, $c_y); $cr->ShowText("Game Over"); ``` 我們移到中心并顯示文本。 ```php public function check_apple() { if ($this->x[0] == $this->apple_x and $this->y[0] == $this->apple_y) { $this->dots = $this->dots + 1; $this->locate_apple(); } } ``` `check_apple()`方法檢查蛇是否擊中了蘋果對象。 如果是這樣,我們添加另一個蛇形關節并調用`locate_apple()`方法,該方法將隨機放置一個新的`Apple`對象。 在`move()`方法中,我們有游戲的關鍵算法。 要了解它,請看一下蛇是如何運動的。 您控制蛇的頭。 您可以使用光標鍵更改其方向。 其余關節在鏈上向上移動一個位置。 第二關節移動到第一個關節的位置,第三關節移動到第二個關節的位置,依此類推。 ```php while ($z > 0) { $this->x[$z] = $this->x[($z - 1)]; $this->y[$z] = $this->y[($z - 1)]; $z--; } ``` 該代碼將關節向上移動。 ```php if ($this->left) { $this->x[0] -= DOT_SIZE; } ``` 將頭向左移動。 在`check_collision()`方法中,我們確定蛇是否擊中了自己或撞墻之一。 ```php while ($z > 0) { if ($z > 4 and $this->x[0] == $this->x[$z] and $this->y[0] == $this->y[$z]) { $this->inGame = false; } $z--; } ``` 如果蛇用頭撞到關節之一,我們就結束游戲。 ```php if ($this->y[0] > HEIGHT - DOT_SIZE) { $this->inGame = false; } ``` 如果蛇擊中了棋盤的底部,我們就結束了游戲。 `locate_apple()`方法在板上隨機放置一個蘋果。 ```php $r = rand(0, RAND_POS); ``` 我們得到一個從 0 到`RAND_POS-1`的隨機數。 ```php $this->apple_x = $r * DOT_SIZE; ... $this->apple_y = $r * DOT_SIZE; ``` 這些行設置了`apple`對象的 x,y 坐標。 在`Board`類的`on_key_down()`方法中,我們確定按下的鍵。 ```php if ($key == Gdk::KEY_Left and !$this->right) { $this->left = true; $this->up = false; $this->down = false; } ``` 如果單擊左光標鍵,則將`$this->left`變量設置為 true。 在`move()`方法中使用此變量來更改蛇對象的坐標。 還要注意,當蛇向右行駛時,我們不能立即向左轉。 ```php <?php /* ZetCode PHP GTK tutorial In this program, we create a Nibbles game clone. author: Jan Bodnar website: www.zetcode.com last modified: September 2011 */ include 'board.php'; class Example extends GtkWindow { public function __construct() { parent::__construct(); $this->init_ui(); } private function init_ui() { $this->set_title('Nibbles'); $this->connect_simple('destroy', array('gtk', 'main_quit')); $this->board = new Board(); $this->board->connect('key-press-event', array($this, 'on_key_down')); $this->add($this->board); $this->set_default_size(300, 270); $this->set_position(GTK::WIN_POS_CENTER); $this->show_all(); } public function on_key_down($sender, $event) { $key = $event->keyval; $this->board->on_key_down($event); } } new Example(); Gtk::main(); ?> ``` 這是`nibbles.php`文件。 在此文件中,我們設置了貪食蛇游戲。 ```php public function on_key_down($sender, $event) { $key = $event->keyval; $this->board->on_key_down($event); } ``` 在這個類中,我們捕獲按鍵事件。 并將處理委托給板類的`on_key_down()`方法。 ![Nibbles](https://img.kancloud.cn/08/3e/083e193a9eabbeaae9cb9771db933033_308x298.jpg) 圖:貪食蛇 這是用 GTK 庫和 PHP 編程語言編程的貪食蛇電腦游戲。
                  <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>

                              哎呀哎呀视频在线观看