<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國際加速解決方案。 廣告
                # Mono Winforms 中的貪食蛇 > 原文: [http://zetcode.com/gui/csharpwinforms/snake/](http://zetcode.com/gui/csharpwinforms/snake/) 在 Mono Winforms 編程教程的這一部分中,我們將創建一個貪食蛇游戲克隆。 ## 貪食蛇游戲 貪食蛇是較舊的經典視頻游戲。 它最初是在 70 年代后期創建的。 后來它被帶到 PC 上。 在這個游戲中,玩家控制蛇。 目的是盡可能多地吃蘋果。 蛇每次吃一個蘋果,它的身體就會長大。 蛇必須避開墻壁和自己的身體。 該游戲有時稱為 Nibbles 。 ## 開發 蛇的每個關節的大小為 10px。 蛇由光標鍵控制。 最初,蛇具有三個關節。 通過按下光標鍵之一開始游戲。 如果游戲結束,我們將在棋盤中間顯示`"Game Over"`消息。 `Board.cs` ```cs using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; public class Board : UserControl { private const int WIDTH = 300; private const int HEIGHT = 300; private const int DOT_SIZE = 10; private const int ALL_DOTS = 900; private const int RAND_POS = 27; private int[] x = new int[ALL_DOTS]; private int[] y = new int[ALL_DOTS]; private int dots; private int apple_x; private int apple_y; private bool left = false; private bool right = true; private bool up = false; private bool down = false; private bool inGame = true; private Timer timer; private Bitmap dot; private Bitmap apple; private Bitmap head; private IContainer components; public int BORDER_WIDTH; public int TITLEBAR_HEIGHT; public Board() { components = new Container(); BackColor = Color.Black; DoubleBuffered = true; this.ClientSize = new Size(WIDTH, HEIGHT); try { dot = new Bitmap("dot.png"); apple = new Bitmap("apple.png"); head = new Bitmap("head.png"); } catch (Exception e) { Console.WriteLine(e.Message); Environment.Exit(1); } initGame(); } private void OnTick(object sender, EventArgs e) { if (inGame) { checkApple(); checkCollision(); move(); } this.Refresh(); } private void initGame() { dots = 3; for (int z = 0; z < dots; z++) { x[z] = 50 - z * 10; y[z] = 50; } locateApple(); KeyUp += new KeyEventHandler(OnKeyUp); timer = new Timer(this.components); timer.Enabled = true; timer.Interval = 100; timer.Tick += new System.EventHandler(this.OnTick); Paint += new PaintEventHandler(this.OnPaint); } private void OnPaint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; if (inGame) { g.DrawImage(apple, apple_x, apple_y); for (int z = 0; z < dots; z++) { if (z == 0) { g.DrawImage(head, x[z], y[z]); } else { g.DrawImage(dot, x[z], y[z]); } } } else { gameOver(g); } } private void gameOver(Graphics g) { String msg = "Game Over"; StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; g.DrawString(msg, Font, Brushes.White, ClientRectangle, format); timer.Stop(); } private void checkApple() { if ((x[0] == apple_x) && (y[0] == apple_y)) { dots++; locateApple(); } } private void move() { for (int z = dots; z > 0; z--) { x[z] = x[(z - 1)]; y[z] = y[(z - 1)]; } if (left) { x[0] -= DOT_SIZE; } if (right) { x[0] += DOT_SIZE; } if (up) { y[0] -= DOT_SIZE; } if (down) { y[0] += DOT_SIZE; } } private void checkCollision() { for (int z = dots; z > 0; z--) { if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { inGame = false; } } if (y[0] > HEIGHT - DOT_SIZE - TITLEBAR_HEIGHT - BORDER_WIDTH) { inGame = false; } if (y[0] < 0) { inGame = false; } if (x[0] > WIDTH - DOT_SIZE - 2 * BORDER_WIDTH) { inGame = false; } if (x[0] < 0) { inGame = false; } } private void locateApple() { Random rand = new Random(); int r = (int)(rand.Next(RAND_POS)); apple_x = ((r * DOT_SIZE)); r = (int)(rand.Next(RAND_POS)); apple_y = ((r * DOT_SIZE)); } private void OnKeyUp(object sender, KeyEventArgs e) { int key = (int) e.KeyCode; if ((key == (int) Keys.Left) && (!right)) { left = true; up = false; down = false; } if ((key == (int) Keys.Right) && (!left)) { right = true; up = false; down = false; } if ((key == (int) Keys.Up) && (!down)) { up = true; right = false; left = false; } if ((key == (int) Keys.Down) && (!up)) { down = true; right = false; left = false; } } } ``` 首先,我們將定義游戲中使用的常量。 `WIDTH`和`HEIGHT`常數確定電路板的大小。 `DOT_SIZE`是蘋果的大小和蛇的點。 `ALL_DOTS`常數定義了板上可能的最大點數。 (`900 = 300 * 300 / 10 * 10`)`RAND_POS`常數用于計算蘋果的隨機位置。 `DELAY`常數確定游戲的速度。 ```cs private int[] x = new int[ALL_DOTS]; private int[] y = new int[ALL_DOTS]; ``` 這兩個數組存儲蛇的所有關節的 x,y 坐標。 在`move()`方法中,我們有游戲的關鍵算法。 要了解它,請看一下蛇是如何運動的。 您控制蛇的頭。 您可以使用光標鍵更改其方向。 其余關節在鏈上向上移動一個位置。 第二關節移動到第一個關節的位置,第三關節移動到第二個關節的位置,依此類推。 ```cs for (int z = dots; z > 0; z--) { x[z] = x[(z - 1)]; y[z] = y[(z - 1)]; } ``` 該代碼將關節向上移動。 ```cs if (left) { x[0] -= DOT_SIZE; } ``` 將頭向左移動。 在`checkCollision()`方法中,我們確定蛇是否擊中了自己或撞墻之一。 ```cs for (int z = dots; z > 0; z--) { if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { inGame = false; } } ``` 如果蛇用頭撞到關節之一,我們就結束游戲。 ```cs if (y[0] > HEIGHT - DOT_SIZE - TITLEBAR_HEIGHT - BORDER_WIDTH) { inGame = false; } ``` 如果蛇擊中了棋盤的底部,我們就結束了游戲。 `Snake.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class Snake : Form { public Snake() { Text = "Snake"; DoubleBuffered = true; FormBorderStyle = FormBorderStyle.FixedSingle; int borderWidth = (this.Width - this.ClientSize.Width) / 2; int titleBarHeight = this.Height - this.ClientSize.Height - borderWidth; Board board = new Board(); board.BORDER_WIDTH = borderWidth; board.TITLEBAR_HEIGHT = titleBarHeight; Controls.Add(board); CenterToScreen(); } } class MApplication { public static void Main() { Application.Run(new Snake()); } } ``` 這是主要的類。 ![Snake](https://img.kancloud.cn/fa/be/fabe454b692b90d01058c2c22e417cbd_302x303.jpg) 圖:貪食蛇 這是使用 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>

                              哎呀哎呀视频在线观看