c#飞机大战

c#飞机大战源码

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();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Size = new Size(1000,800);
            this.BackColor = Color.Pink;
            this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
            this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
            
            //创建一个游戏区
            
            BG.BackColor = Color.White; 
            BG.Location = new Point(20, 30);
            BG.Width = 940;
            BG.Height = 600;
            this.Controls.Add(BG);


            //字母生成器Createtime
            Timer CreateTime = new Timer();
            CreateTime.Interval = 750;
            CreateTime.Tick += CreateTime_Tick;
            CreateTime.Start();

            //控制字母下落FlyTime
            Timer Flytime = new Timer();
            Flytime.Interval = 10;
            Flytime.Tick += Flytime_Tick;
            Flytime.Start();
            
            //创建一个飞机
            player.Size = new Size(80,80);
            player.Top = BG.Height - player.Height;
            player.Left = BG.Width / 2 - player.Width / 2;
            player.Image = Image.FromFile(@"../../img/RP03.png");
            player.SizeMode = PictureBoxSizeMode.StretchImage;
            player.Tag = "plan";
            BG.Controls.Add(player);

            this.KeyPress += Form1_KeyPress;
        }

       

        PictureBox player = new PictureBox();
        Panel BG = new Panel();
        Random r = new Random();
        private void CreateTime_Tick(object sender, EventArgs e)
        {
            //生成字母的本质是使用Lable控件生成
            Label lb = new Label();
            lb.Tag = "zimu";
            lb.Text = ((char)r.Next(97, 123)).ToString();
            lb.Font = new Font("", r.Next(20, 25));
            lb.ForeColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
            lb.Top = 0;
            lb.Left = r.Next(0, BG.Width - lb.Width);
            lb.AutoSize = true;//设置自适应大小
            lb.BackColor = Color.Transparent;//透明色
            BG.Controls.Add(lb);

        }
        //控制生成的字母下落
        private void Flytime_Tick(object sender, EventArgs e)
        {
            foreach (Control item in BG.Controls)
            {
                if (item.Tag.ToString() == "zimu" || item.Tag.ToString() == "biaoji")
                {
                    item.Top += 2;

                    if (item.Top < -item.Height)
                    {
                        item.Dispose();
                    }

                }
                //item的tag是zd则这个item是子弹
                if (item.Tag.ToString() == "zd")
                {
                    item.Top -= 3;
                    if (item.Top < -item.Height)
                    {
                        item.Dispose();
                    }
                    foreach (Control zm in BG.Controls)
                    {
                        if (zm.Tag.ToString() == "biaoji")
                        {
                            if (item.Top <= zm.Top + zm.Height && item.Left + item.Width / 2 == zm.Left + zm.Width / 2)
                            {
                                item.Dispose();
                                zm.Dispose();

                            }
                        }
                    }
                }


            }

        }
        //按下对应的字母
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
           {
            //事件2,e的参数,e事件触发者对象
            //e.keychar
            foreach (Control item in BG.Controls)
            {
                //判断按键值和某个字母值对应
                if (item.Text == e.KeyChar.ToString() && item.Tag.ToString() == "zimu")
                {
                    item.Tag = "biaoji";
                    //则先控制飞机移动到字母正下方
                    player.Left = item.Left + item.Width / 2 - player.Width / 2;
                    //创建子弹
                    PictureBox bullet = new PictureBox();
                    bullet.Size = new Size(6, 30);
                    bullet.Tag = "zd";
                    bullet.Image = Image.FromFile(@"../../img/zd.jpeg");
                    bullet.SizeMode = PictureBoxSizeMode.StretchImage;
                    bullet.Left = player.Left + player.Width / 2 - bullet.Width / 2;
                    bullet.Top = player.Top - bullet.Height;
                    BG.Controls.Add(bullet);
                    //跳出当前事件,return以下的事件中的代码全部无效
                    return;
                }
            }


        }
    }
}

运行结果:

c#飞机大战_第1张图片

你可能感兴趣的:(C#练习)