C#实现打字母游戏(逐步骤)

一、剖析:

  • 设计界面(左侧游戏区域,右侧游戏相关设置栏)
  • 游戏区域随机生成字母由上向下落,超过游戏区域底部让字母释放掉并且在右侧栏血量减少
  • 游戏区域显示飞机并且按下对应字母让飞机移动到对应字母正下方,在飞机顶部生成子弹并发射(飞机、字母、子弹三点一线)
  • 当子弹碰到字母时,让子弹、字母都释放掉并且在碰撞位置产生爆炸效果,右侧得分栏加分

二、具体代码如下:

设计界面
下面需要用到游戏区域对象所以设置为全局变量

Panel panel1 = new Panel();//添加游戏区域
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Typing Game";
            this.Size = new Size(1200,700);
            this.BackgroundImage = Image.FromFile("../../img/bg4.png");
            this.BackgroundImageLayout = ImageLayout.Stretch;
            this.CenterToScreen();
            panel1.Size = new Size(950,661);//游戏区域大小
            panel1.BackColor = Color.Purple;//游戏区域背景颜色
            this.Controls.Add(panel1);
        }   

生成字母

Random s = new Random();//随机数
Timer timer1 = new Timer();//用于生成字母
private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 700;
            timer1.Tick += Timer1_Tick;
        }   
private void Timer1_Tick(object sender, EventArgs e)
        {
            Label lb = new Label();//用label装字母
            lb.BackColor = Color.Transparent;
            lb.Location = new Point(s.Next(0,panel1.Width-lb.Width),-lb.height);
            lb.Text = ((char)s.Next(97,123)).ToString();//字母随机
            lb.ForeColor = Color.FromArgb(s.Next(0,255),s.Next(0,255),s.Next(0,255));//字母颜色随机
            lb.Font = new Font("黑体",s.Next(25,35));//字母大小随机
            lb.AutoSize = true;//label根据字母大小改变(确保字母显示完整)
            lb.Tag = "zm";
            panel1.Controls.Add(lb);
        }        

生成飞机

PictureBox pic1 = new PictureBox();//添加飞机
private void Form1_Load(object sender, EventArgs e)
        {
            pic1.Size = new Size(80,80);
            pic1.Image = Image.FromFile("../../img/BP03.png");//飞机
            pic1.SizeMode = PictureBoxSizeMode.StretchImage;//背景图片拉伸
            pic1.Location = new Point(panel1.Width/2-pic1.Width/2,panel1.Height-pic1.Height);
            pic1.Tag = "plane";
            panel1.Controls.Add(pic1);
        }   

字母下落

Timer timer2 = new Timer();//用于控制字母下落
private void Form1_Load(object sender, EventArgs e)
        {
            timer2.Interval = 30;
            timer2.Tick += Timer2_Tick;
        }   
private void Timer2_Tick(object sender, EventArgs e)
        {
            foreach (Control item in panel1.Controls)
            {
                if (item.Tag.ToString()=="zm"||item.Tag.ToString()=="zimu")//字母下落
                {
                    item.Top += 3;
                    if (item.Top+item.Height>=pic1.Top)//字母落到游戏区域底部让字母释放掉
                    {
                        item.Dispose();
                    }
                }
            }   
        }

设置键盘事件(按下对应字母让飞机移动到对应字母正下方并且生成子弹)

private void Form1_Load(object sender, EventArgs e)
        {
            this.KeyPress += Form1_KeyPress;
        }   
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            foreach (Control item in panel1.Controls)
            {
                if (item.Tag.ToString() == "zm")
                {
                    if (item.Text == e.KeyChar.ToString())//判断按下的字符与字母是否相同
                    {
                        
                        item.Tag = "zimu";
                        pic1.Left = item.Left + item.Width / 2 - pic1.Width / 2;//飞机移动到按下字符正下方下面
                        //飞机移动到相应位置后生成子弹
                        PictureBox pic2 = new PictureBox();//生成子弹
                        bullet.Play();//播放发射音效
                        pic2.Size = new Size(30, 60);
                        pic2.Image = Image.FromFile("../../img/jiguang3.png");
                        pic2.SizeMode = PictureBoxSizeMode.StretchImage;
                        pic2.Location = new Point(pic1.Left + pic1.Width / 2 - pic2.Width / 2, pic1.Top - pic2.Height);
                        pic2.Tag = "zd";
                        panel1.Controls.Add(pic2);
                        return;//解决子弹多发问题
                    }
                }
            }
        }

发射子弹并且碰到对应字母爆炸(飞机、字母、子弹三点一线)

Timer timer2 = new Timer();//用于控制字母下落
private void Form1_Load(object sender, EventArgs e)
        {
            timer2.Interval = 30;
            timer2.Tick += Timer2_Tick;
        }
private void Timer2_Tick(object sender, EventArgs e)
        {
            foreach (Control item in panel1.Controls)
            {
                if (item.Tag.ToString()=="zd")//子弹上升
                {
                    item.Top -= 10;
                    //当子弹碰到字母产生爆炸,字母和子弹都消失
                    foreach (Control it in panel1.Controls)
                    {
                        if (it.Tag.ToString()=="zimu")
                        {
                            if (item.Top<=it.Top+it.Height && item.Left + item.Width / 2 == it.Left + it.Width / 2)//飞机子弹字母三点一线(保证不脱靶)
                            {
                                item.Dispose();
                                it.Dispose();
                                boom.Play();
                                //创建爆炸特效(有频率的展示完imagelist里面的图像)
                                PictureBox bz = new PictureBox();
                                bz.Tag = 0;
                                bz.Size = new Size(50,50);
                                bz.Location = new Point(item.Left+item.Width/2-bz.Width/2,item.Top-bz.Height/2);
                                bz.Image = imageList1.Images[0];
                                panel1.Controls.Add(bz);
                                Timer timer3 = new Timer();//用于控制爆炸效果
                                timer3.Interval = 30;
                                timer3.Tick += Timer3_Tick;
                                timer3.Start();
                                timer3.Tag = bz;
                            }
                        }
                    }
                }
            }   
        }    
private void Timer3_Tick(object sender, EventArgs e)
        {
            Timer baozha=(Timer)sender;
            PictureBox pic3 = (PictureBox)baozha.Tag;//获取到bz对象
            pic3.Image = imageList1.Images[(int)pic3.Tag];
            pic3.Tag = (int)pic3.Tag + 1;
            if ((int)pic3.Tag>31)
            {
                pic3.Dispose();
                baozha.Dispose();
            }
        }

右侧栏开始游戏以及重新开始游戏

int c = 0;
private void Form1_Load(object sender, EventArgs e)
        {
            PictureBox start = new PictureBox();
            start.Image = Image.FromFile("../../img/start.png");
            start.SizeMode = PictureBoxSizeMode.AutoSize;
            start.Location = new Point(panel1.Width+45,50);
            start.BackColor = Color.Transparent;
            this.Controls.Add(start);
            start.Click += Start_Click;
        }
private void Start_Click(object sender, EventArgs e)
        {
            
            if (c==0)
            {
                timer1.Start();
                timer2.Start();
                PictureBox reset = (PictureBox)sender;
                reset.Image = Image.FromFile("../../img/reset.png");
                reset.SizeMode = PictureBoxSizeMode.AutoSize;
                reset.Location = new Point(panel1.Width + 100, 50);
                c = 1;
                return;
            }
            if (c==1)
            {
                Form2 f2 = new Form2();
                this.Hide();
                f2.ShowDialog();
                this.Dispose();
            }
        }

暂停和继续游戏

int c = 0;
private void Form1_Load(object sender, EventArgs e)
        {
            //暂停游戏
            PictureBox zanting = new PictureBox();
            zanting.Image = Image.FromFile("../../img/zanting.png");
            zanting.SizeMode = PictureBoxSizeMode.AutoSize;
            zanting.BackColor = Color.Transparent;
            zanting.Location = new Point(panel1.Width + 80, 100);
            this.Controls.Add(zanting);
            zanting.Click += Zanting_Click;
        }
private void Start_Click(object sender, EventArgs e)
        {
            
            if (c==0)
            {
                timer1.Stop();
                timer2.Stop();
                PictureBox zt = (PictureBox)sender;
                zt.Image = Image.FromFile("../../img/jixu.png");
                zt.SizeMode = PictureBoxSizeMode.AutoSize;
                zt.Location = new Point(panel1.Width + 80, 110);
                c = 1;
                return;
            }
            if (c==1)
            {
                timer1.Start();
                timer2.Start();
                PictureBox jx = (PictureBox)sender;
                jx.Image = Image.FromFile("../../img/zanting.png");
                jx.SizeMode = PictureBoxSizeMode.AutoSize;
                jx.Location = new Point(panel1.Width + 80, 100);
                c = 0;
                return;
            }
        }

游戏难度以及关卡设置

int score = 0;
int y = 100;
private void Form1_Load(object sender, EventArgs e)
        {
			//难度选项
            ComboBox nandu = new ComboBox(); 
            nandu.Location = new Point(panel1.Width + 45, 250);
            nandu.Width = 150;
            nandu.Font = new Font("黑体", 15f, FontStyle.Bold);
            nandu.ForeColor = Color.Orange;
            nandu.Items.Add("简易");
            nandu.Items.Add("困难");
            nandu.Items.Add("噩梦");
            nandu.DropDownStyle = ComboBoxStyle.DropDownList;//设置不可编辑
            nandu.SelectedIndex = 0;
            this.Controls.Add(nandu);
            nandu.SelectedIndexChanged += Nandu_SelectedIndexChanged;
            
            //场景选项
            ComboBox cj = new ComboBox();
            cj.Location = new Point(panel1.Width + 45, 300);
            cj.Width = 150;
            cj.Font = new Font("黑体", 15f, FontStyle.Bold);
            cj.ForeColor = Color.Orange;
            cj.Items.Add("太空之战");
            cj.Items.Add("深海大战");
            cj.Items.Add("平原战争");
            cj.Items.Add("星空之战");
            cj.DropDownStyle = ComboBoxStyle.DropDownList;//设置不可编辑
            cj.SelectedIndex = 3;
            this.Controls.Add(cj);
            cj.SelectedIndexChanged += Gk_SelectedIndexChanged;
        }
private void Nandu_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox nandu1 = (ComboBox)sender;
            if (nandu1.SelectedIndex==0)
            {
                timer1.Interval = 700;
                timer2.Interval = 20;
            }
            if (nandu1.SelectedIndex == 1)
            {
                timer1.Interval = 600;
                timer2.Interval = 15;
            }
            if (nandu1.SelectedIndex == 2)
            {
                timer1.Interval = 300;
                timer2.Interval = 5;
            }
        }
private void Gk_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox changjing = (ComboBox)sender;
            if (changjing.SelectedIndex== 0)
            {
                this.BackgroundImage = Image.FromFile("../../img/bg1.png");
                this.BackgroundImageLayout = ImageLayout.Stretch;
                panel1.BackColor = Color.OrangeRed;
            }
            if (changjing.SelectedIndex == 1)
            {
                this.BackgroundImage = Image.FromFile("../../img/bg2.png");
                this.BackgroundImageLayout = ImageLayout.Stretch;
                panel1.BackColor = Color.DeepSkyBlue;
            }
            if (changjing.SelectedIndex == 2)
            {
                this.BackgroundImage = Image.FromFile("../../img/bg3.png");
                this.BackgroundImageLayout = ImageLayout.Stretch;
                panel1.BackColor = Color.Green;
            }
            if (changjing.SelectedIndex == 3)
            {
                this.BackgroundImage = Image.FromFile("../../img/bg4.png");
                this.BackgroundImageLayout = ImageLayout.Stretch;
                panel1.BackColor = Color.Purple;
            }
        }

得分以及血量相关设置(按对一个字母加一分,字母落到游戏区域外掉一滴血)

Label lab2 = new Label();
Label lab4 = new Label();
private void Form1_Load(object sender, EventArgs e)
        {
            Label lab1 = new Label();
            Label lab3 = new Label();
            Label lab5 = new Label();
            Label lab6 = new Label();
            lab1.Text = "得分:";
            lab1.Font = new Font("黑体", 15f, FontStyle.Bold);
            lab1.ForeColor = Color.Orange;
            lab1.BackColor = Color.Transparent;
            lab1.Location = new Point(panel1.Width + 45, 500);
            this.Controls.Add(lab1);

            lab2.Text = score.ToString();
            lab2.Font = new Font("黑体", 15f, FontStyle.Bold);
            lab2.ForeColor = Color.Orange;
            lab2.BackColor = Color.Transparent;
            lab2.Location = new Point(lab1.Left+lab1.Width, 500);
            this.Controls.Add(lab2);
            
            lab3.Text = "血量:";
            lab3.Font = new Font("黑体", 15f, FontStyle.Bold);
            lab3.ForeColor = Color.Orange;
            lab3.BackColor = Color.Transparent;
            lab3.Location = new Point(panel1.Width + 45, 550);
            this.Controls.Add(lab3);
            
            lab4.Text = y.ToString();
            lab4.Font = new Font("黑体", 15f, FontStyle.Bold);
            lab4.ForeColor = Color.Orange;
            lab4.BackColor = Color.Transparent;
            lab4.Location = new Point(lab3.Left + lab3.Width, 550);
            this.Controls.Add(lab4);
        }
private void Timer2_Tick(object sender, EventArgs e)
        {
            foreach (Control item in panel1.Controls)
            {
                if (item.Tag.ToString()=="zm"||item.Tag.ToString()=="zimu")//字母下落
                {
                    item.Top += 3;
                    if (item.Top+item.Height>=pic1.Top)
                    {
                        item.Dispose();
                        y--;
                        if (y==0)
                        {
                            MessageBox.Show("Game Over!!!","前方高能",MessageBoxButtons.OK,MessageBoxIcon.Information);
                            timer1.Stop();
                            timer2.Stop();
                        }
                        lab4.Text = y.ToString();
                    }
                }
                if (item.Tag.ToString()=="zd")//子弹上升
                {
                    item.Top -= 10;
                    //当子弹碰到字母产生爆炸,字母和子弹都消失
                    foreach (Control it in panel1.Controls)
                    {
                        if (it.Tag.ToString()=="zimu")
                        {
                            if (item.Top<=it.Top+it.Height && item.Left + item.Width / 2 == it.Left + it.Width / 2)//飞机子弹字母三点一线(保证不脱靶)
                            {
                                item.Dispose();
                                it.Dispose();
                                boom.Play();
                                //创建爆炸特效(有频率的展示完imagelist里面的图像)
                                PictureBox bz = new PictureBox();
                                bz.Tag = 0;
                                bz.Size = new Size(50,50);
                                bz.Location = new Point(item.Left+item.Width/2-bz.Width/2,item.Top-bz.Height/2);
                                bz.Image = imageList1.Images[0];
                                panel1.Controls.Add(bz);
                                Timer timer3 = new Timer();//用于控制爆炸效果
                                timer3.Interval = 30;
                                timer3.Tick += Timer3_Tick;
                                timer3.Start();
                                timer3.Tag = bz;
                                score++;
                                lab2.Text = score.ToString();
                            }
                        }
                    }
                }
            }
        }

设置页面跳转

在当前项目中新建一个窗体(Form2),当作封面,设置图片点击事件进行跳转

注意:需要在Program.cs中更改主函数入口为Form2

private void Form2_Load(object sender, EventArgs e)
        {
            this.Text = "Typing Game";
            this.Size = new Size(1200, 700);
            this.CenterToScreen();
            this.BackgroundImage = Image.FromFile("../../img/pig.jpg");
            this.BackgroundImageLayout = ImageLayout.Stretch;
            PictureBox kaishi = new PictureBox();
            kaishi.Image = Image.FromFile("../../img/start.png");
            kaishi.SizeMode = PictureBoxSizeMode.AutoSize;
            kaishi.Location = new Point(this.Width/2-kaishi.Width/2,500);
            kaishi.BackColor = Color.Transparent;
            this.Controls.Add(kaishi);
            kaishi.Click += Kaishi_Click;
        }
private void Kaishi_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();//对Form1实例化
            this.Hide();//对用户隐藏控件
            f1.ShowDialog();//将Form1显示为模式对话框
            this.Dispose();//释放所有Form2资源
        }        

解决添加按钮后键盘事件不起作用问题

让键盘事件重新获取焦点

this.KeyPreview = true;//解决添加btn后按键不起作用问题

三、效果图

C#实现打字母游戏(逐步骤)_第1张图片
C#实现打字母游戏(逐步骤)_第2张图片
C#实现打字母游戏(逐步骤)_第3张图片
C#实现打字母游戏(逐步骤)_第4张图片
C#实现打字母游戏(逐步骤)_第5张图片
C#实现打字母游戏(逐步骤)_第6张图片
C#实现打字母游戏(逐步骤)_第7张图片
C#实现打字母游戏(逐步骤)_第8张图片

看完记得点赞,下期讲解如何在winform中添加音乐以及页面切换详解!!!

你可能感兴趣的:(C#打字母游戏,C#案例,winform实现打字母游戏)