游戲開始界面
游戲開始
游戲簡介:
共有兩條蛇管引,吃到紅色食物加1分梆靖,吃到綠色毒食物減1分,知道0不減在抛;
碰到墻壁游戲結(jié)束钟病,碰到對方游戲結(jié)束,碰到自己游戲結(jié)束
此游戲通過Canvas畫布布局霜定,通過C#代碼實現(xiàn)
游戲主界面大小為1200*1000
界面設計:
<Window x:Class="貪吃蛇__.MainWindow"
? ? ? ? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
? ? ? ? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
? ? ? ? xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
? ? ? ? xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
? ? ? ? xmlns:local="clr-namespace:貪吃蛇__"
? ? ? ? mc:Ignorable="d"
? ? ? ? Title="貪吃蛇" Loaded="Window_Loaded" Height="700" Width="900">
? ? <Canvas Name="BG" Width = "1200" Height = "1000"></Canvas>
</Window>
全局聲明:
? ? ? Random ran = new Random();// 隨機對象
? ? ? ? int size = 20; // 邊框距離BG距離,格子數(shù)
? ? ? ? Canvas gameArea = new Canvas(); // 游戲區(qū)對象
? ? ? ? Border food = new Border(); //食物對象
? ? ? ? Border dufood = new Border(); // 毒食物對象
? ? ? ? List<Border> snakeList1 = new List<Border>(); // 存儲蛇1的泛型
? ? ? ? List<Border> snakeList2 = new List<Border>(); // 存儲蛇2的泛型
? ? ? ? DispatcherTimer moveTimer1 = new DispatcherTimer(); // 蛇1移動定時器
? ? ? ? DispatcherTimer moveTimer2 = new DispatcherTimer(); // 蛇2移動定時器
? ? ? ? SoundPlayer EatFm = new SoundPlayer(@"../../music/EatFood.wav"); // 吃食物音樂
? ? ? ? SoundPlayer EatDuFm = new SoundPlayer(@"../../music/EatDufood.wav");// 吃毒食物音樂
? ? ? ? SoundPlayer Overm = new SoundPlayer(@"../../music/Over.wav"); // 游戲結(jié)束音樂
? ? ? ? SoundPlayer BGm = new SoundPlayer(@"../../music/BGmusic.wav"); // 背景音樂
? ? ? ? int score = 0; //記錄分數(shù)
? ? ? ? Label feng = new Label(); // 分數(shù)對象
? ? ? ? bool Pand = true; // 游戲開始和暫停判斷
加載事件:
private void Window_Loaded(object sender, RoutedEventArgs e)
? ? ? ? {
? ? ? ? ? ? this.Background = Brushes.Cyan;
? ? ? ? ? ? this.WindowState = WindowState.Maximized; // 屏幕最大化
? ? ? ? ? ? BG.Background = new LinearGradientBrush(Colors.Yellow, Colors.Pink, 10);
? ? ? ? ? ? // 邊框
? ? ? ? ? ? Border bianKuang = new Border();
? ? ? ? ? ? bianKuang.Width = 1040;
? ? ? ? ? ? bianKuang.Height = 840;
? ? ? ? ? ? Canvas.SetLeft(bianKuang, size);
? ? ? ? ? ? Canvas.SetTop(bianKuang, size);
? ? ? ? ? ? bianKuang.BorderThickness = new Thickness(20); // 邊框厚度
? ? ? ? ? ? bianKuang.CornerRadius = new CornerRadius(10);? // 畫圓
? ? ? ? ? ? bianKuang.BorderBrush = Brushes.Green;
? ? ? ? ? ? BG.Children.Add(bianKuang);
? ? ? ? ? ? Image map = new Image();
? ? ? ? ? ? map.Source = new BitmapImage(new Uri("../../img/BGimg.png",UriKind.Relative));
? ? ? ? ? ? ImageBrush mp = new ImageBrush();
? ? ? ? ? ? mp.ImageSource = map.Source; // 顯示圖片
? ? ? ? ? ? // 游戲區(qū)
? ? ? ? ? ? gameArea.Width = 1000;
? ? ? ? ? ? gameArea.Height = 800;
? ? ? ? ? ? gameArea.Background = mp;
? ? ? ? ? ? mp.Stretch = Stretch.Fill;
? ? ? ? ? ? Canvas.SetLeft(gameArea, 2 * size);
? ? ? ? ? ? Canvas.SetTop(gameArea, 2 * size);
? ? ? ? ? ? BG.Children.Add(gameArea);
? ? ? ? ? ? // 得分對象
? ? ? ? ? ? feng.Width = 120;
? ? ? ? ? ? feng.Height = 50;
? ? ? ? ? ? Canvas.SetLeft(feng, 1060);
? ? ? ? ? ? Canvas.SetTop(feng, 100);
? ? ? ? ? ? feng.Background = Brushes.Coral;
? ? ? ? ? ? feng.Foreground = Brushes.DeepSkyBlue;
? ? ? ? ? ? feng.FontFamily = new FontFamily("楷體");
? ? ? ? ? ? feng.Content = "? 得分:" + score + "分";
? ? ? ? ? ? feng.FontSize = 20;
? ? ? ? ? ? BG.Children.Add(feng);
? ? ? ? ? ? // 蛇1移動定時器
? ? ? ? ? ? moveTimer1.Tick += MoveTimer1_Tick; ;
? ? ? ? ? ? moveTimer1.Interval = TimeSpan.FromMilliseconds(100);
? ? ? ? ? // 蛇2移動定時器
? ? ? ? ? ? moveTimer2.Tick += MoveTimer2_Tick;
? ? ? ? ? ? moveTimer2.Interval = TimeSpan.FromMilliseconds(100);
? ? ? ? ? ? // 開始按鈕
? ? ? ? ? ? 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.DarkKhaki;
? ? ? ? ? ? Kaise.Click += Kaise_Click;
? ? ? ? ? ? BG.Children.Add(Kaise);
? ? ? ? ? ? // 暫停按鈕
? ? ? ? ? ? Button Zanting = new Button();
? ? ? ? ? ? Zanting.Cursor = Cursors.Hand; // 鼠標移動到按鈕處變?yōu)槭中?/p>
? ? ? ? ? ? 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;
? ? ? ? ? ? BG.Children.Add(Zanting);
? ? ? ? ? ? // 背景音樂播放
? ? ? ? ? ? BGm.Play();
? ? ? ? }
以上為部分代碼档悠,全部代碼查看: