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

C#打字游戏(2)

程序员文章站 2022-07-16 19:10:00
...

C#打字游戏(2)

C#打字游戏(2)

C#打字游戏(2)

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 Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //随机
        Random r = new Random();
        //鸟的盒子
        PictureBox box = new PictureBox();
        //字母下落
        Timer zmtop = new Timer();
        //鸟飞
        Timer fly = new Timer();
        //随机字母
        Timer latimer = new Timer();
        //鸟动画
        Timer bird = new Timer();
        //飞机盒子
        PictureBox plane = new PictureBox();
        //小火苗1
        PictureBox fire1 = new PictureBox();
        //小火苗2
        PictureBox fire2 = new PictureBox();
        //得分
        Label lab = new Label();
        //记录得分
        int score = 0;
        //进度血条
        Label ljd = new Label();
        //记录血量
        int xt = 100;
        //清屏
        List<Label> zimulab = new List<Label>();
        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            this.Text = "打字游戏";
            this.BackgroundImage = Image.FromFile("../../img/1.jpg");
            this.BackgroundImageLayout = ImageLayout.Stretch;
            //游戏区
            panel1.Width = 1100;
            panel1.Height = 680;
            panel1.BackColor = Color.White;
            this.Controls.Add(panel1);
            //随机字母
            latimer.Interval = 800; 
            latimer.Tick += Latimer_Tick;
            //字母下落
            zmtop.Interval = 50;
            zmtop.Tick += Zmtop_Tick;
            //键盘
            this.KeyPress += Form1_KeyPress;
            //鸟盒子
            box.Image = imageList1.Images[0];
            box.Tag = "bird";
            box.Width = 211;
            box.Height = 169;
            panel1.Controls.Add(box);
            //鸟动画
            bird.Interval = 50;
            bird.Tick += Bird_Tick;
            //鸟飞
            fly.Interval = 20;
            fly.Tick += Fly_Tick;
            //飞机
            plane.Tag = "plane";
            plane.Size = new Size(70, 70);
            plane.SizeMode = PictureBoxSizeMode.StretchImage;
            plane.Image = Image.FromFile("../../img/plane1.png");
            plane.Location = new Point(panel1.Width / 2 - plane.Width / 2, panel1.Height - plane.Height-20);
            panel1.Controls.Add(plane);
            //小火苗1
            fire1.Size = new Size(10,20);
            fire1.SizeMode = PictureBoxSizeMode.StretchImage;
            fire1.Tag = 0;
            fire1.Image = imageList3.Images[0];
            fire1.Left = plane.Left+plane.Width/2 - fire1.Width-8;
            fire1.Top = plane.Top+plane.Height;
            panel1.Controls.Add(fire1);
            //小火苗2
            fire2.Size = new Size(10, 20);
            fire2.SizeMode = PictureBoxSizeMode.StretchImage;
            fire2.Tag = 0;
            fire2.Image = imageList3.Images[0];
            fire2.Left = plane.Left + plane.Width/2 - fire2.Width+15;
            fire2.Top = plane.Top + plane.Height;
            panel1.Controls.Add(fire2);
            //小火苗计时器
            Timer firet = new Timer();
            firet.Interval = 10;
            firet.Tag = fire1;
            firet.Start();
            firet.Tick += Firet_Tick;
            //控制区
            panel2.Left=panel1.Left+panel1.Width+20;
            panel2.Top = 20;
            panel2.Width = 200;
            panel2.Height = 680;
            panel2.BackColor = Color.Cyan;
            this.Controls.Add(panel2);
            //开始游戏
            Label start = new Label();
            start.Text = "开始游戏";
            start.Font = new Font("", 20);
            start.AutoSize = true;
            start.Location = new Point(40, 50);
            start.Click += Start_Click;
            panel2.Controls.Add(start);
            //暂停游戏
            Label stop = new Label();
            stop.Text = "暂停游戏";
            stop.Font = new Font("", 20);
            stop.AutoSize = true;
            stop.Location = new Point(40, 120);
            stop.Click += Stop_Click;
            panel2.Controls.Add(stop);
            //得分
            lab.Text = "得分:0分";
            lab.Font = new Font("", 20);
            lab.AutoSize = true;
            lab.Location = new Point(40, 200);
            panel2.Controls.Add(lab);
            //血条
            Label lxt = new Label();
            lxt.Text = "血量";
            lxt.Font = new Font("", 20);
            lxt.AutoSize = true;
            lxt.Location = new Point(40, 270);
            panel2.Controls.Add(lxt);
            //进度血条
            ljd.Size = new Size(100, 20);
            ljd.Location = new Point(40, 350);
            ljd.BackColor = Color.Red;
            panel2.Controls.Add(ljd);
            //进度条背景
            Label lbj = new Label();
            lbj.Size = new Size(100, 20);
            lbj.Location = new Point(40, 350);
            lbj.BackColor = Color.White;
            panel2.Controls.Add(lbj);
        }
        //小火苗动画
        private void Firet_Tick(object sender, EventArgs e)
        {
            fire1.Image = imageList3.Images[(int)fire1.Tag];
            fire2.Image = imageList3.Images[(int)fire2.Tag];
            fire1.Tag = (int)fire1.Tag + 1;
            fire2.Tag = (int)fire2.Tag + 1;
            if ((int)fire1.Tag > 1 || (int)fire2.Tag > 1)
            {
                fire1.Tag=0;
                fire2.Tag=0;
            }
        }

        //暂停游戏
        private void Stop_Click(object sender, EventArgs e)
        {
            zmtop.Stop();
            fly.Stop();
            latimer.Stop();
            bird.Stop();
        }

        //开始游戏计时器
        private void Start_Click(object sender, EventArgs e)
        {
            zmtop.Start();
            fly.Start();
            latimer.Start();
            bird.Start();
        }

        //飞计时器
        private void Fly_Tick(object sender, EventArgs e)
        {
            box.Left += 3;
            if (box.Left >= panel1.Width)
            {
                box.Left = -box.Width;
            }

        }
        //鸟动画计时器
        int a = 0;
        private void Bird_Tick(object sender, EventArgs e)
        {
            a++;
            box.Image = imageList1.Images[a];
            
            if (a >= 10)
            {
                a = -1;
            }
        }
        //键盘按钮
        private void Form1_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()=="zm")
                    {
                        //item.Dispose();
                        plane.Left = item.Left + item.Width / 2 - plane.Width / 2;
                        item.Tag = "bj";
                        fire1.Left = plane.Left + plane.Width / 2 - fire1.Width - 8;
                        fire1.Top = plane.Top + plane.Height;
                        fire2.Left = plane.Left + plane.Width / 2 - fire2.Width + 15;
                        fire2.Top = plane.Top + plane.Height;
                        //子弹盒子
                        PictureBox bullet = new PictureBox();
                        bullet.Tag = "zd";
                        bullet.Size = new Size(4, 30);
                        bullet.SizeMode = PictureBoxSizeMode.StretchImage;
                        bullet.Image = Image.FromFile("../../img/Ammo1.png");
                        bullet.Location = new Point(plane.Left+plane.Width/2-bullet.Width/2,plane.Top-bullet.Height);
                        panel1.Controls.Add(bullet);
                        return;
                    }
                }
            }
        }

        //字母下落计时器,子弹上升计时器
        private void Zmtop_Tick(object sender, EventArgs e)
        {
            //字母下落
            foreach(Control item in panel1.Controls)
            {
                if (item.GetType().Name == "Label")
                {
                    item.Top += 3;
                    if (item.Top >= panel1.Height)
                    {
                        item.Dispose();
                        //血条
                        xt -= 10;
                        ljd.Width = xt;
                        if (xt == 0)
                        {
                            zmtop.Stop();
                            fly.Stop();
                            latimer.Stop();
                            bird.Stop();
                            if (MessageBox.Show("游戏结束,你的得分" + score, "Game,over", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Retry)
                            {
                                zmtop.Start();
                                fly.Start();
                                latimer.Start();
                                bird.Start();
                                score = 0;

                                lab.Text = "得分" + score + "分";
                                xt = 100;
                                ljd.Width = xt;
                                qingping();
                            }
                            else
                            {
                                this.Close();
                            }
                        }
                    }
                }
                //子弹上升
                if(item.Tag.ToString()=="zd")
                {
                    item.Top -= 5;
                    if (item.Top < -item.Height)
                    {
                        item.Dispose();
                    }
                    //判断字母子弹相撞
                    foreach(Control zmu in panel1.Controls)
                    {
                        if (zmu.Tag.ToString() == "bj")
                        {
                            if (item.Top <= zmu.Top + zmu.Height && item.Left + item.Width / 2 == zmu.Left + zmu.Width / 2)
                            {
                                item.Dispose();
                                zmu.Dispose();
                                //记录得分
                                score += 5;
                                lab.Text = "得分" + score + "分";
                                //爆炸
                                PictureBox bomb = new PictureBox();
                                bomb.Tag = 0;
                                bomb.Size = new Size(50,50);
                                bomb.SizeMode = PictureBoxSizeMode.StretchImage;
                                bomb.Image = imageList2.Images[0];
                                bomb.Location = new Point(zmu.Left + zmu.Width / 2 - bomb.Width / 2, zmu.Top + zmu.Height / 2 - bomb.Height / 2);
                                panel1.Controls.Add(bomb);
                                //爆炸图片
                                Timer bombt = new Timer();
                                bombt.Interval = 70;
                                bombt.Start();
                                bombt.Tag = bomb;
                                bombt.Tick += Bombt_Tick;
                            }
                        }
                    }
                }
            }
        }
        //爆炸动画
        private void Bombt_Tick(object sender, EventArgs e)
        {
            Timer bz = (Timer)sender;
            PictureBox picture = (PictureBox)bz.Tag;
            picture.Image = imageList2.Images[(int)picture.Tag];
            picture.Tag = (int)picture.Tag + 1;
            if ((int)picture.Tag > 31)
            {
                picture.Dispose();
                bz.Dispose();
            }

        }

        //字母计时器
        private void Latimer_Tick(object sender, EventArgs e)
        {
            if (box.Left >= 0 && box.Left <= panel1.Width - box.Width)
            {
                //字母生成
                Label la = new Label();
                la.Tag = "zm";
                la.Text = ((char)r.Next(97, 123)).ToString();
                la.Top = box.Height;
                la.Left = box.Left+box.Width/2-la.Width/2;
                la.Font = new Font("", r.Next(15, 30));
                la.ForeColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
                la.AutoSize = true;
                panel1.Controls.Add(la);
                zimulab.Add(la);
            }
        }
        private void qingping()
        {
            foreach (Label zm in zimulab)
            {
                zm.Dispose();
            }
        }

        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}