贪吃蛇大作战双人版加强版完整代码加详细步骤,不懂问博主 啦啦啦。

贪吃蛇总结:

  1. 创建WPF应用程序

 

  1. 添加Canvas标签。Name=back

 

  1. 添加loaded事件

 

  1. 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);

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

            }

 

  1. 添加一个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;

 

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

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

            }

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

            }

 

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

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

 

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

                {

                    y++;

                    l1.Content = "小强蛇分数:" + y;

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

                {

                    x++;

                    l.Content = "小黑蛇分数:" + x;

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

                }

            }

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

            }

        }

  1. 添加食物

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

            {

                //创建食物

                food = new Border();

                food.Width = food.Height = 10;

                //food.Background = Brushes.Yellow;

                //设置阴影

                //food.Effect = new System.Windows.Media.Effects.DropShadowEffect();

                //背景色渐变

                food.Background = new LinearGradientBrush(Colors.Red, Colors.Yellow, new Point(0, 0.1), new Point(0.8, 0.5));

 

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

            }

 

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

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

}

  1. 在与食物相撞时添加一节蛇尾
  2. 添加一个border来作为蛇尾,添加到snake集合中
  3. 添加一个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);

            }

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

                }

            }

 

12.判断蛇撞到自己,游戏结束。

 

    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();

                    if (x > y)

                    {

                        MessageBox.Show("GAME  OVER!" + "\n" + "小黑蛇赢!");

                    }

                    else if (y > x)

                    {

                        MessageBox.Show("GAME  OVER!" + "\n" + "小强蛇赢!");

                    }

                    else

                        if (x == y)

                    {

                        MessageBox.Show("GAME  OVER!" + "\n" + "平局!");

                    }

 

                }

            }

   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();

                    if (x > y)

                    {

                        MessageBox.Show("GAME  OVER!" + "\n" + "小黑蛇赢!");

                    }

                    else if (y > x)

                    {

                        MessageBox.Show("GAME  OVER!" + "\n" + "小强蛇赢!");

                    }

                    else

                        if (x == y)

                    {

                        MessageBox.Show("GAME  OVER!" + "\n" + "平局!");

                    }

                }

            }

 

13.蛇与面板四周相撞,游戏结束

 if (Canvas.GetTop(snack[0]) == 0 || Canvas.GetLeft(snack[0]) == 0 || Canvas.GetTop(snack[0]) == 500 || Canvas.GetLeft(snack[0]) == 780)

            {

                dt.Stop();

                if (x > y)

                {

                    MessageBox.Show("GAME  OVER!" + "\n" + "小黑蛇赢!");

                }

                else if (y > x)

                {

                    MessageBox.Show("GAME  OVER!" + "\n" + "小强蛇赢!");

                }

                else

                    if (x == y)

                {

                    MessageBox.Show("GAME  OVER!" + "\n" + "平局!");

                }

            }

  if (Canvas.GetTop(snack1[0]) == 0 || Canvas.GetLeft(snack1[0]) == 0 || Canvas.GetTop(snack1[0]) == 560 || Canvas.GetLeft(snack1[0]) == 780)

            {

                dt.Stop();

                if (x > y)

                {

                    MessageBox.Show("GAME  OVER!" + "\n" + "小黑蛇赢!");

                }

                else if (y > x)

                {

                    MessageBox.Show("GAME  OVER!" + "\n" + "小强蛇赢!");

                }

                else

                    if (x == y)

                {

                    MessageBox.Show("GAME  OVER!" + "\n" + "平局!");

                }

           

14.蛇与对方相撞,游戏结束

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

            {

                for (int j = 0; j < snack1.Count; j++)

                {

                    if (Canvas.GetTop(snack[0]) == Canvas.GetTop(snack1[j]) && Canvas.GetLeft(snack[0]) == Canvas.GetLeft(snack1[j])|| Canvas.GetTop(snack1[0]) == Canvas.GetTop(snack[i]) && Canvas.GetLeft(snack1[0]) == Canvas.GetLeft(snack[i]))

                    {

                        dt.Stop();

                        if (x > y)

                        {

                            MessageBox.Show("GAME  OVER!" + "\n" + "小黑蛇赢!");

                        }

                        else if (y > x)

                        {

                            MessageBox.Show("GAME  OVER!" + "\n" + "小强蛇赢!");

                        }

                        else

                            if (x == y)

                        {

                            MessageBox.Show("GAME  OVER!" + "\n" + "平局!");

                        }

                    }

 

                }

            }

15.添加开始、暂停、退出、难度选择

 <Button x:Name="start" Content="开始" Canvas.Left="31" Canvas.Top="-36" Width="75" />

        <Button x:Name="stop" Content="暂停" Canvas.Left="130" Canvas.Top="-36" Width="75"/>

        <ComboBox x:Name="nandu" Canvas.Left="221" Canvas.Top="-38" Width="108"  IsReadOnly="True"/>

        <Button x:Name="tuichu" Content="退出" Canvas.Left="439" Canvas.Top="-36" Width="75"/>

 //开始按钮设置

            start.Height = 20;

            start.Width = 50;

            start.Background = Brushes.OrangeRed;

            start.Margin = new Thickness(20, 0, 20, 20);

            start.Click += Start_Click;

            //暂停按钮设计

            stop.Height = 20;

            stop.Width = 50;

            stop.Background = Brushes.OrangeRed;

            stop.Margin = new Thickness(0, 0, 60, 20);

            stop.Click += Stop_Click;

            //退出按钮设置

            tuichu.Height = 20;

            tuichu.Width = 50;

            tuichu.Background = Brushes.OrangeRed;

            tuichu.Margin = new Thickness(0, 0, 60, 20);

            tuichu.Click += Tuichu_Click;

            nandu.SelectedIndex = 0;

            nandu.Items.Add("学霸模式");

            nandu.Items.Add("学渣模式");

            nandu.Items.Add("学习模式");

            nandu.Background = Brushes.Blue;

            nandu.Margin = new Thickness(0, 0, 20, 20)

        private void Tuichu_Click(object sender, RoutedEventArgs e)

        {

            this.Close();

        }

 

        private void Stop_Click(object sender, RoutedEventArgs e)

        {

            dt.Stop();

        }

 private void Start_Click(object sender, RoutedEventArgs e)

        {

            if (nandu.SelectedValue.ToString() == "学霸模式")

            {

                dt.Interval = TimeSpan.FromMilliseconds(50);

            }

 

            else if (nandu.SelectedValue.ToString() == "学渣模式")

            {

                dt.Interval = TimeSpan.FromMilliseconds(200);

            }

            else if (nandu.SelectedValue.ToString() == "学习模式")

            {

                dt.Interval = TimeSpan.FromMilliseconds(100);

            }

            nandu.IsEnabled = false; ;

            dt.Start();

        }

  1. 添加背景音乐按钮控制播放

<MediaElement Name="audio"/>

        <Button Name="music" Content="点我有音乐哦" Canvas.Left="347" Canvas.Top="-36" >

            <Button.Triggers>

                <EventTrigger RoutedEvent="Button.Click">

                    <BeginStoryboard>

                        <Storyboard>

                            <MediaTimeline Source="7895.WAV" Storyboard.TargetName="audio" RepeatBehavior="Forever" />

                        </Storyboard>

                    </BeginStoryboard>

                </EventTrigger>

            </Button.Triggers>

        </Button>

 

  1. 其中遇到的问题:

设置button时,键盘的控制蛇移动的上下左右键会控制ComboBox,蛇不能移动

解决:将Button和ComboBox放在windows上面,而不是Canvas上面,当开始按钮按下时,设置ComboBox为不可用。

 

posted @ 2017-12-20 16:19  绳命如此井猜  阅读(672)  评论(0编辑  收藏  举报