C#WPF打字游戏

 实现简单的功能,可以消灭字母,没有写计时,分数

设计界面

<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="打字游戏" Height="450" Width="800">
    <Canvas x:Name="gm" Margin="0,0,0,0" Loaded="gm_Loaded">
        <Canvas x:Name="game">
            <Button Content="开始游戏" Canvas.Left="750" Canvas.Top="50" Click="Button_Click" ></Button>
            <Button Content="暂停游戏" Canvas.Left="750" Canvas.Top="100" Click="Button_Click_1"></Button>
        </Canvas>
    </Canvas>
</Window>

代码界面

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 打字游戏
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        Random r = new Random();
        //字母生成计时器
        DispatcherTimer zimu = new DispatcherTimer();
        //字母下落
        DispatcherTimer zmtop = new DispatcherTimer();
        //飞机图片
        Image plane = new Image();
        //泛型集合字母
        List<Label> zmu = new List<Label>();
        //泛型集合子弹
        List<Image> zdan = new List<Image>();
        private void gm_Loaded(object sender, RoutedEventArgs e)
        {
            this.Width = 900;
            this.Height = 700;
            this.Left = SystemParameters.PrimaryScreenWidth / 2 - this.Width / 2;
            this.Top = SystemParameters.WorkArea.Height  / 2 - this.Height / 2;
            gm.Background = Brushes.Cyan;
            //线性渐变
            //gm.Background = new LinearGradientBrush(Colors.Green, Colors.Yellow, 0);
            //径向渐变
            //gm.Background = new RadialGradientBrush(Colors.Yellow, Colors.Green);
            //纯色绘制
            //gm.Background = new SolidColorBrush(Colors.Red);
            game.Width = 700;
            game.Height = 600;
            Canvas.SetLeft(game, 30);
            Canvas.SetTop(game, 30);
            game.Background = Brushes.White;

            //字母生成计时器
            //设置天,小时,分钟,秒,毫秒
            //zimu.Interval = new TimeSpan(0, 0, 0, 0, 300);
            //毫秒
            zimu.Interval = TimeSpan.FromMilliseconds(800);
            zimu.Tick += Zimu_Tick;
            

            //字母下落
            zmtop.Interval = TimeSpan.FromMilliseconds(50);
            zmtop.Tick += Zmtop_Tick;
            

            //键盘
            this.KeyDown += MainWindow_KeyDown;

            //飞机图片
            plane.Tag = "plane";
            plane.Source = new BitmapImage(new Uri("../../img/plane1.png", UriKind.Relative));
            plane.Width = 50;
            plane.Height = 50;
            plane.Stretch = Stretch.UniformToFill;
            Canvas.SetLeft(plane, game.Width / 2 - plane.Width / 2);
            Canvas.SetTop(plane, game.Height - plane.Height);
            game.Children.Add(plane);
        }
        //按键
        private void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            //循环游戏区内所有东西
            for (int i=0;i<game.Children.Count;i++)
            {
                //判断字母
                if (game.Children[i].GetType().Name == "Label")
                {
                    //判断按键与字母
                    if(e.Key.ToString().ToLower()==(game.Children[i] as Label).Content.ToString() && ((Label)game.Children[i]).Tag.ToString() == "zm")
                    {
                        //对应后tag值改成标记
                        ((Label)game.Children[i]).Tag = "bj";
                        //判断字母与飞机在同一中轴线
                        Canvas.SetLeft(plane, Canvas.GetLeft(game.Children[i]) + (game.Children[i] as Label).Width / 2 - plane.Width / 2);
                        //game.Children.Remove(game.Children[i]);

                        //子弹生成
                        Image zidan = new Image();
                        zidan.Tag = "zd";
                        zidan.Source = new BitmapImage(new Uri("../../img/zd.png", UriKind.Relative));
                        zidan.Width = 10;
                        zidan.Height = 30;
                        //zidan.Stretch = Stretch.UniformToFill;
                        Canvas.SetLeft(zidan, Canvas.GetLeft(plane)+plane.Width / 2 - zidan.Width / 2);
                        Canvas.SetTop(zidan, Canvas.GetTop(plane) - plane.Height/2);
                        zdan.Add(zidan);
                        game.Children.Add(zidan);
                        
                        break;

                        

                    }
                }
            }
        }

        //字母下落
        private void Zmtop_Tick(object sender, EventArgs e)
        {
            //遍历字母集合
            foreach(UIElement zimu in zmu)
            {
                //判断是字母或者标记,下落
                if (((Label)zimu).Tag.ToString() == "zm" || ((Label)zimu).Tag.ToString() == "bj")
                {
                    double top = Canvas.GetTop(zimu);
                    Canvas.SetTop(zimu, top += 5);
                    //到达游戏区底部释放
                    if (top >= game.Height-30)
                    {
                        game.Children.Remove(zimu);
                    }
                }
                //遍历子弹集合
                foreach(UIElement zidan in zdan)
                {
                    //判断子弹上升
                    if (((Image)zidan).Tag.ToString() == "zd")
                    {
                        double top = Canvas.GetTop(zidan);
                        Canvas.SetTop(zidan, top -= 3);
                        //判断到达顶部释放
                        if (top <= 0)
                        {
                            game.Children.Remove(zidan);
                        }
                        //遍历字母集合,找到标记字母
                        foreach(UIElement zmbj in zmu)
                        {
                            if (zmbj.GetType().Name == "Label" && ((Label)zmbj).Tag.ToString() == "bj")
                            {
                                //判断相撞
                                if (Canvas.GetTop(zidan) <= Canvas.GetTop(zmbj) + ((Label)zmbj).Height && Canvas.GetLeft(zidan) + ((Image)zidan).Width / 2 == Canvas.GetLeft(zmbj) + ((Label)zmbj).Width / 2)
                                {
                                    
                                    //相撞后消失
                                    game.Children.Remove(zmbj);
                                    game.Children.Remove(zidan);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        //字母生成器
        private void Zimu_Tick(object sender, EventArgs e)
        {
            Label zm = new Label();
            zm.Tag = "zm";
            zm.Content =(char)r.Next(97, 123);
            zm.Width = 40;
            zm.Height = 40;
            zm.FontSize = 25;
            zm.HorizontalContentAlignment = HorizontalAlignment.Stretch;
            zm.VerticalContentAlignment = VerticalAlignment.Stretch;
            //zm.FontStretch = new FontStretch();
            zm.Foreground = new SolidColorBrush(Color.FromRgb((byte)r.Next(0, 255), (byte)r.Next(0, 255), (byte)r.Next(0, 255)));
            Canvas.SetLeft(zm, r.Next(0,670));
            Canvas.SetTop(zm, 0);
            zmu.Add(zm);
            game.Children.Add(zm);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            zimu.Start();
            zmtop.Start();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            zimu.Stop();
            zmtop.Stop();
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值