码迷,mamicode.com
首页 > 其他好文 > 详细

贪吃蛇大作战双人版完整代码,不懂问博主。

时间:2017-12-14 03:51:34      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:com   keyevent   text   initial   red   amp   navig   doc   ima   

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using System.Windows.Data;  
  9. using System.Windows.Documents;  
  10. using System.Windows.Input;  
  11. using System.Windows.Media;  
  12. using System.Windows.Media.Imaging;  
  13. using System.Windows.Navigation;  
  14. using System.Windows.Shapes;  
  15. using System.Windows.Threading;  
  16.   
  17. namespace 贪吃蛇  
  18. {  
  19.     /// <summary>  
  20.     /// MainWindow.xaml 的交互逻辑  
  21.     /// </summary>  
  22.     public partial class MainWindow : Window  
  23.     {  
  24.         public MainWindow()  
  25.         {  
  26.             InitializeComponent();  
  27.             this.Background = Brushes.Pink;  
  28.         }  
  29.         Random ra = new Random();  
  30.         Border food;  
  31.         Border bd1;  
  32.         DispatcherTimer dt;  
  33.         string s = "";  
  34.         string s1 = "";  
  35.         List<Bordersnack = new List<Border>();  
  36.         List<Bordersnakeback = new List<Border>();  
  37.         List<Bordersnack1 = new List<Border>();  
  38.         List<Bordersnakeback1 = new List<Border>();  
  39.         //创建一个放食物的集合  
  40.         List<Borderfoodback = new List<Border>();  
  41.         private void Window_Loaded(object sender, RoutedEventArgs e)  
  42.         {  
  43.             this.Left = 0;  
  44.             this.Top = 0;  
  45.             //this.Width = SystemParameters.FullPrimaryScreenWidth;  
  46.             //this.Height = SystemParameters.FullPrimaryScreenHeight;  
  47.             this.Width = 800;  
  48.             this.Height = 600;  
  49.             for (int i = 0; i 5; i++)  
  50.             {  
  51.                 Border bd = new Border();  
  52.                 if (i == 0)  
  53.                 {  
  54.                     bd.Background = Brushes.Red;  
  55.                 }  
  56.                 else  
  57.                 {  
  58.                     bd.Background = Brushes.Black;  
  59.                 }  
  60.                 bd.Height = bd.Width = 10;  
  61.                 bd.CornerRadius = new CornerRadius(5);  
  62.                 Canvas.SetLeft(bd, 200);  
  63.                 Canvas.SetTop(bd, 200 + i * bd.Height);  
  64.                 back.Children.Add(bd);  
  65.                 snack.Add(bd);  
  66.                 //存储蛇的位置的border  
  67.                 Border bdback = new Border();  
  68.                 Canvas.SetLeft(bdback, 200);  
  69.                 Canvas.SetTop(bdback, 200 + i * bd.Height);  
  70.                 snakeback.Add(bdback);  
  71.             }  
  72.             for (int i = 0; i 5; i++)  
  73.             {  
  74.                 bd1 = new Border();  
  75.                 bd1.Width = bd1.Height = 10;  
  76.                 bd1.CornerRadius = new CornerRadius(5);  
  77.                 if (i == 0)  
  78.                     bd1.Background = Brushes.Chocolate;  
  79.                 else  
  80.                     bd1.Background = Brushes.DarkBlue;  
  81.                 Canvas.SetTop(bd1, 400 + i * bd1.Height);  
  82.                 Canvas.SetLeft(bd1, 400);  
  83.                 back.Children.Add(bd1);  
  84.                 snack1.Add(bd1);  
  85.   
  86.                 //创建蛇的位置border  
  87.                 Border bdback1 = new Border();  
  88.                 bdback1.Height = bdback1.Width = 10;  
  89.                 Canvas.SetTop(bdback1, 400 + i * bd1.Height);  
  90.                 Canvas.SetLeft(bdback1, 400);  
  91.                 snakeback1.Add(bdback1);  
  92.             }  
  93.             this.KeyDown += MainWindow_KeyDown;  
  94.             dt = new DispatcherTimer();  
  95.             dt.Tick += Dt_Tick;  
  96.             dt.Interval = TimeSpan.FromMilliseconds(80);  
  97.             dt.Start();  
  98.             for (int i = 0; i 30; i++)  
  99.             {  
  100.                 //创建食物  
  101.                 food = new Border();  
  102.                 food.Width = food.Height = 10;  
  103.                 food.Background = Brushes.Yellow;  
  104.                 Canvas.SetTop(food, ra.Next(60) * 10);  
  105.                 Canvas.SetLeft(food, ra.Next(80) * 10);  
  106.                 food.CornerRadius = new CornerRadius(10);  
  107.                 back.Children.Add(food);  
  108.                 foodback.Add(food);  
  109.             }  
  110.         }  
  111.   
  112.         private void Dt_Tick(object sender, EventArgs e)  
  113.         {  
  114.             if (s == "up")  
  115.             {  
  116.                 Canvas.SetTop(snack[0], Canvas.GetTop(snack[0]) - snack[0].Height);  
  117.                 Run();  
  118.                   
  119.             }  
  120.             if (s == "down")  
  121.             {  
  122.                 Canvas.SetTop(snack[0], Canvas.GetTop(snack[0]) + snack[0].Height);  
  123.                 Run();  
  124.                  
  125.             }  
  126.             if (s == "left")  
  127.             {  
  128.                 Canvas.SetLeft(snack[0], Canvas.GetLeft(snack[0]) - snack[0].Width);  
  129.                 Run();  
  130.                   
  131.             }  
  132.             if (s == "right")  
  133.             {  
  134.                 Canvas.SetLeft(snack[0], Canvas.GetLeft(snack[0]) + snack[0].Width);  
  135.                 Run();  
  136.                   
  137.             }  
  138.             if (s1 == "w")  
  139.             {  
  140.                 Canvas.SetTop(snack1[0], Canvas.GetTop(snack1[0]) - snack1[0].Height);  
  141.                 Run1();  
  142.             }  
  143.             if (s1 == "s")  
  144.             {  
  145.                 Canvas.SetTop(snack1[0], Canvas.GetTop(snack1[0]) + snack1[0].Height);  
  146.                 Run1();  
  147.             }  
  148.             if (s1 == "a")  
  149.             {  
  150.                 Canvas.SetLeft(snack1[0], Canvas.GetLeft(snack1[0]) - snack1[0].Width);  
  151.                 Run1();  
  152.             }  
  153.             if (s1 == "d")  
  154.             {  
  155.                 Canvas.SetLeft(snack1[0], Canvas.GetLeft(snack1[0]) + snack1[0].Width);  
  156.                 Run1();  
  157.             }  
  158.             for (int i = 0; i 30; i++)  
  159.             {  
  160.                 if (Canvas.GetTop(snack1[0]) == Canvas.GetTop(foodback[i]) && Canvas.GetLeft(snack1[0]) == Canvas.GetLeft(foodback[i]))  
  161.                 {  
  162.                     Canvas.SetTop(foodback[i], ra.Next(60) * 10);  
  163.                     Canvas.SetLeft(foodback[i], ra.Next(80) * 10);  
  164.   
  165.                     //创建一个border添加到尾部  
  166.                     Border bdfood1 = new Border();  
  167.                     bdfood1.Width = bdfood1.Height = 10;  
  168.                     bdfood1.Background = Brushes.DarkBlue;  
  169.                     bdfood1.CornerRadius = new CornerRadius(5);  
  170.                     Canvas.SetTop(bdfood1, Canvas.GetTop(snack[snack.Count - 1]));  
  171.                     Canvas.SetLeft(bdfood1, Canvas.GetLeft(snack[snack.Count - 1]));  
  172.                     snack1.Add(bdfood1);  
  173.                     back.Children.Add(bdfood1);  
  174.   
  175.                     //创建一个border,存放加到后面的蛇身的位置  
  176.                     Border br1 = new Border();  
  177.                     Canvas.SetTop(br1, Canvas.GetTop(bdfood1));  
  178.                     Canvas.SetLeft(br1, Canvas.GetLeft(bdfood1));  
  179.                     snakeback1 .Add(br1);  
  180.                 }  
  181.             }  
  182.             for (int i = 0; i 30; i++)  
  183.             {  
  184.                 if (Canvas.GetTop(snack[0]) == Canvas.GetTop(foodback[i]) && Canvas.GetLeft(snack[0]) == Canvas.GetLeft(foodback[i]))  
  185.                 {  
  186.                     Canvas.SetTop(foodback[i], ra.Next(60) * 10);  
  187.                     Canvas.SetLeft(foodback[i], ra.Next(80) * 10);  
  188.   
  189.                     //创建一个border添加到尾部  
  190.                     Border bdfood = new Border();  
  191.                     bdfood.Width = bdfood.Height = 10;  
  192.                     bdfood.Background = Brushes.Black;  
  193.                     bdfood.CornerRadius = new CornerRadius(5);  
  194.                     Canvas.SetTop(bdfood, Canvas.GetTop(snack[snack.Count - 1]));  
  195.                     Canvas.SetLeft(bdfood, Canvas.GetLeft(snack[snack.Count - 1]));  
  196.                     snack.Add(bdfood);  
  197.                     back.Children.Add(bdfood);  
  198.   
  199.                     //创建一个border,存放加到后面的蛇身的位置  
  200.                     Border br = new Border();  
  201.                     Canvas.SetTop(br, Canvas.GetTop(bdfood));  
  202.                     Canvas.SetLeft(br, Canvas.GetLeft(bdfood));  
  203.                     snakeback.Add(br);  
  204.                 }  
  205.             }  
  206.             for (int i = 1; i snack.Count; i++)  
  207.             {  
  208.                 if (Canvas.GetTop(snack[0]) == Canvas.GetTop(snack[i]) && Canvas.GetLeft(snack[0]) == Canvas.GetLeft(snack[i]))  
  209.                 {  
  210.                     dt.Stop();  
  211.                     MessageBox.Show("GAME  OVER!");  
  212.                 }  
  213.             }  
  214.             if (Canvas.GetTop(snack[0]) == 0 || Canvas.GetLeft(snack[0]) == 0 || Canvas.GetTop(snack[0]) == 560 || Canvas.GetLeft(snack[0]) == 780)  
  215.             {  
  216.                 dt.Stop();  
  217.                 MessageBox.Show("GAME  OVER!");  
  218.             }  
  219.             for (int i = 1; i snack1.Count; i++)  
  220.             {  
  221.                 if (Canvas.GetTop(snack1[0]) == Canvas.GetTop(snack1[i]) && Canvas.GetLeft(snack1[0]) == Canvas.GetLeft(snack1[i]))  
  222.                 {  
  223.                     dt.Stop();  
  224.                     MessageBox.Show("GAME  OVER!");  
  225.                 }  
  226.             }  
  227.             if (Canvas.GetTop(snack1[0]) == 0 || Canvas.GetLeft(snack1[0]) == 0 || Canvas.GetTop(snack1[0]) == 560 || Canvas.GetLeft(snack1[0]) == 780)  
  228.             {  
  229.                 dt.Stop();  
  230.                 MessageBox.Show("GAME  OVER!");  
  231.             }  
  232.         }  
  233.         private void Run()  
  234.         {  
  235.             for (int i = 1; i snack.Count; i++)  
  236.             {  
  237.                 Canvas.SetTop(snack[i], Canvas.GetTop(snakeback[i - 1]));  
  238.                 Canvas.SetLeft(snack[i], Canvas.GetLeft(snakeback[i - 1]));  
  239.             }  
  240.             for (int i = 0; i snack.Count; i++)  
  241.             {  
  242.                 Canvas.SetTop(snakeback[i], Canvas.GetTop(snack[i]));  
  243.                 Canvas.SetLeft(snakeback[i], Canvas.GetLeft(snack[i]));  
  244.             }  
  245.         }  
  246.         private void Run1()  
  247.         {  
  248.             for (int i = 1; i snack1.Count; i++)  
  249.             {  
  250.                 Canvas.SetTop(snack1[i], Canvas.GetTop(snakeback1[i - 1]));  
  251.                 Canvas.SetLeft(snack1[i], Canvas.GetLeft(snakeback1[i - 1]));  
  252.             }  
  253.             for (int i = 0; i snack1.Count; i++)  
  254.             {  
  255.                 Canvas.SetTop(snakeback1[i], Canvas.GetTop(snack1[i]));  
  256.                 Canvas.SetLeft(snakeback1[i], Canvas.GetLeft(snack1[i]));  
  257.             }  
  258.         }  
  259.         private void MainWindow_KeyDown(object sender, KeyEventArgs e)  
  260.         {  
  261.             if (e.Key == Key.Up && s != "down")  
  262.             {  
  263.                 s = "up";  
  264.             }  
  265.             if (e.Key == Key.Down && s != "up")  
  266.             {  
  267.                 s = "down";  
  268.             }  
  269.             if (e.Key == Key.Left && s != "right")  
  270.             {  
  271.                 s = "left";  
  272.             }  
  273.             if (e.Key == Key.Right && s != "left")  
  274.             {  
  275.                 s = "right";  
  276.             }  
  277.             if (e.Key == Key.W && s1 != "s")  
  278.             {  
  279.                 s1 = "w";  
  280.             }  
  281.             if (e.Key == Key.S && s1 != "w")  
  282.             {  
  283.                 s1 = "s";  
  284.             }  
  285.             if (e.Key == Key.A && s1 != "d")  
  286.             {  
  287.                 s1 = "a";  
  288.             }  
  289.             if (e.Key == Key.D && s1 != "a")  
  290.             {  
  291.                 s1 = "d";  
  292.             }  
  293.         }  
  294.     }  
  295. }  

贪吃蛇大作战双人版完整代码,不懂问博主。

标签:com   keyevent   text   initial   red   amp   navig   doc   ima   

原文地址:http://www.cnblogs.com/wjrelax/p/8035061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!