C#wpf貪喫蛇實現

分析

1,實現地圖,蛇,食物的出現
2,蛇的長度以及蛇頭與蛇身的區別
3,食物的隨機位置
4,實現開始繼續讓蛇停止與繼續移動
5,蛇喫到食物加分數
6,蛇碰撞到牆壁或者蛇身就會死亡

代碼實現

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

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //窗體邊框不顯示
            this.WindowStyle = WindowStyle.None;
        }
        DispatcherTimer moverTimer = new DispatcherTimer();//計時器
        Random ra = new Random();//創建隨機數,食物所需要
        Border food = new Border();//創建食物
        int fenshu = 0; //記錄分數
        Label feng = new Label(); // 分數對象
        

        List<Border> snake = new List<Border>();

        //更方便計算
        double size = 20;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            
            this.Background = Brushes.Transparent;
            //全屏顯示
            this.WindowState = WindowState.Maximized;
            this.Background = Brushes.Transparent;

            //顏色漸變
            continer.Background = new RadialGradientBrush(Colors.White,Colors.Blue);

            //正常顯示
            continer.Width = this.Width;
            continer.Height = this.Height;

            feng.Width = 120;
            feng.Height = 50;
            Canvas.SetLeft(feng, 1060);
            Canvas.SetTop(feng, 100);
            feng.Background = Brushes.Coral;
            feng.Foreground = Brushes.DeepSkyBlue;
            feng.Content = "  得分:" + fenshu ;
            feng.FontFamily = new FontFamily("楷體");
            feng.FontSize = 20;
            continer.Children.Add(feng);

            Button Kaise = new Button();
            Kaise.Cursor = Cursors.Hand; // 鼠標移動到按鈕處變成手型
            Kaise.Width = 120;
            Kaise.Height = 50;
            Canvas.SetLeft(Kaise, 1060);
            Canvas.SetTop(Kaise, 160);
            Kaise.Content = "開始遊戲";
            Kaise.FontFamily = new FontFamily("楷體");
            Kaise.BorderBrush = Brushes.Cyan; // 邊框顏色
            Kaise.Foreground = Brushes.DarkRed; // 字體顏色
            Kaise.FontSize = 20;
            Kaise.Background = Brushes.BurlyWood;
            Kaise.Click += Kaise_Click;
            continer.Children.Add(Kaise);

            Button Zanting = new Button();
            Zanting.Cursor = Cursors.Hand; // 鼠標移動到按鈕處變爲手型
            Zanting.Width = 120;
            Zanting.Height = 50;
            Canvas.SetLeft(Zanting, 1060);
            Canvas.SetTop(Zanting, 220);
            Zanting.Content = "暫停遊戲";
            Zanting.FontFamily = new FontFamily("楷體");
            Zanting.BorderBrush = Brushes.Cyan;
            Zanting.Foreground = Brushes.DarkRed;
            Zanting.FontSize = 20;
            Zanting.Background = Brushes.BurlyWood;
            Zanting.Click += Zanting_Click;
            continer.Children.Add(Zanting);


            
            moverTimer.Interval = TimeSpan.FromMilliseconds(100);
            moverTimer.Tick += MoverTimer_Tick;
             //moverTimer.Start();


            CreateSnake();
            CreateDouDou();

            //添加鍵盤事件
            this.KeyDown += MainWindow_KeyDown;
        }

        private void Kaise_Click(object sender, RoutedEventArgs e)
        {
            moverTimer.Start();
        }


        private void Zanting_Click(object sender, RoutedEventArgs e)
        {
            
            moverTimer.Stop();
            
        }
        private void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {

            //識別鍵盤按鈕,而設置標籤
            switch (e.Key)
            {
                case Key.Right:
                    snake[0].Tag = "Right";
                    break;
                case Key.Left:
                    snake[0].Tag = "Left";
                    break;
                case Key.Up:
                    snake[0].Tag = "Up";
                    break;
                case Key.Down:
                    snake[0].Tag = "Down";
                    break;
                
            }
           
        }

        private void MoverTimer_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 (snake.Count > 0 && Canvas.GetTop(snake[0]) == Canvas.GetTop(food) && Canvas.GetLeft(snake[0]) == Canvas.GetLeft(food))
            {
                fenshu++; 
                Canvas.SetLeft(food, ra.Next(geziNumX) * size);
                Canvas.SetTop(food, ra.Next(geziNumY) * size);
                Border tianjia = new Border();//增添蛇身
                tianjia.Width = tianjia.Height = size;
                tianjia.Tag = " ";
                tianjia.Background = Brushes.Green;
                tianjia.CornerRadius = new CornerRadius(20); // 變圓
                Canvas.SetLeft(tianjia, Canvas.GetLeft(snake[snake.Count - 1])); // 獲取到蛇尾的Left位置
                Canvas.SetTop(tianjia, Canvas.GetTop(snake[snake.Count - 1])); // 獲取到蛇尾的Top位置
                continer.Children.Add(tianjia);
                snake.Add(tianjia);
                feng.Content = "  得分:" + fenshu  ;
            }
            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]))
                {
                    MessageBox.Show("遊戲結束");
                    moverTimer.Stop();
                }
                //蛇頭碰到牆
                if (Canvas.GetTop(snake[0]) <= 0 || Canvas.GetTop(snake[0]) == continer.Height || Canvas.GetLeft(snake[0]) <= 0 || Canvas.GetLeft(snake[0]) == continer.Width)
                {
                    MessageBox.Show("遊戲結束");
                    moverTimer.Stop();
                }
            }
        }

        int geziNumX = 60;
        int geziNumY = 40;


        //創建蛇元素
        private void CreateSnake() 
        {
            for (int i = 0; i < 4; i++)
            {
                Border br = new Border();
                br.Width = size;
                br.Height = size;
                //初始移動方向爲向右
                br.Tag = "Right";
                if (i == 0)
                {
                    //蛇頭
                    br.Background = Brushes.Black;
                }
                else {
                    //
                    br.Background = Brushes.Green;
                }
                br.CornerRadius = new CornerRadius(20);
                // 位置
                Canvas.SetLeft(br,(geziNumX/2)*size-i*size);
                Canvas.SetTop(br, (geziNumY/2)*size);
                continer.Children.Add(br);
                snake.Add(br);
            }
        }


        //創建食物
        private void CreateDouDou()
        {
            
            food.Width = food.Height = size;
            //食物顏色
            food.Background = new RadialGradientBrush(Colors.Red, Colors.Red);
            food.CornerRadius = new CornerRadius(5);
            //隨機位置
            Canvas.SetLeft(food,ra.Next(geziNumX)*size);
            Canvas.SetTop(food, ra.Next(geziNumY) * size);
            continer.Children.Add(food);
            
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章