基于C# WPF框架的贪吃蛇
2021-01-22 15:12
标签:hand muse ISE names music 对象 ade 大小 停止
共有两条蛇,吃到红色食物加1分,吃到绿色毒食物减1分,知道0不减; 此游戏通过Canvas画布布局,通过C#代码实现 ##### 键盘事件 基于C# WPF框架的贪吃蛇 标签:hand muse ISE names music 对象 ade 大小 停止 原文地址:https://www.cnblogs.com/lonelyxmas/p/12079057.html游戏开始界面
游戏开始
碰到墙壁游戏结束,碰到对方游戏结束,碰到自己游戏结束
游戏主界面大小为1200*1000
全局声明:
Random ran = new Random();// 随机对象
int size = 20; // 边框距离BG距离,格子数
Canvas gameArea = new Canvas(); // 游戏区对象
Border food = new Border(); //食物对象
Border dufood = new Border(); // 毒食物对象
List
加载事件:
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; // 显示图片
// 游戏区
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; // 鼠标移动到按钮处变为手型
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();
}
开启游戏按钮
// 开启游戏
private void Kaise_Click(object sender, RoutedEventArgs e)
{
BGm.Stop(); // 背景音乐停止
this.KeyDown += MainWindow_KeyDown;
if (Pand == true)
{
CreateSnake1(); // 生成蛇1方法
CreateSnake2(); // 生成蛇2方法
CreateFood(); // 生成食物
CReateDufood();// 生成毒食物
}
moveTimer1.Start(); // 蛇1移动
moveTimer2.Start();// 蛇2移动
Pand = false;
}
暂停按钮
// 暂停游戏
private void Zanting_Click(object sender, RoutedEventArgs e)
{
this.KeyDown -= MainWindow_KeyDown;
moveTimer1.Stop(); // 蛇1移动
moveTimer2.Stop();// 蛇2移动
Pand = false;
}
创建食物方法:
// 创建食物
private void CreateFood()
{
food.Width = food.Height = size;
food.Background = Brushes.Red;
food.CornerRadius = new CornerRadius(20);
Canvas.SetLeft(food, ran.Next(1, 50) * size);
Canvas.SetTop(food, ran.Next(1, 40) * size);
// 判断食物在蛇位置上
for (int i = 0; i
创建毒食物方法:
// 创建毒食物
private void CReateDufood()
{
dufood.Width = dufood.Height = size;
dufood.Background = Brushes.GreenYellow;
dufood.CornerRadius = new CornerRadius(20);
Canvas.SetLeft(dufood, ran.Next(1, 50) * size);
Canvas.SetTop(dufood, ran.Next(1, 40) * size);
// 毒食物与食物位置重合
if (Canvas.GetLeft(dufood)==Canvas.GetLeft(food)&&Canvas.GetTop(dufood)==Canvas.GetTop(food))
{
Canvas.SetLeft(dufood, ran.Next(1, 50) * size);
Canvas.SetTop(dufood, ran.Next(1, 40) * size);
}
// 毒食物在蛇身上
for (int i = 0; i
蛇1创建方法:
// 创建蛇1方法
private void CreateSnake1()
{
int lenght = 4; // 默认蛇长度
for (int i = 0; i
蛇2创建方法:
// 创建蛇2方法
private void CreateSnake2()
{
int length = 4;
for (int i = 0; i
蛇1移动
// 蛇1移动定时器
private void MoveTimer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i 0; i--)
{
// 蛇身跟随蛇头,占蛇头位置
snakeList1[i].Tag = snakeList1[i - 1].Tag;
}
// 蛇碰到墙
if (Canvas.GetTop(snakeList1[0]) 0&&Canvas.GetTop(snakeList1[0]) == Canvas.GetTop(food) && Canvas.GetLeft(snakeList1[0]) == Canvas.GetLeft(food))
{
gameArea.Children.Remove(food); //删除吃的食物
CreateFood(); // 重新创建食物
EatFm.Play(); // 吃食物的音乐
//新创建的蛇身
Border Slimuse = new Border();//增添蛇身
Slimuse.Width = Slimuse.Height = size;
Slimuse.Tag = " ";
Slimuse.Background = Brushes.Purple;
Slimuse.CornerRadius = new CornerRadius(20); // 变圆
Canvas.SetLeft(Slimuse, Canvas.GetLeft(snakeList1[snakeList1.Count - 1])); // 获取到蛇尾的Left位置
Canvas.SetTop(Slimuse, Canvas.GetTop(snakeList1[snakeList1.Count - 1])); // 获取到蛇尾的Top位置
gameArea.Children.Add(Slimuse);
snakeList1.Add(Slimuse);
score++;
feng.Content = " 得分:" + score + "分";
}
//吃到毒食物
if (snakeList1.Count > 0 && Canvas.GetTop(snakeList1[0]) == Canvas.GetTop(dufood) && Canvas.GetLeft(snakeList1[0]) == Canvas.GetLeft(dufood))
{
EatDuFm.Play(); // 吃毒食物音乐
gameArea.Children.Remove(dufood);// 删掉吃的毒食物
CReateDufood(); // 重新创建毒食物
if (score > 0)
{
score--;
feng.Content = " 得分:" + score + "分";
}
gameArea.Children.Remove(snakeList1[snakeList1.Count - 1]); // 清除蛇身最后一节
snakeList1.Remove(snakeList1[snakeList1.Count - 1]); // 清除集合最后一节
}
// 蛇头碰到蛇身体
for (int i = 1; i
蛇2移动
// 蛇2移动定时器
private void MoveTimer2_Tick(object sender, EventArgs e)
{
for (int i = 0; i 0; i--)
{
// 蛇身跟随蛇头移动
snakeList2[i].Tag = snakeList2[i - 1].Tag;
}
// 蛇2碰到墙
if (Canvas.GetLeft(snakeList2[0]) = gameArea.Width || Canvas.GetTop(snakeList2[0]) = gameArea.Height)
{
GameOver();// 调用游戏结束方法
}
// 蛇2碰到蛇1
for (int x = 0; x 0&& Canvas.GetLeft(snakeList2[0]) == Canvas.GetLeft(food) && Canvas.GetTop(snakeList2[0]) == Canvas.GetTop(food))
{
gameArea.Children.Remove(food); // 删除食物
CreateFood(); // 创建新食物
EatFm.Play();// 吃食物音乐
Border Slom = new Border();// 添加的蛇身 创建新蛇身
Slom.Tag = " ";
Slom.Width = Slom.Height = size;
Slom.Background = Brushes.Orange;
Slom.CornerRadius = new CornerRadius(20);
Canvas.SetLeft(Slom, Canvas.GetLeft(snakeList2[snakeList2.Count - 1]));
Canvas.SetTop(Slom, Canvas.GetTop(snakeList2[snakeList2.Count - 1]));
snakeList2.Add(Slom);
gameArea.Children.Add(Slom);
score++;
feng.Content = " 得分:" + score + "分";
}
// 吃到毒食物
if (snakeList2.Count > 0&&Canvas.GetLeft(snakeList2[0])==Canvas.GetLeft(dufood)&&Canvas.GetTop(snakeList2[0])==Canvas.GetTop(dufood))
{
EatDuFm.Play(); // 吃毒食物音乐
gameArea.Children.Remove(dufood); // 消除毒食物
CReateDufood(); // 创建新毒食物
if (score>0)
{
score--;
feng.Content = " 得分:" + score + "分";
}
gameArea.Children.Remove(snakeList2[snakeList2.Count - 1]);
snakeList2.Remove(snakeList2[snakeList2.Count - 1]);
}
// 蛇头碰到蛇身
for (int i = 1; i
游戏结束方法:
// 游戏结束方法
private void GameOver()
{
this.KeyDown -= MainWindow_KeyDown;
moveTimer1.Stop();
moveTimer2.Stop();
gameArea.Children.Clear(); // 清除游戏区子级
// Overm.Play(); // 游戏结束音乐
MessageBoxResult Out = MessageBox.Show("游戏结束,得分为:" + score + ",是否重新开始游戏", "游戏结束", MessageBoxButton.YesNo, MessageBoxImage.Asterisk);
if (Out == MessageBoxResult.Yes)
{
snakeList1.Clear(); // 清除存储蛇1的泛型
snakeList2.Clear(); // 清除存储蛇2的泛型
Pand = true;
score = 0;
feng.Content = " 得分:" + score + "分";
}
else
{
this.Close();
}
}
csharp // 键盘事件 private void MainWindow_KeyDown(object sender, KeyEventArgs e) { switch (e.Key) { case Key.Left: snakeList1[0].Tag = "Left"; break; case Key.Right: snakeList1[0].Tag = "Right"; break; case Key.Up: snakeList1[0].Tag = "Up"; break; case Key.Down: snakeList1[0].Tag = "Down"; break; case Key.A: snakeList2[0].Tag = "Left"; break; case Key.D: snakeList2[0].Tag = "Right"; break; case Key.W: snakeList2[0].Tag = "Up"; break; case Key.S: snakeList2[0].Tag = "Down"; break; default: break; } }