C#WPF贪吃蛇源码分享,C#贪吃蛇教程分享
117906 ワード
有毒食品と加点食品に分けて、有毒食品は食べた後に相手の1つの長さを減らして、スペースボタンは加速ボタンで、壁にぶつかって検査して、自分で検査します
コードを差し上げますが、足りないところはご指摘ください.
CSプログラムコード
コードを差し上げますが、足りないところはご指摘ください.
CSプログラムコード
using System; using System.Collections.Generic; using System.Linq; using System.Media; 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; namespace { ///
/// MainWindow.xaml /// public partial class MainWindow : Window { #region public MainWindow() { InitializeComponent(); // this.WindowStyle = WindowStyle.None; // this.AllowsTransparency = true; // this.WindowState = WindowState.Maximized; } Canvas Game = new Canvas(); // const int row = 30, col = 40; // ,row ,col Image food,dufood; // Random ran = new Random(); // DispatcherTimer SankMOve = new DispatcherTimer(); // const double Sk_size = 20; // List<Image> SnakeList = new List<Image>(); // Label Score = new Label(); SoundPlayer foodmic = new SoundPlayer("../../mic/ .wav"); SoundPlayer over = new SoundPlayer("../../mic/ .wav"); SoundPlayer dufoodmic = new SoundPlayer("../../mic/ .wav"); SoundPlayer zhuang = new SoundPlayer("../../mic/ .wav"); List<Image> snaksTwolist = new List<Image>(); #endregion private void Window_Loaded(object sender, RoutedEventArgs e) { MessageBox.Show(" (Enter) , "," ",MessageBoxButton.OK,MessageBoxImage.Information); // this.Opacity = 0.8; // BG.Background = new LinearGradientBrush(Colors.White, Colors.Aqua,90); // Snakes(SnakeList,300); Snakes(snaksTwolist, 500); BG.Width = this.Width; BG.Height = this.Height; // SankMOve.Interval = new TimeSpan(0,0,0,0,100); SankMOve.Tick += SankMOve_Tick; this.KeyDown += ; this.KeyUp += ; Food(); DuFood(); // Game.Width = Sk_size * col; Game.Height = Sk_size * row; // Game Image baimg = new Image(); baimg.Source = new BitmapImage(new Uri("../../img/ .jpg", UriKind.RelativeOrAbsolute)); ImageBrush backimg = new ImageBrush(); backimg.ImageSource = baimg.Source; Game.Background = backimg; Canvas.SetLeft(Game, (BG.Width - Game.Width) / 2); Canvas.SetTop(Game, (BG.Height - Game.Height) / 2); BG.Children.Add(Game); // Image scoreImg = new Image(); scoreImg.Source = new BitmapImage(new Uri("/img/ .png", UriKind.RelativeOrAbsolute)); scoreImg.Stretch = Stretch.Fill; scoreImg.Width = 200; scoreImg.Height = 100; Canvas.SetLeft(scoreImg, 20); Canvas.SetTop(scoreImg, 100); BG.Children.Add(scoreImg); Score.FontSize = 35; Canvas.SetLeft(Score, scoreImg.Width/2); Canvas.SetTop(Score,130); BG.Children.Add(Score); } private void (object sender, KeyEventArgs e) { if (e.Key==Key.Space) { SankMOve.Interval = new TimeSpan(0, 0, 0, 0, 100); } } MediaPlayer play = new MediaPlayer(); // ↓ private void Food() { // food = new Image(); // food.Width = food.Height = Sk_size; int isfood=ran.Next(0,2); // if (isfood==0) { food.Source = new BitmapImage(new Uri("/img/ "+ran.Next(0,5)+".png", UriKind.RelativeOrAbsolute)); food.Tag = " "; } else { food.Source = new BitmapImage(new Uri("/img/ .png", UriKind.RelativeOrAbsolute)); food.Tag = " "; } food.Stretch = Stretch.Fill; // Canvas.SetLeft(food,ran.Next(1,40)*Sk_size); Canvas.SetTop(food,ran.Next(1,30)*Sk_size); Game.Children.Add(food); } private void DuFood() { // dufood = new Image(); // dufood.Width = food.Height = Sk_size; int isfood = ran.Next(0, 5); // if (isfood == 0) { dufood.Source = new BitmapImage(new Uri("/img/ .png", UriKind.RelativeOrAbsolute)); dufood.Tag = " "; } else { dufood.Source = new BitmapImage(new Uri("/img/ " + ran.Next(0, 5) + ".png", UriKind.RelativeOrAbsolute)); dufood.Tag = " "; } dufood.Stretch = Stretch.Fill; // Canvas.SetLeft(dufood, ran.Next(1, 40) * Sk_size); Canvas.SetTop(dufood, ran.Next(1, 30) * Sk_size); Game.Children.Add(dufood); } int defen = 0; private void (object sender, KeyEventArgs e) { #region Tag switch (e.Key) { #region case Key.Space: SankMOve.Interval = new TimeSpan(0, 0, 0, 0, 30); break; // case Key.Enter: if (SankMOve.IsEnabled == true) { SankMOve.Stop(); } else { SankMOve.Start(); play.Open(new Uri(@"../../mic/BG .wav", UriKind.Relative)); play.MediaEnded += Play_MediaEnded; play.Play(); } break; #endregion #region case Key.Left: if (SnakeList[0].Tag.ToString()!="Right") { // RotateTransform transform = new RotateTransform(270,Sk_size/2,Sk_size/2); SnakeList[0].RenderTransform = transform; SnakeList[0].Tag = "Left"; } break; case Key.Right: if (SnakeList[0].Tag.ToString() != "Left") { // RotateTransform transform = new RotateTransform(90, Sk_size / 2, Sk_size / 2); SnakeList[0].RenderTransform = transform; SnakeList[0].Tag = "Right"; } break; case Key.Up: if (SnakeList[0].Tag.ToString()!="Down") { // RotateTransform transform = new RotateTransform(-360, Sk_size / 2, Sk_size / 2); SnakeList[0].RenderTransform = transform; SnakeList[0].Tag = "Up"; } break; case Key.Down: if (SnakeList[0].Tag.ToString() != "Up") { // RotateTransform transform = new RotateTransform(180, Sk_size / 2, Sk_size / 2); SnakeList[0].RenderTransform = transform; SnakeList[0].Tag = "Down"; } break; #endregion #region case Key.A: if (snaksTwolist[0].Tag.ToString() != "Right") { // RotateTransform transform = new RotateTransform(270, Sk_size / 2, Sk_size / 2); snaksTwolist[0].RenderTransform = transform; snaksTwolist[0].Tag = "Left"; } break; case Key.D: if (snaksTwolist[0].Tag.ToString() != "Left") { // RotateTransform transform = new RotateTransform(90, Sk_size / 2, Sk_size / 2); snaksTwolist[0].RenderTransform = transform; snaksTwolist[0].Tag = "Right"; } break; case Key.W: if (snaksTwolist[0].Tag.ToString() != "Down") { // RotateTransform transform = new RotateTransform(-360, Sk_size / 2, Sk_size / 2); snaksTwolist[0].RenderTransform = transform; snaksTwolist[0].Tag = "Up"; } break; case Key.S: if (SnakeList[0].Tag.ToString() != "Up") { // RotateTransform transform = new RotateTransform(180, Sk_size / 2, Sk_size / 2); snaksTwolist[0].RenderTransform = transform; snaksTwolist[0].Tag = "Down"; } break; #endregion default: break; } #endregion } // private void Play_MediaEnded(object sender, EventArgs e) { play.Position = new TimeSpan(0); } // private void SankMOve_Tick(object sender, EventArgs e) { SnakesMove(); SnakesTwoMover(); } // ↓ private void SnakesMove() { // for (int i = 0; i < SnakeList.Count; i++) { #region Tag switch (SnakeList[i].Tag.ToString()) { case "Left": Canvas.SetLeft(SnakeList[i], Canvas.GetLeft(SnakeList[i]) - Sk_size); break; case "Right": Canvas.SetLeft(SnakeList[i], Canvas.GetLeft(SnakeList[i]) + Sk_size); break; case "Up": Canvas.SetTop(SnakeList[i], Canvas.GetTop(SnakeList[i]) - Sk_size); break; case "Down": Canvas.SetTop(SnakeList[i], Canvas.GetTop(SnakeList[i]) + Sk_size); break; default: break; } #endregion } // , Tag , for (int i = SnakeList.Count - 1; i > 0; i--) { SnakeList[i].Tag = SnakeList[i - 1].Tag; } Eatfood(SnakeList, snaksTwolist); ZhuangQiang(SnakeList); #region For for (int i = 1; i < SnakeList.Count - 1; i++) { if (Canvas.GetLeft(SnakeList[0 ]) == Canvas.GetLeft(SnakeList[i]) && Canvas.GetTop(SnakeList[0]) == Canvas.GetTop(SnakeList[i])) { // MessageBoxResult result = MessageBox.Show(" , ?", "GAME OVER", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { SnakeList.Clear(); Game.Children.Clear(); Snakes(SnakeList, 300); Food(); } else { SankMOve.Stop(); MessageBox.Show(" , ,BYBY"); this.Close(); } } } #endregion } // string name = null; private void ZhuangQiang(List<Image> SnakeList) { if (Canvas.GetLeft(SnakeList[0]) > Game.Width - 20 || Canvas.GetTop(SnakeList[0]) > Game.Height - 20 || Canvas.GetLeft(SnakeList[0]) < 10 || Canvas.GetTop(SnakeList[0]) < 10) { zhuang.Play(); // if (SnakeList==snaksTwolist) { name = " "; } else { name = " "; } string msg = string.Format("【{0}】 ,【{0}】 , ?", name); MessageBoxResult result = MessageBox.Show(msg, "GAME OVER", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { SnakeList.Clear(); snaksTwolist.Clear(); Game.Children.Clear(); Snakes(SnakeList, 300); Snakes(snaksTwolist, 500); Food(); DuFood(); defen = 0; Score.Content = defen; } else { SankMOve.Stop(); MessageBox.Show(" , ,BYBY"); this.Close(); } } } // private void SnakesTwoMover() { // ↓ for (int i = 0; i < snaksTwolist.Count; i++) { #region Tag switch (snaksTwolist[i].Tag.ToString()) { case "Left": Canvas.SetLeft(snaksTwolist[i], Canvas.GetLeft(snaksTwolist[i]) - Sk_size); break; case "Right": Canvas.SetLeft(snaksTwolist[i], Canvas.GetLeft(snaksTwolist[i]) + Sk_size); break; case "Up": Canvas.SetTop(snaksTwolist[i], Canvas.GetTop(snaksTwolist[i]) - Sk_size); break; case "Down": Canvas.SetTop(snaksTwolist[i], Canvas.GetTop(snaksTwolist[i]) + Sk_size); break; default: break; } #endregion } // , Tag , for (int i = snaksTwolist.Count - 1; i > 0; i--) { snaksTwolist[i].Tag = snaksTwolist[i - 1].Tag; } Eatfood(snaksTwolist,SnakeList); ZhuangQiang(snaksTwolist); } // private void Eatfood(List<Image> SnakeList,List<Image> SnakeListTwo) { if (Canvas.GetLeft(SnakeList[0]) == Canvas.GetLeft(food) && Canvas.GetTop(SnakeList[0]) == Canvas.GetTop(food)) { if (food.Tag.ToString() == " ") { dufoodmic.Play(); defen--; if (SnakeListTwo.Count!=0) { Score.Content = defen; Game.Children.Remove(SnakeListTwo[SnakeListTwo.Count - 1]); SnakeListTwo.Remove(SnakeListTwo[SnakeListTwo.Count - 1]); Game.Children.Remove(food); Food(); } else { string msg = string.Format("【{0}】 ,【{0}】 , ?", name); MessageBoxResult result = MessageBox.Show(msg, "GAME OVER", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { SnakeList.Clear(); snaksTwolist.Clear(); Game.Children.Clear(); Snakes(SnakeList, 300); Snakes(snaksTwolist, 500); Food(); DuFood(); defen = 0; Score.Content = defen; } else { SankMOve.Stop(); MessageBox.Show(" , ,BYBY"); this.Close(); } } } else { foodmic.Play(); defen++; Score.Content = defen; Game.Children.Remove(food); // Image sks = new Image(); sks.Tag = " "; sks.Width = sks.Height = Sk_size; // left 11left sks.Source = new BitmapImage(new Uri("/img/ 4.png", UriKind.RelativeOrAbsolute)); sks.Stretch = Stretch.Fill; Canvas.SetLeft(sks, Canvas.GetLeft(SnakeList[SnakeList.Count - 1])); Canvas.SetTop(sks, Canvas.GetTop(SnakeList[SnakeList.Count - 1])); SnakeList.Add(sks); Game.Children.Add(sks); Food(); } } if (Canvas.GetLeft(SnakeList[0]) == Canvas.GetLeft(dufood) && Canvas.GetTop(SnakeList[0]) == Canvas.GetTop(dufood)) { if (dufood.Tag.ToString() == " ") { dufoodmic.Play(); defen--; Score.Content = defen; Game.Children.Remove(SnakeList[SnakeList.Count - 1]); SnakeList.Remove(SnakeList[SnakeList.Count - 1]); Game.Children.Remove(dufood); DuFood(); } else { foodmic.Play(); defen++; Score.Content = defen; Game.Children.Remove(dufood); // Image sks = new Image(); sks.Tag = " "; sks.Width = sks.Height = Sk_size; // left 11left sks.Source = new BitmapImage(new Uri("/img/ 4.png", UriKind.RelativeOrAbsolute)); sks.Stretch = Stretch.Fill; Canvas.SetLeft(sks, Canvas.GetLeft(SnakeList[SnakeList.Count - 1])); Canvas.SetTop(sks, Canvas.GetTop(SnakeList[SnakeList.Count - 1])); SnakeList.Add(sks); Game.Children.Add(sks); DuFood(); } } } // ↓ private void Snakes(List<Image> SnakeList,int Top) { #region for (int i = 0; i < 4; i++) { Image sks = new Image(); Canvas.SetLeft(sks,500-Sk_size*i); Canvas.SetTop(sks,Top); sks.Width = sks.Height = Sk_size; if (i==0) { sks.Source = new BitmapImage(new Uri("/img/ .png", UriKind.RelativeOrAbsolute)); sks.Stretch = Stretch.Fill; } else { sks.Source = new BitmapImage(new Uri("/img/ 4.png", UriKind.RelativeOrAbsolute)); sks.Stretch = Stretch.Fill; } sks.Tag = "Right"; Game.Children.Add(sks); SnakeList.Add(sks); } #endregion } } }
xaml程序代码
<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="MainWindow" Height="450" Width="800" Loaded="Window_Loaded" >
<Canvas x:Name="BG">
</Canvas>
</Window>