简易版贪吃蛇(双蛇)

简易版贪吃蛇(双蛇)usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usi

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;
using System.Media;

namespace _1212贪吃蛇
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //是否支持透明度
            this.WindowStyle = WindowStyle.None;
            this.AllowsTransparency = true;
        }
        Border brd = new Border();
        DispatcherTimer tim = new DispatcherTimer();//计时器
        DispatcherTimer tim2 = new DispatcherTimer();//蛇2的计时器
        SoundPlayer siwang = new SoundPlayer("../../nusic/死亡.wav");
        SoundPlayer chi = new SoundPlayer("../../nusic/吃食物.wav");
        SoundPlayer beijing = new SoundPlayer("../../nusic/BG音乐.wav");
        SoundPlayer du = new SoundPlayer("../../nusic/毒食物.wav");

      // DispatcherTimer tim3 = new DispatcherTimer();//倒计时
        //创建蛇得要求
        //初始长度为4,尺寸为20*20,颜色蛇头红色,蛇尾粉色
        //起始位置蛇头位于(900,600),圆形
        /// <summary>
        /// 页面布局
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Background = Brushes.Transparent;
            this.WindowState = WindowState.Maximized;//主界面窗体大小,颜色

            //bg.Background = Brushes.Transparent;
            bg.Background = new RadialGradientBrush(Colors.White, Colors.BurlyWood);//渐变
            //bg.Opacity = 0.4;//透明度

           
            tim.Interval =TimeSpan.FromMilliseconds(200);//频率400
            tim.Tick += Tim_Tick;//生成事件
            tim2.Interval = TimeSpan.FromMilliseconds(200);
            tim2.Tick += Tim2_Tick;
            
            bg.Width = this.Width;//bg背景大小
            bg.Height = this.Height;

            brd.Width = size * cols;//边框大小
            brd.Height= size * rows;
            brd.BorderThickness = new Thickness(20);//获取相应的thickness//边框长度
            brd.Background = new LinearGradientBrush(Colors.BlueViolet, Colors.Black, 0);
            brd.CornerRadius = new CornerRadius(10);//边框画圆
            Canvas.SetLeft(brd, (bg.Width - brd.Width) / 2);
            Canvas.SetTop(brd, (bg.Height - brd.Height) / 2);//坐标
            bg.Children.Add(brd);

            //开始游戏
           
            bt.Content = "开始游戏";
            bt.Click += Bt_Click;
            bt.Background = Brushes.Red;
            Canvas.SetLeft(bt, 20);
            Canvas.SetTop(bt, 30);
            bg.Children.Add(bt);
            //倒计时
            //tim3.Interval = new TimeSpan(10000000);
            //tim3.Tick += Tim3_Tick;

            //蛇2的分数
            fen1.Content = "分数1:" + j;
            fen1.Foreground = Brushes.Purple;
            fen1.FontSize = r.Next(30, 30);
            Canvas.SetLeft(fen1, 300);
            Canvas.SetTop(fen1, 30);
            bg.Children.Add(fen1);
           //蛇1的分数
            fen.Content = "分数"+i;//玩家1
            fen.Foreground = Brushes.Blue;
            fen.FontSize = r.Next(30, 30);
            Canvas.SetLeft(fen, 70);
            Canvas.SetTop(fen, 30);
            bg.Children.Add(fen);


            left = Canvas.GetLeft(brd) + 20;
            top = Canvas.GetTop(brd) + 20;
            Createsnake();
            Createshe();
            Createdoudou();
            Createdu();
            this.KeyDown += MainWindow_KeyDown;
        }
        Border duwu;
        private void Createdu()
        {
             duwu = new Border();
            duwu.Width = duwu.Height = size;//生成的豆豆,也就是说贪吃蛇的食物
            duwu.Background = Brushes.Orange;
            duwu.CornerRadius = new CornerRadius(10);//表示程度的角度值
            Canvas.SetLeft(duwu, r.Next(cols) * size + left);
            Canvas.SetTop(duwu, r.Next(rows) * size + top);//豆豆的坐标
            bg.Children.Add(duwu);
        }

        


        List<Border> snake = new List<Border>();
        List<Border> she = new List<Border>();
        double size = 20;//定义size大小
        double left = 0, top = 0;//初始位置
        /// <summary>
        /// 蛇2
        /// </summary>
        private void Createshe()
        {
            int chang = 4;
            for (int i = 0; i < chang; i++)
            {
                Border bd = new Border();
                bd.Width = size;
                bd.Height = size;
                bd.Tag = "D"; 
                if (i == 0)
                {
                    bd.Background = Brushes.Black;
                }
                else
                {
                    bd.Background = Brushes.Pink;
                }
               
                tim.Stop();
                Canvas.SetTop(bd, top + (rows / 3 * size));
                Canvas.SetLeft(bd, left + (cols / 3 * size) - i * size);
                bd.CornerRadius = new CornerRadius(20);
                bg.Children.Add(bd);
                she.Add(bd);
            }
        }
        /// <summary>
        /// 蛇2的移动以及吃豆得分
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Tim2_Tick(object sender, EventArgs e)
        {
            //she1
            for (int i = 0; i < she.Count; i++)//凳子的那种方法
            {
                if (she[i].Tag.ToString() == "A")
                {
                    Canvas.SetLeft(she[i], Canvas.GetLeft(she[i]) - size);
                }
                else if (she[i].Tag.ToString() == "D")
                {
                    Canvas.SetLeft(she[i], Canvas.GetLeft(she[i]) + size);
                }
                else if (she[i].Tag.ToString() == "W")
                {
                    Canvas.SetTop(she[i], Canvas.GetTop(she[i]) - size);
                }
                else if (she[i].Tag.ToString() == "S")
                {
                    Canvas.SetTop(she[i], Canvas.GetTop(she[i]) + size);
                }
            }
            for (int i = she.Count - 1; i > 0; i--)//一会问
            {
                she[i].Tag = she[i - 1].Tag;
            }
            if (Canvas.GetLeft(she[0]) == (Canvas.GetLeft(brd)+size) || Canvas.GetTop(she[0]) == (Canvas.GetTop(brd)+size) ||Canvas.GetLeft(she[0]) >1400||Canvas.GetTop(she[0])>800)
            {
                siwang.Play();
                beijing.PlayLooping();
                tim2.Stop();
                MessageBox.Show("撞墙了");
                
            }
            for (int i = 1; i < she.Count-1;  i++)
            {

            if (Canvas.GetLeft(she[0]) == Canvas.GetLeft(she[i]) && Canvas.GetTop(she[0]) == Canvas.GetTop(she[i]))
            {
                    siwang.Play();
                    beijing.PlayLooping();
                    tim2.Stop();
                    MessageBox.Show("吃到自己了");
            }
            }
            if (Canvas.GetTop(she[0]) == Canvas.GetTop(duwu) && Canvas.GetLeft(she[0]) == Canvas.GetLeft(duwu))
            {
                du.Play();
                beijing.PlayLooping();
                tim2.Stop();
                bg.Children.Remove(duwu);
                Createdu();
                MessageBox.Show("毒死了");
            }
            if (Canvas.GetLeft(she[0]) == Canvas.GetLeft(fond) && Canvas.GetTop(she[0]) == Canvas.GetTop(fond))//一会问
            {
                chi.Play();
                beijing.PlayLooping();
                j++;
                fen1.Content = "分数1:" + j;
                bg.Children.Remove(fond);
                Createdoudou();
                Border b2 = new Border();
                b2.Width = b2.Height = size;
                b2.Tag = " ";
                b2.Background = Brushes.Pink;
                b2.CornerRadius = new CornerRadius(10);
                Canvas.SetLeft(b2, Canvas.GetLeft(she[she.Count - 1]));
                Canvas.SetTop(b2, Canvas.GetTop(she[she.Count - 1]));
                she.Add(b2);
                bg.Children.Add(b2);
            }
               
        }
        int i;int j;
        Label fen = new Label();
        Button bt = new Button();
        bool b = true;
        //bool over = true;
        Label fen1 = new Label();
        /// <summary>
        /// 开始游戏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Bt_Click(object sender, RoutedEventArgs e)
        {
            if (b==true)
            {
                tim.Start();//开启
                tim2.Start();
                beijing.PlayLooping();
                bt.Content = "暂停游戏";
                b = false;
            }
            else
            {
                tim.Stop();//开启
                tim2.Stop();
                beijing.Stop();
                bt.Content = "开始游戏";
                b = true;
            }
        }

        int rows = 40, cols =70;//设计成格子,格子大小
        Random r = new Random();
        Border fond;
        /// <summary>
        /// 键盘事件控制方向
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Left:
                    if (snake[0].Tag.ToString()!="Right")
                    {
                    snake[0].Tag = "Left";

                    }
                    break;
                case Key.Right:
                    if (snake[0].Tag.ToString() != "Left")
                    {
                        snake[0].Tag = "Right";

                    }
                    break;
                case Key.Up:
                    if (snake[0].Tag.ToString() != "Down")
                    {
                        snake[0].Tag = "Up";

                    }
                    break;
                case Key.Down:
                    if (snake[0].Tag.ToString() != "Up")
                    {
                        snake[0].Tag = "Down";

                    }
                    break;
                default:
                    break;
            }
            switch (e.Key)
            {
                case Key.W:
                    if (she[0].Tag.ToString()!="S")
                    {

                    she[0].Tag = "W";
                    }
                    break;
                case Key.S:
                    if (she[0].Tag.ToString() != "W")
                    {

                        she[0].Tag = "S";
                    }
                    break;
                case Key.A:
                    if (she[0].Tag.ToString() != "D")
                    {

                        she[0].Tag = "A";
                    }
                    break;
                case Key.D:
                    if (she[0].Tag.ToString() != "A")
                    {

                        she[0].Tag = "D";
                    }
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 食物的产生,豆豆
        /// </summary>
        private void Createdoudou()
        {
            fond = new Border();
            fond.Width = fond.Height = size;//生成的豆豆,也就是说贪吃蛇的食物
            fond.Background = new RadialGradientBrush(Colors.Red, Colors.Gold);
            fond.CornerRadius = new CornerRadius(10);//表示程度的角度值
            Canvas.SetLeft(fond, r.Next(20,cols) * size+left);
            Canvas.SetTop(fond, r.Next(20,rows) * size+top );//豆豆的坐标
            bg.Children.Add(fond);
            
        }
        

        /// <summary>
        /// 蛇1的移动计时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Tim_Tick(object sender, EventArgs e)//移动
        {
            for (int i = 0; i < snake.Count; i++)
            {
                if (snake[i].Tag.ToString()=="Left")
                {
                    Canvas.SetLeft(snake[i], Canvas.GetLeft(snake[i]) - size);
                }
                else if (snake[i].Tag.ToString()=="Right")
                {
                    Canvas.SetLeft(snake[i], Canvas.GetLeft(snake[i]) + size);
                }
                else if (snake[i].Tag.ToString()=="Up")
                {
                    Canvas.SetTop(snake[i], Canvas.GetTop(snake[i]) - size);
                }
                else if (snake[i].Tag.ToString()=="Down")
                {
                    Canvas.SetTop(snake[i], Canvas.GetTop(snake[i]) + size);
                }
            }
            for (int i = snake.Count-1; i >0; i--)//一会问
            {
                snake[i].Tag = snake[i - 1].Tag;
            }
            //蛇撞墙游戏结束
            if (Canvas.GetLeft(snake[0]) == (Canvas.GetLeft(brd) + size) || Canvas.GetTop(snake[0]) == (Canvas.GetTop(brd) + size) || Canvas.GetLeft(snake[0])>1400||Canvas.GetTop(snake[0])>800)
            {
                siwang.Play();
                beijing.PlayLooping();
                tim.Stop();
                MessageBox.Show("撞墙了");
                }

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

                if (Canvas.GetLeft(snake[0]) == Canvas.GetLeft(snake[i]) && Canvas.GetTop(snake[0]) == Canvas.GetTop(snake[i]))
                {
                    siwang.Play();
                    beijing.PlayLooping();
                    tim.Stop();
                    MessageBox.Show("吃到自己了");
                }
            }
            if (Canvas.GetTop(snake[0]) == Canvas.GetTop(duwu) && Canvas.GetLeft(snake[0]) == Canvas.GetLeft(duwu))
            {
                du.Play();
                beijing.PlayLooping();
                tim.Stop();
                Createdu();
                MessageBox.Show("毒死了");
            }
            if (Canvas.GetLeft(snake[0])==Canvas.GetLeft(fond)&&Canvas.GetTop(snake[0])==Canvas.GetTop(fond))//一会问
            {
                chi.Play();
                beijing.PlayLooping();
                i++;
                fen.Content = "分数" + i;
                bg.Children.Remove(fond);
                Createdoudou();
                Border b = new Border();
                b.Width = b.Height = size;
                b.Tag = " ";
                b.Background = Brushes.Gray;
                b.CornerRadius = new CornerRadius(10);
                Canvas.SetLeft(b, Canvas.GetLeft(snake[snake.Count - 1]));
                Canvas.SetTop(b, Canvas.GetTop(snake[snake.Count - 1]));
                snake.Add(b);
                bg.Children.Add(b);
            } 
        }

        //蛇的初始长度
        //double sk = 4;
        /// <summary>
        /// 蛇1
        /// </summary>
        private void Createsnake()
        {
            int length = 4;//蛇的长度
            for (int i = 0; i < length; i++)
            {
                Border br = new Border();//为什么还要一个边框
                br.Width = size;
                br.Height = size;
                br.Tag = "Right";//自定义
                if (i==0)//蛇头是蓝色,身子是绿色
                {
                    br.Background = Brushes.Blue;
                }
                else
                {
                    br.Background = Brushes.Gray;
                }
                Canvas.SetLeft(br, left + (cols / 2 * size) - i * size);
                Canvas.SetTop(br, top + (rows / 2 * size));
                br.CornerRadius = new CornerRadius(20); //下来问
               
                bg.Children.Add(br);
                snake.Add(br);
            }
        }
    }
}

 

今天的文章简易版贪吃蛇(双蛇)分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/68187.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注