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

贪吃蛇总结:

1. 创建WPF应用程序

 

2. 添加Canvas标签。Name=back;

 

3. 添加loaded事件

 

4. 在loaded事件中创建蛇(设置蛇的位置,大小,颜色)

  this.Left = 0;

            this.Top = 0;

            this.Width  = 800;

            this.Height = 600;

            for (int i = 0; i < 5; i++)

            {

                bd = new Border();

                bd.Width = bd.Height = 10;

                bd.CornerRadius = new CornerRadius(5);

                if (i == 0)

                    bd.Background = Brushes.Red;

                else

                    bd.Background = Brushes.Black;

                Canvas.SetTop(bd,200+i*bd.Height);

                Canvas.SetLeft(bd,200);

                back.Children.Add(bd);

                snake.Add(bd);

 

5. 添加一个timer,(问题:添加timer和winform程序中不一样

Winform 程序中直接添加。

WPF应用程序中不能直接添加,需要引入命名空间:

Using System.Thearing

DispatcherTimer tm1 = new DispatcherTimer();

            tm1.Interval =TimeSpan.FromMilliseconds(200);

            tm1.Tick += Tm1_Tick;

            tm1.Start();

            this.KeyDown += MainWindow_KeyDown;

6. 添加键盘事件,读取键盘敲下的字符

 if (e.Key==Key.Up&&str!="d" )

            {

                str = "up";

            }

            if (e.Key==Key.Down&&str!="up")

            {

                str = "d";

         if(e.key-key.left&&str!="r")

            {

                str = "l";

            }

            if (e.Key == Key.Right&&str!="l")

            {

                str = "r";

            }

 

7.在timer事件中实现蛇的移动(问题:在实现向左移动时出现错误

解决:添加一个集合,来存储蛇的位置信息,与此同时添加一个border储存位置信息border)

 

        //创建蛇的位置集合

        List<Border> snakeback =new List<Border>();

 //创建蛇的位置border

                Border bdback = new Border();

                bdback.Height = bdback.Width = 10;

                Canvas.SetTop(bdback, 200 + i * bd.Height);

                Canvas.SetLeft(bdback, 200);

                snakeback.Add(bdback);

if (str == "up")

            {

                Canvas.SetTop(snake[0],Canvas.GetTop(snake[0]) - snake[0].Height);

                run();

            }

            if (str=="d")

            {

                Canvas.SetTop(snake[0],Canvas.GetTop(snake[0])+ snake[0].Height);

                run();

            }

            if (str == "l")

            {

                Canvas.SetLeft(snake[0],Canvas.GetLeft(snake[0]) - snake[0].Height);

                run();

            }

            if (str == "r")

            {

                Canvas.SetLeft(snake[0],Canvas.GetLeft(snake[0]) + snake[0].Height);

                run();

            }

 

private void run()

        {

            for (int i = 1; i < snake.Count; i++)

            {

                Canvas.SetTop(snake[i],Canvas.GetTop(snakeback[i - 1]));

                Canvas.SetLeft(snake[i],Canvas.GetLeft(snakeback[i - 1]));

            }

            for (int i = 0; i < snake.Count; i++)

            {

                Canvas.SetTop(snakeback[i],Canvas.GetTop(snake[i]));

                Canvas.SetLeft(snakeback[i],Canvas.GetLeft(snake[i]));

            }

        }

 

8. 判断与食物相撞时,食物改变位置

 if (Canvas.GetTop(snake[0])==Canvas.GetTop(food)&&Canvas.GetLeft(snake[0])==Canvas.GetLeft(food))

            {

                Canvas.SetTop(food, ra.Next(60) * 10);

                Canvas.SetLeft(food, ra.Next(80) * 10);

}

9. 在与食物相撞时添加一节蛇尾

10. 添加一个border来作为蛇尾,添加到snake集合中

11. 添加一个border存储所增加的蛇尾的位置信息

if (Canvas.GetTop(snake[0])==Canvas.GetTop(food)&&Canvas.GetLeft(snake[0])==Canvas.GetLeft(food))

            {

                Canvas.SetTop(food, ra.Next(60) * 10);

                Canvas.SetLeft(food, ra.Next(80) * 10);

                //定义一个增加的蛇尾的border

                Border bdfood = new Border();

                bdfood.Height = bdfood.Width = 10;

                bdfood.CornerRadius = new CornerRadius(5);

                bdfood.Background = Brushes.Azure;

                Canvas.SetTop(bdfood,Canvas.GetTop(snake[snake.Count-1]));

                Canvas.SetLeft(bdfood,Canvas.GetLeft(snake[snake.Count - 1]));

                back.Children.Add(bdfood);

                snake.Add(bdfood);

 

                //定义一个border存储增加的的集合

                Border br = new Border();

                br.Height = br.Width = 10;

                Canvas.SetTop(br, Canvas.GetTop(snakeback[snakeback.Count - 1]));

                Canvas.SetLeft(br, Canvas.GetLeft(snakeback[snakeback.Count - 1]));

                snakeback.Add(br);

            }

 

12.判断蛇与面板四周相撞或者蛇撞到自己,游戏结束。

 for (int i = 1; i < snake.Count; i++)

            {

                if (Canvas.GetTop(snake[0]) ==Canvas.GetTop(snake[i]) && Canvas.GetLeft(snake[0]) == Canvas.GetLeft(snake[i]))

                {

                    tm1.Stop();

                    MessageBox.Show("GAME  OVER!");

                }

            }

            if (Canvas.GetTop(snake[0]) == 0 ||Canvas.GetTop(snake[0]) == 560 || Canvas.GetLeft(snake[0]) == 0 || Canvas.GetLeft(snake[0]) == 790)

            {

                tm1.Stop();

                MessageBox.Show("GAME  OVER!");

            }

完整版代码:

 

[csharp] view plain copy
 
 print?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Windows;  
  4. using System.Windows.Controls;  
  5. using System.Windows.Input;  
  6. using System.Windows.Media;  
  7. using System.Windows.Threading;  
  8.   
  9. namespace 贪吃蛇1._0  
  10. {  
  11.     /// <summary>  
  12.     /// MainWindow.xaml 的交互逻辑  
  13.     /// </summary>  
  14.     public partial class MainWindow : Window  
  15.     {  
  16.         public MainWindow()  
  17.         {  
  18.             InitializeComponent();  
  19.             this.Background = Brushes.Pink;  
  20.         }  
  21.         string str = "";  
  22.         Border bd;  
  23.         DispatcherTimer tm1;  
  24.         Border food;  
  25.         List<Border> snake = new List<Border>();  
  26.         //创建蛇的位置集合  
  27.         List<Border> snakeback = new List<Border>();  
  28.         List<Border> foodback = new List<Border>();  
  29.         Random ra = new Random();  
  30.         private void Window_Loaded(object sender, RoutedEventArgs e)  
  31.         {  
  32.             this.Left = 0;  
  33.             this.Top = 0;  
  34.             this.Width = 800;  
  35.             this.Height = 600;  
  36.             for (int i = 0; i < 5; i++)  
  37.             {  
  38.                 bd = new Border();  
  39.                 bd.Width = bd.Height = 10;  
  40.                 bd.CornerRadius = new CornerRadius(5);  
  41.                 if (i == 0)  
  42.                     bd.Background = Brushes.Red;  
  43.                 else  
  44.                     bd.Background = Brushes.Black;  
  45.                 Canvas.SetTop(bd, 200 + i * bd.Height);  
  46.                 Canvas.SetLeft(bd, 200);  
  47.                 back.Children.Add(bd);  
  48.                 snake.Add(bd);  
  49.   
  50.                 //创建蛇的位置border  
  51.                 Border bdback = new Border();  
  52.                 bdback.Height = bdback.Width = 10;  
  53.                 Canvas.SetTop(bdback, 200 + i * bd.Height);  
  54.                 Canvas.SetLeft(bdback, 200);  
  55.                 snakeback.Add(bdback);  
  56.             }  
  57.   
  58.             tm1 = new DispatcherTimer();  
  59.             tm1.Interval = TimeSpan.FromMilliseconds(200);  
  60.             tm1.Tick += Tm1_Tick;  
  61.             tm1.Start();  
  62.             this.KeyDown += MainWindow_KeyDown;  
  63.             for (int i = 0; i < 30; i++)  
  64.             {  
  65.                 //创建食物  
  66.                 food = new Border();  
  67.                 food.Height = food.Width = 10;  
  68.                 food.CornerRadius = new CornerRadius(5);  
  69.                 Canvas.SetTop(food, ra.Next(60) * 10);  
  70.                 Canvas.SetLeft(food, ra.Next(80) * 10);  
  71.                 food.Background = Brushes.Orange;  
  72.                 back.Children.Add(food);  
  73.                 foodback.Add(food);  
  74.             }  
  75.         }  
  76.   
  77.         private void MainWindow_KeyDown(object sender, KeyEventArgs e)  
  78.         {  
  79.             if (e.Key == Key.Up && str != "d")  
  80.             {  
  81.                 str = "up";  
  82.             }  
  83.             if (e.Key == Key.Down && str != "up")  
  84.             {  
  85.                 str = "d";  
  86.             }  
  87.             if (e.Key == Key.Left && str != "r")  
  88.             {  
  89.                 str = "l";  
  90.             }  
  91.             if (e.Key == Key.Right && str != "l")  
  92.             {  
  93.                 str = "r";  
  94.             }  
  95.         }  
  96.   
  97.         private void Tm1_Tick(object sender, EventArgs e)  
  98.         {  
  99.             if (str == "up")  
  100.             {  
  101.                 Canvas.SetTop(snake[0], Canvas.GetTop(snake[0]) - snake[0].Height);  
  102.                 run();  
  103.             }  
  104.             if (str == "d")  
  105.             {  
  106.                 Canvas.SetTop(snake[0], Canvas.GetTop(snake[0]) + snake[0].Height);  
  107.                 run();  
  108.             }  
  109.             if (str == "l")  
  110.             {  
  111.                 Canvas.SetLeft(snake[0], Canvas.GetLeft(snake[0]) - snake[0].Height);  
  112.                 run();  
  113.             }  
  114.             if (str == "r")  
  115.             {  
  116.                 Canvas.SetLeft(snake[0], Canvas.GetLeft(snake[0]) + snake[0].Height);  
  117.                 run();  
  118.             }  
  119.             for (int i = 0; i < 30; i++)  
  120.             {  
  121.                 if (Canvas.GetTop(snake[0]) == Canvas.GetTop(foodback[i]) && Canvas.GetLeft(snake[0]) == Canvas.GetLeft(foodback[i]))  
  122.                 {  
  123.                     Canvas.SetTop(foodback[i], ra.Next(60) * 10);  
  124.                     Canvas.SetLeft(foodback[i], ra.Next(80) * 10);  
  125.                     //定义一个增加的蛇尾的border  
  126.                     Border bdfood = new Border();  
  127.                     bdfood.Height = bdfood.Width = 10;  
  128.                     bdfood.CornerRadius = new CornerRadius(5);  
  129.                     bdfood.Background = Brushes.Black;  
  130.                     Canvas.SetTop(bdfood, Canvas.GetTop(snake[snake.Count - 1]));  
  131.                     Canvas.SetLeft(bdfood, Canvas.GetLeft(snake[snake.Count - 1]));  
  132.                     back.Children.Add(bdfood);  
  133.                     snake.Add(bdfood);  
  134.   
  135.                     //定义一个border存储增加的的集合  
  136.                     Border br = new Border();  
  137.                     br.Height = br.Width = 10;  
  138.                     Canvas.SetTop(br, Canvas.GetTop(snakeback[snakeback.Count - 1]));  
  139.                     Canvas.SetLeft(br, Canvas.GetLeft(snakeback[snakeback.Count - 1]));  
  140.                     snakeback.Add(br);  
  141.                 }  
  142.             }  
  143.             for (int i = 1; i < snake.Count; i++)  
  144.             {  
  145.                 if (Canvas.GetTop(snake[0]) == Canvas.GetTop(snake[i]) && Canvas.GetLeft(snake[0]) == Canvas.GetLeft(snake[i]))  
  146.                 {  
  147.                     tm1.Stop();  
  148.                     MessageBox.Show("GAME  OVER!");  
  149.                 }  
  150.             }  
  151.             if (Canvas.GetTop(snake[0]) == 0 || Canvas.GetTop(snake[0]) == 560 || Canvas.GetLeft(snake[0]) == 0 || Canvas.GetLeft(snake[0]) == 790)  
  152.             {  
  153.                 tm1.Stop();  
  154.                 MessageBox.Show("GAME  OVER!");  
  155.             }  
  156.         }  
  157.   
  158.         private void run()  
  159.         {  
  160.             for (int i = 1; i < snake.Count; i++)  
  161.             {  
  162.                 Canvas.SetTop(snake[i], Canvas.GetTop(snakeback[i - 1]));  
  163.                 Canvas.SetLeft(snake[i], Canvas.GetLeft(snakeback[i - 1]));  
  164.             }  
  165.             for (int i = 0; i < snake.Count; i++)  
  166.             {  
  167.                 Canvas.SetTop(snakeback[i], Canvas.GetTop(snake[i]));  
  168.                 Canvas.SetLeft(snakeback[i], Canvas.GetLeft(snake[i]));  
  169.             }  
  170.         }  
  171.     }  
posted @ 2017-12-13 21:50  绳命如此井猜  阅读(118)  评论(0编辑  收藏  举报