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

贪吃蛇总结:

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,(问题:添加timerwinform程序中不一样

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";

            {

                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!");

            }

完整版代码:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;

namespace 贪吃蛇1._0
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Background = Brushes.Pink;
        }
        string str = "";
        Border bd;
        DispatcherTimer tm1;
        Border food;
        List snake = new List();
        //创建蛇的位置集合
        List snakeback = new List();
        List foodback = new List();
        Random ra = new Random();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            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);

                //创建蛇的位置border
                Border bdback = new Border();
                bdback.Height = bdback.Width = 10;
                Canvas.SetTop(bdback, 200 + i * bd.Height);
                Canvas.SetLeft(bdback, 200);
                snakeback.Add(bdback);
            }

            tm1 = new DispatcherTimer();
            tm1.Interval = TimeSpan.FromMilliseconds(200);
            tm1.Tick += Tm1_Tick;
            tm1.Start();
            this.KeyDown += MainWindow_KeyDown;
            for (int i = 0; i < 30; i++)
            {
                //创建食物
                food = new Border();
                food.Height = food.Width = 10;
                food.CornerRadius = new CornerRadius(5);
                Canvas.SetTop(food, ra.Next(60) * 10);
                Canvas.SetLeft(food, ra.Next(80) * 10);
                food.Background = Brushes.Orange;
                back.Children.Add(food);
                foodback.Add(food);
            }
        }

        private void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            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";
            }
        }

        private void Tm1_Tick(object sender, EventArgs e)
        {
            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();
            }
            for (int i = 0; i < 30; i++)
            {
                if (Canvas.GetTop(snake[0]) == Canvas.GetTop(foodback[i]) && Canvas.GetLeft(snake[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.Height = bdfood.Width = 10;
                    bdfood.CornerRadius = new CornerRadius(5);
                    bdfood.Background = Brushes.Black;
                    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);
                }
            }
            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!");
            }
        }

        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]));
            }
        }
    }
}


 

你可能感兴趣的:(贪吃蛇大作战单人版完整版代码以及详解)