一、贪吃蛇注册界面:a、b玩家注册用户名,用户名不能为空,不能为纯数字,玩家用户名不能一样,并且注册成功后完成与游戏界面的跳转
1、页面设计
<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" WindowStartupLocation="CenterScreen" WindowStyle="None" Width="540" Height="810" Loaded="Window_Loaded">
<Canvas Width="540" Height="810">
<Image Source="18426340.jpg" Width="540" Height="810"/>
<Button Content="开始游戏" FontSize="30" Background="Yellow" Canvas.Left="184" Canvas.Top="657" Width="166" Height="47" Click="Button_Click"/>
<Button Content="退出游戏" FontSize="30" Background="OrangeRed" Canvas.Left="184" Canvas.Top="709" Width="166" Height="48" Click="Button_Click_1"/>
<Canvas Height="90" Canvas.Left="184" Canvas.Top="520" Width="166" Background="Yellow"/>
<TextBox Name="a" Width="166" Canvas.Left="184" Canvas.Top="520" Height="45" FontSize="15">
<TextBox.Resources>
<VisualBrush x:Key="HintText" TileMode="None" Opacity="0.5" Stretch="None" AlignmentX="Center" AlignmentY="Center">
<VisualBrush.Visual>
<TextBlock FontStyle="Italic" Text="A玩家请输入用户名"/>
</VisualBrush.Visual>
</VisualBrush>
</TextBox.Resources>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" Value="{StaticResource HintText}"/>
</Trigger>
<Trigger Property="Text" Value="">
<Setter Property="Background" Value="{StaticResource HintText}"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<TextBox Name="b" Width="166" Canvas.Left="184" Canvas.Top="565" Height="45" FontSize="15">
<TextBox.Resources>
<VisualBrush x:Key="HintText" TileMode="None" Opacity="0.5" Stretch="None" AlignmentX="Center" AlignmentY="Center">
<VisualBrush.Visual>
<TextBlock FontStyle="Italic" Text="B玩家请输入用户名"/>
</VisualBrush.Visual>
</VisualBrush>
</TextBox.Resources>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" Value="{StaticResource HintText}"/>
</Trigger>
<Trigger Property="Text" Value="">
<Setter Property="Background" Value="{StaticResource HintText}"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<Button Name="zhuce" FontSize="30" Content="注册" Canvas.Left="184" Canvas.Top="610" Width="166" Height="45" Background="Yellow" Click="Button_Click_2"/>
</Canvas>
</Window>
2、功能实现:用户名的验证,开始及退出游戏,页面跳转
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
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.IO;
namespace 贪吃蛇
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
//用户名合法后开始游戏
private void Button_Click(object sender, RoutedEventArgs e)
{
if (zhuce.Content.ToString() == "注册成功")
{
Window1 ppp = new Window1();
this.Hide();
ppp.Show();
//写入
FileStream file = new FileStream("贪吃蛇.txt", FileMode.Append, FileAccess.Write);
StreamWriter writer = new StreamWriter(file);
writer.Write("|"+a.Text);
writer.Write("|" + b.Text );
writer.Close();
}
}
//退出游戏
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
//点击注册,判断用户名是否合法
bool aa = true;
bool bb = true;
private void Button_Click_2(object sender, RoutedEventArgs e)
{
string pattern = @"^\d+$";
while (aa)
{
if (string.IsNullOrWhiteSpace(a.Text))
{
MessageBox.Show("玩家A的姓名不能为空,请重新输入");
a.Text = "";
}
else if (Regex.IsMatch(a.Text, pattern))
{
MessageBox.Show("玩家A的姓名不能是纯数字,请重新输入");
a.Text = "";
}
else
{
aa = false;
}
break;
}
while (bb)
{
if (string.IsNullOrWhiteSpace(a.Text))
{
MessageBox.Show("玩家B的姓名不能为空,请重新输入");
b.Text = "";
}
else if (Regex.IsMatch(b.Text, pattern))
{
MessageBox.Show("玩家B的姓名不能是纯数字,请重新输入");
b.Text = "";
}
else if (a.Text==b.Text)
{
MessageBox.Show("玩家B的姓名不能与玩家A相同,请重新输入");
b.Text = "";
}
else
{
bb = false;
}
break;
}
if (aa==false&& bb==false)
{
zhuce.Content = "注册成功";
}
}
}
}
二、游戏界面,两条蛇两个食物,分别用wads跟方向键控制蛇的移动,蛇碰墙,碰自己,碰另一条蛇都判定为死亡,分别记录两条蛇的得分,做一统计
1、页面设计
<Window x:Class="贪吃蛇.Window1"
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="Window1" Name="youxi" WindowStyle="None" WindowStartupLocation="CenterScreen" WindowState="Maximized" AllowsTransparency="True" Background="Transparent" Loaded="Youxi_Loaded" KeyDown="Youxi_KeyDown" >
<DockPanel Name="bg" >
<Canvas Name="game">
<Canvas Name="map"></Canvas>
<Border Name="bd" ></Border>
</Canvas>
<Canvas Name="meu"></Canvas>
</DockPanel>
</Window>
2、功能实现:控制蛇的移动、蛇吃食物;蛇的速度为3档,50分一下一档,50-100一档,100往上一档;空格暂停/继续游戏,Esc退出游戏;得分统计,速度展示;一分食物,两分食物显示效果,蛇身显示动画;与结束界面及注册界面的跳转…
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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.IO;
namespace 贪吃蛇
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
//枚举,记录蛇的方向
enum SnakeDirction
{
Left,
Right,
Up,
Down
}
enum SnakeDirction2
{
Left,
Right,
Up,
Down
}
public Window1()
{
InitializeComponent();
}
DispatcherTimer T = new DispatcherTimer();
TextBox txt = new TextBox();
TextBox txt2 = new TextBox();
TextBox txt3 = new TextBox();
TextBox txt4 = new TextBox();
TextBox txt5 = new TextBox();
int fenshu = 0;
int fenshu2 = 0;
//int zongfen = 0;
SoundPlayer Sound = new SoundPlayer(@"E:\VS\跳转贪吃蛇\贪吃蛇\bg.wav");
SoundPlayer over = new SoundPlayer(@"E:\VS\跳转贪吃蛇\贪吃蛇\7896.wav");
bool b1 = true;//第一条蛇活着
bool b2 = true;//第二条蛇活着
private void Youxi_Loaded(object sender, RoutedEventArgs e)
{
Sound.PlayLooping();
bg.Width = this.Width;
bg.Height = this.Height;
//bg.Background = Brushes.Yellow;
game.Width = 0.9 * bg.Width;
game.Height = bg.Height;
//game.Background = Brushes.Blue;
meu.Background = new RadialGradientBrush(Colors.Yellow, Colors.OrangeRed);
bd.Width = game.Width;
bd.Height = game.Height;
bd.BorderThickness = new Thickness(20);
bd.BorderBrush = Brushes.Red;
bd.Opacity = 1;
Canvas.SetLeft(bd, 0);
Canvas.SetTop(bd, 0);
map.Width = (Convert.ToInt32(game.Width / 20) - 2) * 20;
map.Height = (Convert.ToInt32(game.Height / 20) - 2) * 20;
Canvas.SetLeft(map, 20);
Canvas.SetTop(map, 20);
map.Background = new LinearGradientBrush(Colors.OrangeRed, Colors.Yellow, 45);
map.Opacity = 0.8;
SnakeCreate();
T.Interval = new TimeSpan(0, 0, 0, 0, 120);
T.Tick += T_Tick;
T.Start();
lie = Convert.ToInt32(map.Width / ssize);
hang = Convert.ToInt32(map.Height / ssize);
FoodCreate();
//记录第一条蛇得分
txt.Background = Brushes.Transparent;
txt.BorderThickness