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">
<Grid>
<Canvas x:Name="bg" Margin="0,0,0,0">
<Canvas x:Name="game" Margin="0,0,0,0">
<Button Content="开始游戏" Canvas.Left="800" Canvas.Top="70" Width="75" Height="40" Click="Button_Click" />
<Button Content="结束游戏" Canvas.Left="800" Canvas.Top="152" Width="76" Height="40" RenderTransformOrigin="0.499,2.358" Click="Button_Click_1"/>
</Canvas>
</Canvas>
</Grid>
</Window>
CS
using System;
using System.Collections.Generic;
using System.Linq;
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 打字游戏wpf
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Random ran = new Random(); // 随机数类
List<Label> zimus = new List<Label>(); // 泛型集合字母
List<Image> zidan = new List<Image>(); // 泛型集合子弹
Image play = new Image();
DispatcherTimer tim1 = new DispatcherTimer();
DispatcherTimer tim2 = new DispatcherTimer();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Width = 1000;
this.Height = 800;
this.Background = Brushes.Cyan;
Game.Width = 800;
Game.Height = 600;
Game.Background = Brushes.White;
Canvas.SetLeft(Game,20);
Canvas.SetTop(Game, 20);
play.Width = 100;
play.Height = 100;
play.Source = new BitmapImage(new Uri("../../img/a.jpg", UriKind.Relative));
play.Stretch = Stretch.UniformToFill;
play.Tag = "plane";
Canvas.SetLeft(play, Game.Width / 2 - play.Width / 2);
Canvas.SetTop(play, Game.Height - play.Height);
Game.Children.Add(play);
// 字母生成计时器
tim1.Interval = new TimeSpan(0, 0, 1);
tim1.Tick += Tim1_Tick; ;
// 字母下落,子弹上升计时器
tim2.Interval = new TimeSpan(0, 0, 0, 0, 200);
tim2.Tick += Tim2_Tick; ;
this.KeyDown += MainWindow_KeyDown; ;
}
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
for (int i = 0; i < Game.Children.Count; i++)
{
// 拿到全窗体的Lanel
if (Game.Children[i].GetType().Name == "Label")
{
// 判断字母是否和键盘按下的键对应,并且是Tag值是字母
if (e.Key.ToString() == (Game.Children[i] as Label).Content.ToString()
&& ((Label)Game.Children[i]).Tag.ToString() == "字母"
)
{
// 对应后Tag值改为选中
((Label)Game.Children[i]).Tag = "选中";
Canvas.SetLeft(play, Canvas.GetLeft(Game.Children[i]) + (Game.Children[i] as Label).Width / 2 - play.Width / 2);
// 创建子弹图片Image
Image zidanimg = new Image();
zidanimg.Width = 8;
zidanimg.Height = 20;
zidanimg.Stretch = Stretch.Fill;
zidanimg.Tag = "ZD";
Canvas.SetTop(zidanimg, Canvas.GetTop(play) - play.Height / 2);
// 设置子弹的Lfet在飞机的中间
Canvas.SetLeft(zidanimg, Canvas.GetLeft(play) + play.Width / 2 - zidanimg.Width / 2);
// 设置Image图片路径
zidanimg.Source = new BitmapImage(new Uri("../../img/b.png", UriKind.RelativeOrAbsolute));
Game.Children.Add(zidanimg);
zidan.Add(zidanimg);
break;
}
}
}
}
private void Tim2_Tick(object sender, EventArgs e)
{
// 遍历字母集合
foreach (UIElement zimu in zimus)
{
// 是字母或者是选中的让其下落
if (((Label)zimu).Tag.ToString() == "字母" || ((Label)zimu).Tag.ToString() == "选中")
{
double top = Canvas.GetTop(zimu);
Canvas.SetTop(zimu, top += 10);
// 大于游戏区域消失
if (top >= Game.Height)
{
Game.Children.Remove(zimu);
}
}
// 遍历子弹集合
foreach (UIElement zidan in zidan)
{
// 是子弹就上升
if (((Image)zidan).Tag.ToString() == "ZD")
{
double top = Canvas.GetTop(zidan);
Canvas.SetTop(zidan, top -= 10);
// 出游戏区域消失
if (top <= 0)
{
Game.Children.Remove(zidan);
}
// 遍历字母集合,找到选中的字母
foreach (UIElement zimubi in zimus)
{
if (zimubi.GetType().Name == "Label" && ((Label)zimubi).Tag.ToString() == "选中")
{
// 判断相撞
if (Canvas.GetTop(zidan) <= Canvas.GetTop(zimubi) + ((Label)zimubi).Height &&
Canvas.GetLeft(zidan) + ((Image)zidan).Width / 2 == Canvas.GetLeft(zimubi) + ((Label)zimubi).Width / 2)
{
// 清楚碰撞的字母与子弹
Game.Children.Remove(zimubi);
Game.Children.Remove(zidan);
break;
}
}
}
}
}
}
}
private void Tim1_Tick(object sender, EventArgs e)
{
// 生成字母
Label lab = new Label();
// 随机字母
lab.Content = (char)ran.Next(65, 90);
lab.Width = 30;
lab.Height = 30;
lab.FontSize = 20;
// 统一设置TOP为0
Canvas.SetTop(lab, 0);
// 字母随机颜色
lab.Foreground = new SolidColorBrush(Color.FromRgb(Convert.ToByte(ran.Next(0, 255)), Convert.ToByte(ran.Next(0, 255)), Convert.ToByte(ran.Next(0, 255))));
Canvas.SetLeft(lab, ran.Next(800));
lab.Tag = "字母";
Game.Children.Add(lab);
zimus.Add(lab);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
tim1.Start();
tim2.Start();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
tim1.Stop();
tim2.Stop();
}
}
}