1、XAML代码
<Window x:Class="SnakeTest2.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:SnakeTest2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="MainWindow_OnLoaded">
<Canvas Name="CanvasAll">
<Canvas Name="CanvasLeft"></Canvas>
<Canvas Name="CanvasRight"></Canvas>
</Canvas>
</Window>
2、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 SnakeTest2
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// 设置窗体最大化
this.WindowState = WindowState.Maximized;
// 去掉窗体样式
this.WindowStyle = WindowStyle.None;
}
//初始化游戏开始、暂停按钮
Button btn = new Button();
//创建计时器
DispatcherTimer timerStart = new DispatcherTimer();
//实现计分板
Label lab = new Label();
//创建得分
private int result = 0;
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
// 设置容器大小等于窗体
CanvasAll.Width = this.Width;
CanvasAll.Height = this.Height;
// 设置游戏区域样式
CanvasLeft.Width = 0.75 * CanvasAll.Width;
CanvasLeft.Height = CanvasAll.Height;
CanvasLeft.Background = new LinearGradientBrush(Colors.Chartreuse, Colors.Orange,45);
Canvas.SetLeft(CanvasLeft,0);
// 设置控制区域大小
CanvasRight.Width = 0.25 * CanvasAll.Width;
CanvasRight.Height = CanvasAll.Height;
CanvasRight.Background = Brushes.Aquamarine;
Canvas.SetLeft(CanvasRight, Canvas.GetLeft(CanvasLeft)+CanvasLeft.Width);
// 创建一条蛇
creatSnake(50,snakeList);
// 创建食物
creatFood(50);
// 鼠标按下
this.KeyDown += MainWindow_KeyDown;
timerStart.Interval = TimeSpan.FromMilliseconds(400);
timerStart.Tick += TimerStart_Tick;
btn.Width = CanvasRight.Width;
btn.Height = 100;
btn.FontSize = 32;
btn.FontFamily = new FontFamily("楷体");
btn.Content = "游戏开始";
btn.Background = Brushes.CornflowerBlue;
btn.Click += Btn_Click;
CanvasRight.Children.Add(btn);
lab.Width = CanvasRight.Width;
lab.Height = 100;
lab.FontSize = 32;
lab.Foreground = Brushes.PaleVioletRed;
lab.HorizontalAlignment = HorizontalAlignment.Center;
lab.FontFamily = new FontFamily("楷体");
lab.Background = Brushes.Aquamarine;
Canvas.SetTop(lab, 100);
Canvas.SetLeft(lab, 120);
CanvasRight.Children.Add(lab);
}
///
/// 按钮点击事件
///
///
///
private void Btn_Click(object sender, RoutedEventArgs e)
{
if (btn.Content == "游戏开始")
{
timerStart.Start();
btn.Content = "游戏暂停";
}
else
{
timerStart.Stop();
btn.Content = "游戏开始";
}
}
///
/// 计时器事件
///
///
///
private void TimerStart_Tick(object sender, EventArgs e)
{
moveSnake(snakeList, 50);
eatFood(snakeList,50);
overSnake(snakeList);
lab.Content = "总分:" + result;
}
///
/// 蛇移动的方法
///
///
protected void moveSnake(List<Border> snake, int size)
{
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;
}
}
///
/// 鼠标按下事件
///
///
///
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
// MessageBox.Show(e.Key.ToString());
switch (e.Key)
{
case Key.Right:
snakeList[0].Tag = "Right";
break;
case Key.Left:
snakeList[0].Tag = "Left";
break;
case Key.Up:
snakeList[0].Tag = "Up";
break;
case Key.Down:
snakeList[0].Tag = "Down";
break;
}
}
// 创建代表蛇的泛型List
List<Border> snakeList = new List<Border>();
// 创建随机数对象
Random ra = new Random();
///
/// 创建蛇的方法
///
///
protected void creatSnake(int size,List<Border> snake)
{
for (int i = 0; i < 4; i++)
{
Border cSnake = new Border();
cSnake.Width = cSnake.Height = size;
cSnake.Tag = "Right";
if (i == 0)
{
cSnake.Background = Brushes.Red;
}
else
{
cSnake.Background = Brushes.Blue;
}
Canvas.SetLeft(cSnake,400-i*size);
Canvas.SetTop(cSnake,200);
cSnake.CornerRadius = new CornerRadius(size);
CanvasLeft.Children.Add(cSnake);
snake.Add(cSnake);
}
}
Border cFood = new Border();
///
/// 创建食物的方法
///
///
protected void creatFood(int size)
{
cFood.Width = cFood.Height = size;
cFood.Background = Brushes.DarkViolet;
Canvas.SetLeft(cFood, ra.Next(Convert.ToInt32(CanvasLeft.Width / size)) * size);
Canvas.SetTop(cFood,ra.Next(Convert.ToInt32(CanvasLeft.Height / size)) * size);
cFood.CornerRadius = new CornerRadius(size);
CanvasLeft.Children.Add(cFood);
}
///
/// 吃食物
///
///
///
protected void eatFood(List<Border> snake, int size)
{
if (Canvas.GetLeft(snake[0]) == Canvas.GetLeft(cFood) && Canvas.GetTop(snake[0]) == Canvas.GetTop(cFood))
{
++result;
SoundPlayer player = new SoundPlayer();
player.SoundLocation = "../../sounds/三全音.wav";
player.Play();
Canvas.SetLeft(cFood, ra.Next(Convert.ToInt32(CanvasLeft.Width / size)) * size);
Canvas.SetTop(cFood, ra.Next(Convert.ToInt32(CanvasLeft.Height / size)) * size);
// 创建一节蛇
Border aSnake = new Border();
aSnake.Width = aSnake.Height = size;
aSnake.Background = Brushes.Green;
//判断方向
Canvas.SetLeft(aSnake,Canvas.GetLeft(snake[snake.Count-1]));
Canvas.SetTop(aSnake,Canvas.GetTop(snake[snake.Count-1]));
aSnake.Tag = "";
aSnake.CornerRadius = new CornerRadius(size);
CanvasLeft.Children.Add(aSnake);
snake.Add(aSnake);
}
}
///
/// 蛇死亡
///
protected void overSnake(List<Border> snake)
{
if (Canvas.GetLeft(snake[0])<=0 || Canvas.GetLeft(snake[0])>= CanvasLeft.Width-snake[0].Width || Canvas.GetTop(snake[0]) <= 0 || Canvas.GetTop(snake[0]) >= CanvasLeft.Height - snake[0].Height)
{
SoundPlayer player = new SoundPlayer();
player.SoundLocation = "../../sounds/MyBomb.wav";
player.Play();
MessageBox.Show("您已碰墙,游戏结束");
Environment.Exit(-1);
}
for (int i = snake.Count - 1; i > 0; i--)
{
if (Canvas.GetLeft(snake[0]) == Canvas.GetLeft(snake[i]) && Canvas.GetTop(snake[0]) == Canvas.GetTop(snake[i]))
{
SoundPlayer player = new SoundPlayer();
player.SoundLocation = "../../sounds/MyBomb.wav";
player.Play();
MessageBox.Show("您已碰身,游戏结束");
//强行关闭
Environment.Exit(-1);
}
}
}
}
}