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);
Timer CreateTime = new Timer();
CreateTime.Interval = 750;
CreateTime.Tick += CreateTime_Tick;
CreateTime.Start();
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)
{
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();
}
}
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)
{
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;
}
}
}
}
}
运行结果:
