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 WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
List<Label> zimuList = new List<Label>(); // 存储生成的字母
DispatcherTimer timer = new DispatcherTimer(); // 生成字母
DispatcherTimer zmYidtimer = new DispatcherTimer(); // 字母移动定时器
Label dengFeng = new Label(); // 得分对象
Label time = new Label();// 时间对象
DispatcherTimer shitimer = new DispatcherTimer();// 倒计时定时器
double num=0; // 得分参数
double tim = 5; // 时间参数
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Width = 1000;
this.Height = 800;
this.Left = SystemParameters.FullPrimaryScreenWidth / 2 - this.Width / 2;
this.Top = SystemParameters.FullPrimaryScreenHeight / 2 - this.Height / 2;
// 地图下的底板
BG.Width = 1000;
BG.Height = 800;
BG.Background =Brushes.Black;
// 游戏区
Gamequ.Width = 800;
Gamequ.Height = 800;
Gamequ.Background = new SolidColorBrush(Color.FromRgb(80,00,80));
Canvas.SetLeft(Gamequ, 5);
// 控制按钮区
kongzhiqu.Width = 200;
kongzhiqu.Height = 800;
kongzhiqu.Background = new LinearGradientBrush(Colors.Pink,Colors.Yellow,0);
Canvas.SetLeft(kongzhiqu,810);
// 生成字母
timer.Tick += Timer_Tick;
timer.Interval = TimeSpan.FromMilliseconds(500);
// 移动字母
zmYidtimer.Tick += ZmYidtimer_Tick;
zmYidtimer.Interval = TimeSpan.FromMilliseconds(200);
// 开始按钮
Button kaiqi = new Button();
kaiqi.Width = 180;
kaiqi.Height = 50;
Canvas.SetTop(kaiqi, 20);
kaiqi.Background = new RadialGradientBrush(Colors.Blue, Colors.Green);
kaiqi.Content = "开始游戏";
kongzhiqu.Children.Add(kaiqi);
kaiqi.Click += Kaiqi_Click;
// 暂停按钮
Button zanting = new Button();
zanting.Width = 180;
zanting.Height = 50;
Canvas.SetTop(zanting, 80);
zanting.Background = new RadialGradientBrush(Colors.Red, Colors.Blue);
zanting .Content = "暂停游戏";
kongzhiqu.Children.Add(zanting);
zanting.Click += Zanting_Click;
// 得分对象
dengFeng.Width = 180;
dengFeng.Height = 50;
Canvas.SetTop(dengFeng, 150);
dengFeng.Foreground = new LinearGradientBrush(Colors.Pink, Colors.Beige, 0);
dengFeng.Background = Brushes.Gray;
dengFeng.Content="得分为:"+num+"分";
dengFeng.FontFamily = new FontFamily("楷体");
dengFeng.FontSize = 25;
kongzhiqu.Children.Add(dengFeng);
// 时间对象
time.Width = 180;
time.Height = 50;
Canvas.SetTop(time, 210);
time.Background = Brushes.LightGray;
time.Content = " 时间:" + tim+"秒";
time.FontSize = 28;
kongzhiqu.Children.Add(time);
// 倒计时
shitimer.Interval = TimeSpan.FromMilliseconds(1000);
shitimer.Tick += Shitimer_Tick;
}
private void Shitimer_Tick(object sender, EventArgs e)
{
tim--;
time.Content = " 时间:" + tim + "秒";
if (tim<=0)
{
for (int i = 0; i < zimuList.Count; i++)
{
Gamequ.Children.Remove(zimuList[i]); // 消除对应字母
zimuList.Remove(zimuList[i]); // 消除泛型中字母
}
timer.Stop();
zmYidtimer.Stop();
time.Content = " 时间:" + 0 + "秒";
MessageBoxResult Out= MessageBox.Show("得分为:" + num + "分"+",是否重新开始游戏", "游戏结束", MessageBoxButton.YesNo, MessageBoxImage.Information);
if (Out.Equals(MessageBoxResult.Yes))
{
timer.Start(); // 生成字母定时器
zmYidtimer.Start(); // 字母移动定时器
tim = 5;
time.Content = " 时间:" + tim + "秒";
num = 0;
dengFeng.Content = "得分为:" + num + "分";
}
}
}
private void Kaiqi_Click(object sender, RoutedEventArgs e)
{
timer.Start(); // 字母生成
zmYidtimer.Start(); // 字母移动
shitimer.Start(); // 倒计时开启
this.KeyDown += MainWindow_KeyDown; // 开启键盘事件
}
private void Zanting_Click(object sender, RoutedEventArgs e)
{
timer.Stop(); // 字母生成
zmYidtimer.Stop(); // 字母移动
shitimer.Stop(); // 倒计时关闭
this.KeyDown -= MainWindow_KeyDown; // 关闭键盘事件
}
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
// 第一中消失字母方法
/*for (int i = 0; i < Gamequ.Children.Count; i++)
{
if (Gamequ.Children[i].GetType().Name=="Label")
{
if (e.Key.ToString().ToLower()==((Label)Gamequ.Children[i]).Content.ToString())
{
Gamequ.Children.Remove(Gamequ.Children[i]);
MessageBox.Show("QQQ");
}
}
}*/
// 第二种消失字母方法
for (int i = 0; i < zimuList.Count; i++)
{
if (e.Key.ToString().ToLower()==zimuList[i].Content.ToString())
{
//MessageBox.Show("AAA");
Gamequ.Children.Remove(zimuList[i]); // 消除对应字母
zimuList.Remove(zimuList[i]); // 消除泛型中字母
num++;
dengFeng.Content = "得分为:" + num + "分";
break; // 只能执行一次
}
}
}
// 字母移动
private void ZmYidtimer_Tick(object sender, EventArgs e)
{
// 获取每一个生成的字母
// 第一种获取字母的方法 将生成的字母存放到泛型中
for (int i = 0; i < zimuList.Count; i++)
{
double zimTop = Canvas.GetTop(zimuList[i]);
Canvas.SetTop(zimuList[i],zimTop+30);
if (zimTop>Gamequ.Height)
{
// MessageBox.Show("QQQ");
zimuList.Remove(zimuList[i]);
}
}
// 第二种获取字母的方法 给字母添加Name属性
/*foreach (Label item in Gamequ.Children)
{
if (item.Name.ToString()=="zimu")
{
double zimTop = Canvas.GetTop(item);
Canvas.SetTop(item, zimTop += 50);
if (zimTop > Gamequ.Height)
{
Gamequ.Children.Remove(item);
}
}
}*/
}
Random ran = new Random();
private void Timer_Tick(object sender, EventArgs e)
{
Label zimu = new Label(); // 字母对象
zimu.Tag = "";
zimu.Name = "zimu";
zimu.Content = ((char)ran.Next(97, 123)).ToString();
zimu.FontSize = ran.Next(30, 40);
// 随机字母颜色
zimu.Foreground = new SolidColorBrush(Color.FromRgb(Convert.ToByte(ran.Next(255)), Convert.ToByte(ran.Next(255)), Convert.ToByte(ran.Next(255)))); // 随机颜色
Canvas.SetTop(zimu, 0); // 字母top初始位置
//double ranZLeft= Convert.ToDouble(Gamequ.Width - zimu.Width);
Canvas.SetLeft(zimu, ran.Next(0, 780)); // 字母初始left位置随机
Gamequ.Children.Add(zimu);
zimuList.Add(zimu);
}
}
}
WPF 打字游戏
猜你喜欢
转载自blog.csdn.net/dust__/article/details/103506899
周排行