C#飞机大战(随机位置)

C#飞机大战(随机位置)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text; 
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 飞机
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 游戏区域
        Panel Game = new Panel();
        // 己方飞机
        PictureBox plane = new PictureBox();
        // 随机数
        Random ran = new Random();
        // 尾翼火
        PictureBox fr1 = new PictureBox();
        // 尾翼火
        PictureBox fr2 = new PictureBox();
        // 补给箱
        //Timer f1tim = new Timer();
        // 上升计时器
        Timer timertop = new Timer();
        // 创建敌机计时器
        Timer enem_timer = new Timer();
        private void Form1_Load(object sender, EventArgs e)
        {
            // 窗体位置
            this.Size = new Size(1000, 800);
            this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
            this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
            this.BackColor = Color.White;
            // 游戏区域Panel-Game
            Game.Size = new Size(600, 800);
            this.BackColor = Color.Pink;
            Game.BackColor = Color.White;
            Game.Location = new Point(10, 10);
            this.Controls.Add(Game);
            // 添加飞机-plean在游戏区域
            plane.Size = new Size(60, 60);
            plane.Tag = "plean";
            plane.Top = Game.Height - plane.Height;
            plane.Left = Game.Width / 2 - plane.Width / 2;
            plane.BackgroundImage = Image.FromFile(@"../../img/fj.png");
            plane.BackgroundImageLayout = ImageLayout.Stretch;
            Game.Controls.Add(plane);
            fr1.Image = imageList2.Images[0];
            fr2.Image = imageList2.Images[0];
            fr1.Top = Game.Height - plane.Height;
            fr1.Left = Game.Width / 2 - plane.Width / 2;
            fr2.Top = Game.Height - plane.Height;
            fr2.Left = Game.Width / 2 - plane.Width / 2;
            fr1.Tag = 0;
            fr2.Tag = 0;
            fr1.Size = new Size(11, 25);
            fr2.Size = new Size(11, 25);
            Game.Controls.Add(fr1);
            Game.Controls.Add(fr2);
           // f1tim.Interval = 30;
            //f1tim.Start();
           // f1tim.Tick += F1tim_Tick;
            // 鼠标移动事件
            Game.MouseMove += Game_MouseMove;
            // 键盘发射子弹
            this.KeyPress += Form1_KeyPress;
            plane.Click += Game_Click;
            // 上升计时器
            timertop.Interval = 10;
            timertop.Start();
            timertop.Tick += Timertop_Tick;
            // 创建敌机计x时器          
            enem_timer.Interval = 1000;
            enem_timer.Start();
            enem_timer.Tick += Em_timer_Tick;
            // 补给箱计时器
            Timer dazhao = new Timer();
            dazhao.Interval = 10000;
            dazhao.Start();
            dazhao.Tick += Dazhao_Tick;
        }
        // 鼠创创建子弹
        private void Game_Click(object sender, EventArgs e)
        {
            SoundPlayer player = new SoundPlayer(@"../../music/bullet.wav");
            player.Play();
            PictureBox butt = new PictureBox();
            butt.Tag = "zidan";
            butt.Image = Image.FromFile(@"../../img/1.png");
            butt.Size = new Size(6, 30);
            butt.SizeMode = PictureBoxSizeMode.StretchImage;
            butt.Left = plane.Left + plane.Width / 2 - butt.Width / 2;
            butt.Top = plane.Top - butt.Height;
            Game.Controls.Add(butt);
            Bull.Add(butt);
        }

        // 补给箱子
        private void Dazhao_Tick(object sender, EventArgs e)
        {
            PictureBox buji = new PictureBox();
            buji.Image = Image.FromFile(@"../../img/11.jpg");
            buji.SizeMode = PictureBoxSizeMode.StretchImage;
            buji.Tag = "bj";
            buji.Size = new Size(30, 30);
            buji.Location = new Point(ran.Next(Game.Width - buji.Width), -buji.Width);
            Game.Controls.Add(buji);
        }
        //  己方飞机拖尾
        private void F1tim_Tick(object sender, EventArgs e)
        {
            fr1.Image = imageList2.Images[(int)fr1.Tag];
            fr1.Tag = (int)fr1.Tag + 1;
            fr2.Image = imageList2.Images[(int)fr2.Tag];
            fr2.Tag = (int)fr2.Tag + 1;
            if ((int)fr1.Tag > 1)
            {
                fr1.Tag = 0;
                fr2.Tag = 0;
            }
        }
        // 创建敌机
        List<PictureBox> Enemys = new List<PictureBox>();
        private void Em_timer_Tick(object sender, EventArgs e)
        {
            PictureBox enemyimg = new PictureBox();
            enemyimg.Size = new Size(50, 50);
            enemyimg.Tag = "enemyimg";
            if (conve >= 10)
            {
                 enemyimg.Image = Image.FromFile(@"../../img/Enemy3_" + ran.Next(1, 4) + ".png");
            }
            else
            {
                enemyimg.Image = Image.FromFile(@"../../img/Enemy3_" + ran.Next(1, 4) + ".png");
            }
            enemyimg.SizeMode = PictureBoxSizeMode.StretchImage;
            enemyimg.Location = new Point(ran.Next(Game.Width - enemyimg.Width), 0);
            Game.Controls.Add(enemyimg);
            Enemys.Add(enemyimg);
        }
        // 键盘发射子弹
        List<PictureBox> Bull = new List<PictureBox>();
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 'x')
            {
                SoundPlayer player = new SoundPlayer(@"../../music/bullet.wav");
                player.Play();
                PictureBox butt = new PictureBox();
                butt.Tag = "zidan";
                butt.Image = Image.FromFile(@"../../img/1.png");
                butt.Size = new Size(6, 30);
                butt.SizeMode = PictureBoxSizeMode.StretchImage;
                butt.Left = plane.Left + plane.Width / 2 - butt.Width / 2;
                butt.Top = plane.Top - butt.Height;
                Game.Controls.Add(butt);
                Bull.Add(butt);
            }
        }
        // 飞机跟随鼠标移动
        private void Game_MouseMove(object sender, MouseEventArgs e)
        {
            Cursor.Hide();
            plane.Left = MousePosition.X - this.Left - Game.Left - plane.Width / 2;
            plane.Top = MousePosition.Y - this.Top - Game.Top - plane.Height / 2;
            fr1.Location = new Point(plane.Left + plane.Width / 5, plane.Top + plane.Height);
            fr2.Location = new Point(plane.Left + plane.Width / 2, plane.Top + plane.Height);
        }
        // 子弹上升计时器
        int conve = 0;
        private void Timertop_Tick(object sender, EventArgs e)
        {
            foreach (Control item in Game.Controls)
            {
                // 敌机下落
                if (item.Tag.ToString() == "enemyimg")
                {
                    item.Top += 1;
                    if (item.Top > Game.Height)
                    {
                        item.Dispose();
                    }
                   
                    if (item.Left >= plane.Left &&
                        item.Left <= plane.Left + plane.Width &&
                        item.Top >= plane.Top &&
                        item.Top <= plane.Left + plane.Height)
                    {
                        item.Dispose();
                        plane.Dispose();
                        fr1.Dispose();
                        fr2.Dispose();
                       // f1tim.Stop();
                        timertop.Stop();
                        enem_timer.Stop();
                        SoundPlayer player = new SoundPlayer(@"../../music/use_bomb.wav");
                        player.Play();
                        baozha(item);
                        MessageBox.Show("Game Over");

                    }
                }
                // 子弹上升
                if (item.Tag.ToString() == "zidan")
                {
                    item.Top -= 10;
                    if (item.Top < -item.Height)
                    {
                        item.Dispose();
                    }
                    // 子弹敌机碰撞
                    foreach (Control enemy in Game.Controls)
                    {
                        if (enemy.Tag.ToString() == "enemyimg")
                        {
                            if (item.Top <= enemy.Top + enemy.Height &&
                            item.Left >= enemy.Left &&
                            item.Left + item.Width <= enemy.Left + enemy.Width)
                            {
                                item.Dispose();
                                enemy.Dispose();
                                SoundPlayer player = new SoundPlayer(@"../../music/enemy0_down.wav");
                                player.Play();
                                conve += 1;
                                baozha(enemy);
                            }
                        }

                    }

                }
                // 补给清屏                
                if (item.Tag.ToString() == "bj")
                {
                    item.Top += 3;
                    /*if (item.Top <= plane.Top + plane.Height &&
                    item.Left >= plane.Left &&
                    item.Left + item.Width <= plane.Left + plane.Width)*/
                    if (item.Left >= plane.Left &&
                       item.Left <= plane.Left + plane.Width &&
                       item.Top >= plane.Top &&
                       item.Top <= plane.Left + plane.Height)
                    {
                        item.Dispose();
                        SoundPlayer player = new SoundPlayer(@"../../music/get_double_laser.wav");
                        player.Play();
                        qingpingmu();
                    }
                }
            }
        }
        // 爆炸动画
        private void Bomtim_Tick(object sender, EventArgs e)
        {
            Timer bomtim = sender as Timer;
            PictureBox bomimgs = bomtim.Tag as PictureBox;
            bomimgs.Image = imageList1.Images[(int)bomimgs.Tag];
            bomimgs.Tag = (int)bomimgs.Tag + 1;
            if ((int)bomimgs.Tag >= 31)
            {
                bomimgs.Dispose();
                bomtim.Dispose();
            }
        }
        // 清空屏幕
        private void qingpingmu()
        {

            foreach (PictureBox enemy in Enemys)
            {
                enemy.Dispose();
            }
        }

        // 爆炸图片
        private void baozha(Control control)
        {
            PictureBox bomimgs = new PictureBox();
            bomimgs.Tag = 0;
            bomimgs.Size = new Size(50, 50);
            bomimgs.Location = new Point
           (control.Left + control.Width / 2 - bomimgs.Width / 2,
           control.Top + control.Height / 2 - bomimgs.Height / 2);
            bomimgs.Image = imageList1.Images[0];
            Game.Controls.Add(bomimgs);
            // 创建爆炸图片切换计时器
            Timer bomtim = new Timer();
            bomtim.Tag = bomimgs;
            bomtim.Interval = 20;
            bomtim.Start();
            bomtim.Tick += Bomtim_Tick;
        }
    }
}

运行结果:C#飞机大战(随机位置)_第1张图片

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