[Windows Mobile]撞牆蛇

  • 8819
  • 0
  • 2013-04-15

[Windows Mobile]撞牆蛇

 

1. 簡介

本來想寫個貪食蛇遊戲,在 Windows Mobile 中可以玩,不過因為要準備點部落聚會的簡報,結果沒時間寫,只好變成撞牆蛇了

 

2. 方法

在程式的功能上,就是當蛇撞到螢幕的邊邊時,遊戲結束,程式流程如下

(1) 取得螢幕解析度,才能到蛇什麼時候撞牆死掉,可參考先前的文章 [Windows Mobile]旋轉螢幕方向、取得螢幕解析度與工作區域大小

(2) 宣告全域變數用來記錄座標

(3) 點選 Menu 上的 Start 按鈕,使 Timer 動作,並且跟據使用者點選的方向更新座標,並判斷是否撞牆了

(4) 在表單 Paint 事件中,繪製蛇出來

 

程式碼

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SmartDeviceProject8
{
    public partial class Form1 : Form
    {
        const int snakeSize = 10;
        int w = Screen.PrimaryScreen.WorkingArea.Width / snakeSize;
        int h = Screen.PrimaryScreen.WorkingArea.Height / snakeSize;
        bool[,] point = new bool[Screen.PrimaryScreen.WorkingArea.Width / snakeSize, Screen.PrimaryScreen.WorkingArea.Height / snakeSize];
        string direction = "Right";  // 宣告全域變數 direction,放置 snake 的行進方向

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            point[0, 0] = true;  // 設定 snake 初始位置
        }

        private void miStart_Click(object sender, EventArgs e)
        {
            this.timer1.Interval = 100;  // 設定 timer1 的觸發時間為 0.1
            this.timer1.Enabled = true;  // 開啟 timer1
        }

        // 表單 Paint 重繪事件
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.Red, 2);  // 設定 Pen Color: Red,width: 2
            int x = 0; int y = 0;
            while (y < h)
            {
                bool skip = false;
                if (point[x, y] == true)
                {
                    g.DrawRectangle(pen, x * snakeSize, y * snakeSize, snakeSize, snakeSize);
                }
                if (x >= w-1)
                {
                    x = 0; y++; skip = true;
                }

                if (x < w-1 && skip == false)
                {
                    x++;
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                int x = 0; int y = 0;
                while (y < h)
                {
                    bool skip = false;
                    if (direction == "Right")  // 當方向為 Right 時
                    { if (point[x, y] == true) { point[x, y] = false; point[x + 1, y] = true; break; } }
                    if (direction == "Left")  // 當方向為 Left 時
                    { if (point[x, y] == true) { point[x, y] = false; point[x - 1, y] = true; break; } }
                    if (direction == "Down") // 當方向為 Down 時
                    { if (point[x, y] == true) { point[x, y] = false; point[x, y + 1] = true; break; } }
                    if (direction == "Up") // 當方向為 Up 時
                    { if (point[x, y] == true) { point[x, y] = false; point[x, y - 1] = true; break; } }
                    if (x >= w-1) { x = 0; y++; skip = true; }
                    if (x < w-1 && skip == false) { x++; }
                }
                Invalidate();  // 重新繪製畫面
            }
            catch
            {
                MessageBox.Show("Game Over");
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // 將使用者點選按鍵給direction
            direction = e.KeyData.ToString();
        }
    }
}

 

3. 執行結果