C#貪吃蛇(WPF版)

XML火柴發表於2016-04-11

最近寫了個貪吃蛇,雖然有很多BUG,但是還是拿出來讓大家看下!
額,我用的還是VS2010.
首先,新建專案——WPF應用程式
將 canvas設定為定義為lol
接下來是引用部分
新增
using System.Windows.Threading;
這個是類似於Form裡的Timer
接下來是程式碼

public partial class MainWindow : Window
   {
        double x = 800;
        double y = 600;
        string key_name = "start";          //鍵盤的敲擊狀態
        Border[] bdr = new Border[30];       //貪吃蛇的長度
        Random r = new Random();              //隨機變數
        DispatcherTimer dt = new DispatcherTimer();    //類似於Timer
        double a = 0, b = 0;       //兩個變數
        public MainWindow()
        {
            InitializeComponent();
            this.Top = 100; this.Left = 200;       //定義開始介面的座標
            this.Width = x; this.Height = y;          //定義開始介面的大小

            this.Background = Brushes.Black;             //背景為黑色

            dt.Interval = TimeSpan.FromMilliseconds(500);        //設定時間,0.5秒
            dt.Tick += new EventHandler(dt_Tick);                  //事件
            this.KeyDown += new KeyEventHandler(lol_KeyDown);        //鍵盤響應事件
            snack_start();             //函式,下文解釋
            snake_food(); //函式,下文解釋
            dt.Start(); //Timer開始
        }
        void dt_Tick(object sender, EventArgs e)
        {
            double x_1, y_1;              //變數記錄座標
            x_1 = Canvas.GetLeft(bdr[0]);
            y_1 = Canvas.GetTop(bdr[0]);
            if (key_name == "start")       //開始狀態
            {
                Canvas.SetLeft(bdr[0], x_1 + 20);         //蛇一直向右走
                snake_move(x_1, y_1);         //函式,下文解釋
            }
            if (key_name == "Right")   //向右狀態
            {
                Canvas.SetLeft(bdr[0], x_1 + 20);  //蛇一直向右走
                snake_move(x_1, y_1);
            }
            if (key_name == "Up") //向上狀態
            {
                Canvas.SetTop(bdr[0], y_1 - 20);  //蛇一直向右走
                snake_move(x_1, y_1);
            }
            if (key_name == "Down") //向下狀態
            {
                Canvas.SetTop(bdr[0], y_1 + 20);  //蛇一直向右走
                snake_move(x_1, y_1);
            }
            if (key_name == "Left"//向左狀態
            {
                Canvas.SetLeft(bdr[0], x_1 - 20);  //蛇一直向右走
                snake_move(x_1, y_1);
            }
            if(x_1>x) //穿牆設定,超出右屏
            {
                Canvas.SetLeft(bdr[0],0);
            }
            if(x_1<0) //穿牆設定,超出左屏
            {
                Canvas.SetLeft(bdr[0],x);
            }
            if(y_1>y) //穿牆設定,超出下界
            {
                Canvas.SetTop(bdr[0],0);
            }
            if(y_1<0) //穿牆設定,超出上界
            {
                Canvas.SetTop(bdr[0],y);
            }
            eat_time();    //函式,下文解釋
        }
        void lol_KeyDown(object sender, KeyEventArgs e)   //鍵盤敲擊事件
        {
            double x_1, y_1;    //定義變數,記錄座標
            x_1 = Canvas.GetLeft(bdr[0]);
            y_1 = Canvas.GetTop(bdr[0]);
            key_name = e.Key.ToString();    //獲取鍵盤狀態
                      if (key_name == "Right")  //向右狀態
            {
                Canvas.SetLeft(bdr[0], x_1 + 20); //蛇一直向右走
                snake_move(x_1, y_1);
            }
            if (key_name == "Up") //向上狀態
            {
                Canvas.SetTop(bdr[0], y_1 - 20); //蛇一直向右走
                snake_move(x_1, y_1);
            }
            if (key_name == "Down") //向下狀態
            {
                Canvas.SetTop(bdr[0], y_1 + 20); //蛇一直向右走
                snake_move(x_1, y_1);
            }
            if (key_name == "Left"//向左狀態
            {
                Canvas.SetLeft(bdr[0], x_1 - 20); //蛇一直向右走
                snake_move(x_1, y_1);
            }
            eat_time();   //函式,下文解釋
        }

        private void eat_time()
        {
//定義變數,設計中,蛇的每一塊大小,與蛇的食物,都是20*20的大小
            double x1 = 20, y1 = 20, x2 , y2 , a1 = 20, b1 = 20;
//遍厲
            foreach (Border bo in lol.Children)
            {
                if (bo.Tag.ToString() == "food".ToString())     //如果Tag是food
                {
//獲取座標
                    x2 = Canvas.GetLeft(bo);
                    y2 = Canvas.GetTop(bo);
                }
                if (bo.Tag.ToString() == "0".ToString())  //如果Tag是0(蛇頭)
                {
//獲取座標
                    x1 = Canvas.GetLeft(bo);  //左
                    y1 = Canvas.GetTop(bo);  //上
                    a1 = Canvas.GetBottom(bo);   //底
                    b1 = Canvas.GetRight(bo);  //右
                }
            }
//蛇頭與食物重合(從各個方向重合)
            if ((x1 == x2 && y1 == y2) || (x1 == y2 && y1 == x2) || (a1 == x2 && b1 == y2) || (a1 == y2 && b1 == x2))
            {
                snake_eat();   //函式,下文解釋
                foreach (Border bo in lol.Children)   //遍厲
                {
                    if (bo.Tag.ToString() == "food".ToString())              //  將食物座標改變
                    {
                        Canvas.SetTop(bo, r.Next(1, 30) * 20);
                        Canvas.SetLeft(bo, r.Next(1, 30) * 20);
                    }
                }
            }
        }
        void snack_start()      //剛開始,蛇的製作
        {
            for (int i = 0; i < 5; i++)         //初始長度為5
            {
                Border bor = new Border();
                bor.Width = 20;
                bor.Height = 20;
                Canvas.SetLeft(bor, 400-i*20);    //座標設計,保證在同一水平出現
                Canvas.SetTop(bor, 400);
                bor.Background = Brushes.Red;    //紅色
                bor.CornerRadius = new CornerRadius(6);
                bor.Tag = i;
                bdr[i] = bor;
                lol.Children.Add(bor);
            }
        }
        void snake_move(double x_1, double y_1)        //蛇的移動
        {
//設計思路:保證,蛇的每一節都遵從它的前一節運動的軌跡,蛇頭遵從鍵盤指令
            double bor_hei, bor_wid;
            bor_wid = bdr[0].Width;
            bor_hei = bdr[0].Height;
            double xx = 0;
            double yy = 0;
            for (int i = 1; bdr[i] != null; i++)
            {
                if (i >= 3)
                {
                    xx = a;
                    yy = b;
                }
                if (i == 1)
                {
                    xx = Canvas.GetLeft(bdr[i]);
                    yy = Canvas.GetTop(bdr[i]);
                    Canvas.SetLeft(bdr[1], x_1);
                    Canvas.SetTop(bdr[1], y_1);
                }
                else
                {
                    a = Canvas.GetLeft(bdr[i]);
                    b = Canvas.GetTop(bdr[i]);
                    Canvas.SetLeft(bdr[i], xx);
                    Canvas.SetTop(bdr[i], yy);
                }
            }
        }
        void snake_food()        //食物設計
        {
            double xx = Canvas.GetLeft(bdr[0]);
            double yy = Canvas.GetTop(bdr[0]);
            Border bd = new Border();
            bd.Width = 20;
            bd.Height = 20;
//座標隨機出現
            Canvas.SetTop(bd, r.Next(1, 30) * 20);
            Canvas.SetLeft(bd, r.Next(1, 30) * 20);
            bd.Tag = "food";
            bd.Background = Brushes.Yellow;    //食物顏色為黃色
            lol.Children.Add(bd);
        }
        void snake_eat()  // 蛇頭與食物相遇
        {
//新建一個Border,當蛇頭與食物相遇,將這個Border新增到蛇尾最後運動的一個地點,再將這個Border新增到//蛇的陣列中去
            int i = 0;
//接受此時蛇長
            for (; bdr[i] != null; i++) 
                ;
            Border bd = new Border();
            bd.CornerRadius = new CornerRadius(6);
            bd.Width = 20;
            bd.Height = 20;
            bd.Tag = i;
            bd.Background = Brushes.Red;
            bdr[i] = bd;
            Canvas.SetLeft(bdr[i], a);
            Canvas.SetTop(bdr[i], b);
            lol.Children.Add(bdr[i]);
        }
    }

原工程下載地址:http://pan.baidu.com/s/1c15vQ5E

歡迎關注我的微信個人訂閱號
這裡寫圖片描述
每天多學一點0.0

相關文章