欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c#打字游戏

程序员文章站 2022-07-16 18:54:10
...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 动画
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        Random r = new Random();//随机数
        PictureBox flay = new PictureBox();//人物的pictrue
        Timer xiaochu = new Timer();//人物下落的timer
        Timer shang = new Timer();//人物上移的timer
        Button kaishi = new Button();//开始
        Timer xia = new Timer();//字母下落
        Timer yidong = new Timer();//人物移动的timer
        Timer zml = new Timer();//生成字母的timer
        Label jl = new Label();//计分板
        Label dl = new Label();//掉落的label
        private void Form2_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;//很重要 很重要  在开始按钮之后需要一个焦点
            this.WindowState = FormWindowState.Maximized;//窗体最大
            this.BackgroundImage = Image.FromFile("../../img/beijing.jpg");
            this.BackgroundImageLayout = ImageLayout.Stretch;//背景图拉伸自适应


            //游戏区
            panel1.Width = 1600;
            panel1.Height = 950;
            //字母
         // Timer zml = new Timer();  //字母的timer
            zml.Interval = 1200;
        //  zml.Start();
            zml.Tick += Zml_Tick;

            //字母下落
        //  Timer xia = new Timer();
            xia.Interval = 40;
            xia.Tick += Xia_Tick;
        //  xia.Start();

            //人
            flay.Tag = "flaya";//人的tag
            flay.Image = imageList1.Images[0];
            flay.Size = new Size(145,130);
           // flay.Top = 0;
            flay.Left = panel1.Width - flay.Width;
            panel1.Controls.Add(flay);

            //人的timer
          //Timer yidong = new Timer();
            yidong.Interval = 40;
            yidong.Tick += Yidong_Tick;
         // yidong.Start();

            //消除 timer
           
         // xiaochu.Start();
            xiaochu.Tick += Xiaochu_Tick;

            //上的timer
            
            shang.Tick += Shang_Tick;
           // shang.Start();
            this.KeyPress += Form2_KeyPress;//键盘

            //飞机
            fj.SizeMode = PictureBoxSizeMode.StretchImage;
            fj.Tag="fja";//飞机的tag
            fj.Size = new Size(100,100);
            fj.Image = Image.FromFile(@"../../img/feiji.jpg");
            fj.Location = new Point(panel1.Width/2-fj.Width/2,panel1.Height-fj.Height);//屏幕
            panel1.Controls.Add(fj);

            //开始按钮
          //  Button kaishi = new Button();
            kaishi.Location = new Point(panel1.Width + 40, 50);
            kaishi.Text = "开始游戏";
            this.Controls.Add(kaishi);
            kaishi.Click += Kaishi_Click;


            //计分板
           // Label jl = new Label();
            jl.Text =0+ "个";
            jl.Size = new Size(100,40);
            jl.Location = new Point(kaishi.Left,kaishi.Top+kaishi.Height+20);
            this.Controls.Add(jl);

            //记录掉落的字母
            dl.Text = "已掉落" + 0 + "个";
            dl.AutoSize = true;
            dl.Location = new Point(jl.Left,jl.Top+jl.Height+20);
            this.Controls.Add(dl);
        }
        //开始暂停游戏
        private void Kaishi_Click(object sender, EventArgs e)
        {
            if (kaishi.Text == "开始游戏")
            {
                xia.Start();
                yidong.Start();
                zml.Start();
                xiaochu.Start();
                kaishi.Text = "暂停游戏";
            }
            else if(kaishi.Text=="暂停游戏")
            {
                xia.Stop();
                yidong.Stop();
                zml.Stop();
                shang.Stop();
                xiaochu.Stop();
                kaishi.Text = "开始游戏";
            }
        }

        PictureBox fj = new PictureBox();//飞机picturebox

        //人物的上移动
        private void Shang_Tick(object sender, EventArgs e)
        {              
            flay.Left -= 5;
            flay.Top -= 5;
            
            if (flay.Top<=0)
            {
                shang.Stop();
                xiaochu.Start();
            }
        }

        //人物下落的timer
        private void Xiaochu_Tick(object sender, EventArgs e)
        {
            flay.Left -= 5;
            flay.Top += 5;
            if (flay.Left <= 0)
            {
                flay.Left = panel1.Width;
            }
            if (flay.Top>=30)
            {
                xiaochu.Stop();
                shang.Start();
            }
        }

        //按键消失
        private void Form2_KeyPress(object sender, KeyPressEventArgs e)
        {
            foreach (Control item in panel1.Controls)
            {
                if (item.GetType().Name == "Label")
                {
                    if (item.Text == e.KeyChar.ToString() && item.Tag.ToString() == "zma")
                    {
                        item.Tag = "zmb";//改变其tag让其不能对一个字母重复发射子弹
                        fj.Left = item.Left + item.Width / 2 - fj.Width / 2;
                     
                        
                        //子弹
                        PictureBox zd = new PictureBox();
                        zd.Tag = "zda";//子弹的tag
                        zd.SizeMode = PictureBoxSizeMode.StretchImage;
                        zd.Image = Image.FromFile(@"../../img/Ammo1.png");
                        zd.Size = new Size(4, 34);
                        zd.Location = new Point(fj.Left + fj.Width / 2 - zd.Width / 2, fj.Top - zd.Height);
                        panel1.Controls.Add(zd);
                        return;//防止子弹在重复字母初创建
                    }
 
                }
            }
        }
        //字母下落的timer
        int x = 0;//记录分数的变量
        int y = 0;//掉落的变量
        private void Xia_Tick(object sender, EventArgs e)
        {
            foreach (Control item in panel1.Controls)
            {
                if (item.GetType().Name== "Label")
                {
                  

                    item.Top += 2;
                    if (item.Top>=panel1.Height)
                    {
                        item.Dispose();//超过的字母消失
                        y++;
                        dl.Text = "已掉落" + y + "个";
                        if (y>=5)
                        {
                            
                            xia.Stop();
                            yidong.Stop();
                            zml.Stop();
                            shang.Stop();
                            xiaochu.Stop();
                            MessageBox.Show("你输了", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                        }
                    }
                }
                if (item.GetType().Name== "PictureBox")
                {
                    if (item.Tag.ToString()=="zda")//判断子弹的tag让其上升
                    {
                        item.Top -= 11;
                        if (item.Top<=-item.Height)
                        {
                            item.Dispose();
                        }
                        foreach (Control it in panel1.Controls)
                        {
                            if (it.Tag.ToString() == "zmb")//判断其新的tag让其消失
                            {
                                if (it.Top + it.Height >= item.Top && item.Left == it.Left + it.Width / 2 - item.Width / 2)
                                {
                                    it.Dispose();
                                    item.Dispose();
                                    x+=5;
                                    jl.Text="得分:"+x+"个";
                                    if (x>=50)
                                    {
                                        item.Top -= 25;
                                        xia.Interval = 20;
                                    }
                                    if (x>=70)
                                    {
                                        item.Top -= 40;
                                        xia.Interval = 5;
                                    }
                                    //爆炸
                                    PictureBox baz = new PictureBox();
                                    baz.Image = imageList2.Images[0];
                                    baz.Tag = 0;
                                    baz.Size = new Size(90,90);
                                    baz.SizeMode = PictureBoxSizeMode.StretchImage;
                                    baz.Location = new Point(it.Left+it.Width/2-baz.Width/2,it.Top+it.Height-baz.Height/2);
                                    panel1.Controls.Add(baz);
                                    //爆炸动画
                                    Timer bofang = new Timer();
                                    bofang.Start();
                                    bofang.Tag = baz;
                                    bofang.Interval = 60;
                                    bofang.Tick += Bofang_Tick;
                                }
                            }
                        }
                    }
                }             
            }
        }
        //播放动画timer
        private void Bofang_Tick(object sender, EventArgs e)
        {
            Timer bz = (Timer)sender;
            PictureBox pictur = (PictureBox)bz.Tag;
            pictur.Image = imageList2.Images[(int)pictur.Tag];
            pictur.Tag =(int)pictur.Tag + 1;
            if ((int)pictur.Tag==32)
            {
                bz.Dispose();
                pictur.Dispose();
            }
        }

        int index = 0;
        //人物移动
        private void Yidong_Tick(object sender, EventArgs e)
        {
            index++;
            flay.Image = imageList1.Images[index];
            if (index>=10)
            {
                index = -1;
            }
            
        }

       
       
        private void Zml_Tick(object sender, EventArgs e)
        {
            Label zm = new Label();
            zm.Tag = "zma";//字母的tag
            zm.Font = new Font("", r.Next(20, 35));
            zm.ForeColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
            zm.Text = ((char)r.Next(97, 123)).ToString();
            zm.Top = flay.Height+10;
            zm.Left = flay.Left + flay.Width / 2 - zm.Width / 2;
            zm.AutoSize = true;
            panel1.Controls.Add(zm);
            if (zm.Top >= panel1.Height)
            {
                zm.Dispose();
            }
        }

    }
}

c#打字游戏

 

c#打字游戏