- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- namespace 贪吃蛇
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- this.Background = Brushes.Pink;
- }
- Random ra = new Random();
- Border food;
- Border bd1;
- DispatcherTimer dt;
- string s = "";
- string s1 = "";
- List<Border> snack = new List<Border>();
- List<Border> snakeback = new List<Border>();
- List<Border> snack1 = new List<Border>();
- List<Border> snakeback1 = new List<Border>();
- //创建一个放食物的集合
- List<Border> foodback = new List<Border>();
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- this.Left = 0;
- this.Top = 0;
- //this.Width = SystemParameters.FullPrimaryScreenWidth;
- //this.Height = SystemParameters.FullPrimaryScreenHeight;
- this.Width = 800;
- this.Height = 600;
- for (int i = 0; i < 5; i++)
- {
- Border bd = new Border();
- if (i == 0)
- {
- bd.Background = Brushes.Red;
- }
- else
- {
- bd.Background = Brushes.Black;
- }
- bd.Height = bd.Width = 10;
- bd.CornerRadius = new CornerRadius(5);
- Canvas.SetLeft(bd, 200);
- Canvas.SetTop(bd, 200 + i * bd.Height);
- back.Children.Add(bd);
- snack.Add(bd);
- //存储蛇的位置的border
- Border bdback = new Border();
- Canvas.SetLeft(bdback, 200);
- Canvas.SetTop(bdback, 200 + i * bd.Height);
- snakeback.Add(bdback);
- }
- for (int i = 0; i < 5; i++)
- {
- bd1 = new Border();
- bd1.Width = bd1.Height = 10;
- bd1.CornerRadius = new CornerRadius(5);
- if (i == 0)
- bd1.Background = Brushes.Chocolate;
- else
- bd1.Background = Brushes.DarkBlue;
- Canvas.SetTop(bd1, 400 + i * bd1.Height);
- Canvas.SetLeft(bd1, 400);
- back.Children.Add(bd1);
- snack1.Add(bd1);
- //创建蛇的位置border
- Border bdback1 = new Border();
- bdback1.Height = bdback1.Width = 10;
- Canvas.SetTop(bdback1, 400 + i * bd1.Height);
- Canvas.SetLeft(bdback1, 400);
- snakeback1.Add(bdback1);
- }
- this.KeyDown += MainWindow_KeyDown;
- dt = new DispatcherTimer();
- dt.Tick += Dt_Tick;
- dt.Interval = TimeSpan.FromMilliseconds(80);
- dt.Start();
- for (int i = 0; i < 30; i++)
- {
- //创建食物
- food = new Border();
- food.Width = food.Height = 10;
- food.Background = Brushes.Yellow;
- Canvas.SetTop(food, ra.Next(60) * 10);
- Canvas.SetLeft(food, ra.Next(80) * 10);
- food.CornerRadius = new CornerRadius(10);
- back.Children.Add(food);
- foodback.Add(food);
- }
- }
- private void Dt_Tick(object sender, EventArgs e)
- {
- if (s == "up")
- {
- Canvas.SetTop(snack[0], Canvas.GetTop(snack[0]) - snack[0].Height);
- Run();
- }
- if (s == "down")
- {
- Canvas.SetTop(snack[0], Canvas.GetTop(snack[0]) + snack[0].Height);
- Run();
- }
- if (s == "left")
- {
- Canvas.SetLeft(snack[0], Canvas.GetLeft(snack[0]) - snack[0].Width);
- Run();
- }
- if (s == "right")
- {
- Canvas.SetLeft(snack[0], Canvas.GetLeft(snack[0]) + snack[0].Width);
- Run();
- }
- if (s1 == "w")
- {
- Canvas.SetTop(snack1[0], Canvas.GetTop(snack1[0]) - snack1[0].Height);
- Run1();
- }
- if (s1 == "s")
- {
- Canvas.SetTop(snack1[0], Canvas.GetTop(snack1[0]) + snack1[0].Height);
- Run1();
- }
- if (s1 == "a")
- {
- Canvas.SetLeft(snack1[0], Canvas.GetLeft(snack1[0]) - snack1[0].Width);
- Run1();
- }
- if (s1 == "d")
- {
- Canvas.SetLeft(snack1[0], Canvas.GetLeft(snack1[0]) + snack1[0].Width);
- Run1();
- }
- for (int i = 0; i < 30; i++)
- {
- if (Canvas.GetTop(snack1[0]) == Canvas.GetTop(foodback[i]) && Canvas.GetLeft(snack1[0]) == Canvas.GetLeft(foodback[i]))
- {
- Canvas.SetTop(foodback[i], ra.Next(60) * 10);
- Canvas.SetLeft(foodback[i], ra.Next(80) * 10);
- //创建一个border添加到尾部
- Border bdfood1 = new Border();
- bdfood1.Width = bdfood1.Height = 10;
- bdfood1.Background = Brushes.DarkBlue;
- bdfood1.CornerRadius = new CornerRadius(5);
- Canvas.SetTop(bdfood1, Canvas.GetTop(snack[snack.Count - 1]));
- Canvas.SetLeft(bdfood1, Canvas.GetLeft(snack[snack.Count - 1]));
- snack1.Add(bdfood1);
- back.Children.Add(bdfood1);
- //创建一个border,存放加到后面的蛇身的位置
- Border br1 = new Border();
- Canvas.SetTop(br1, Canvas.GetTop(bdfood1));
- Canvas.SetLeft(br1, Canvas.GetLeft(bdfood1));
- snakeback1 .Add(br1);
- }
- }
- for (int i = 0; i < 30; i++)
- {
- if (Canvas.GetTop(snack[0]) == Canvas.GetTop(foodback[i]) && Canvas.GetLeft(snack[0]) == Canvas.GetLeft(foodback[i]))
- {
- Canvas.SetTop(foodback[i], ra.Next(60) * 10);
- Canvas.SetLeft(foodback[i], ra.Next(80) * 10);
- //创建一个border添加到尾部
- Border bdfood = new Border();
- bdfood.Width = bdfood.Height = 10;
- bdfood.Background = Brushes.Black;
- bdfood.CornerRadius = new CornerRadius(5);
- Canvas.SetTop(bdfood, Canvas.GetTop(snack[snack.Count - 1]));
- Canvas.SetLeft(bdfood, Canvas.GetLeft(snack[snack.Count - 1]));
- snack.Add(bdfood);
- back.Children.Add(bdfood);
- //创建一个border,存放加到后面的蛇身的位置
- Border br = new Border();
- Canvas.SetTop(br, Canvas.GetTop(bdfood));
- Canvas.SetLeft(br, Canvas.GetLeft(bdfood));
- snakeback.Add(br);
- }
- }
- for (int i = 1; i < snack.Count; i++)
- {
- if (Canvas.GetTop(snack[0]) == Canvas.GetTop(snack[i]) && Canvas.GetLeft(snack[0]) == Canvas.GetLeft(snack[i]))
- {
- dt.Stop();
- MessageBox.Show("GAME OVER!");
- }
- }
- if (Canvas.GetTop(snack[0]) == 0 || Canvas.GetLeft(snack[0]) == 0 || Canvas.GetTop(snack[0]) == 560 || Canvas.GetLeft(snack[0]) == 780)
- {
- dt.Stop();
- MessageBox.Show("GAME OVER!");
- }
- for (int i = 1; i < snack1.Count; i++)
- {
- if (Canvas.GetTop(snack1[0]) == Canvas.GetTop(snack1[i]) && Canvas.GetLeft(snack1[0]) == Canvas.GetLeft(snack1[i]))
- {
- dt.Stop();
- MessageBox.Show("GAME OVER!");
- }
- }
- if (Canvas.GetTop(snack1[0]) == 0 || Canvas.GetLeft(snack1[0]) == 0 || Canvas.GetTop(snack1[0]) == 560 || Canvas.GetLeft(snack1[0]) == 780)
- {
- dt.Stop();
- MessageBox.Show("GAME OVER!");
- }
- }
- private void Run()
- {
- for (int i = 1; i < snack.Count; i++)
- {
- Canvas.SetTop(snack[i], Canvas.GetTop(snakeback[i - 1]));
- Canvas.SetLeft(snack[i], Canvas.GetLeft(snakeback[i - 1]));
- }
- for (int i = 0; i < snack.Count; i++)
- {
- Canvas.SetTop(snakeback[i], Canvas.GetTop(snack[i]));
- Canvas.SetLeft(snakeback[i], Canvas.GetLeft(snack[i]));
- }
- }
- private void Run1()
- {
- for (int i = 1; i < snack1.Count; i++)
- {
- Canvas.SetTop(snack1[i], Canvas.GetTop(snakeback1[i - 1]));
- Canvas.SetLeft(snack1[i], Canvas.GetLeft(snakeback1[i - 1]));
- }
- for (int i = 0; i < snack1.Count; i++)
- {
- Canvas.SetTop(snakeback1[i], Canvas.GetTop(snack1[i]));
- Canvas.SetLeft(snakeback1[i], Canvas.GetLeft(snack1[i]));
- }
- }
- private void MainWindow_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Up && s != "down")
- {
- s = "up";
- }
- if (e.Key == Key.Down && s != "up")
- {
- s = "down";
- }
- if (e.Key == Key.Left && s != "right")
- {
- s = "left";
- }
- if (e.Key == Key.Right && s != "left")
- {
- s = "right";
- }
- if (e.Key == Key.W && s1 != "s")
- {
- s1 = "w";
- }
- if (e.Key == Key.S && s1 != "w")
- {
- s1 = "s";
- }
- if (e.Key == Key.A && s1 != "d")
- {
- s1 = "a";
- }
- if (e.Key == Key.D && s1 != "a")
- {
- s1 = "d";
- }
- }
- }
- }