C#贪吃蛇(WPF版)

Posted XML火柴

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#贪吃蛇(WPF版)相关的知识,希望对你有一定的参考价值。

最近写了个贪吃蛇,虽然有很多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

以上是关于C#贪吃蛇(WPF版)的主要内容,如果未能解决你的问题,请参考以下文章

C# wpf 使用 polyline 做一个贪吃蛇游戏的小蛇移动吃食部分功能

贪吃蛇大作战单人版完整代码以及详细解释

利用c# 编写贪吃蛇游戏如何修改label的属性

基于C#开发的简易贪吃蛇

C#贪吃蛇(窗体版本)

c# 贪吃蛇源码