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 打字游戏2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random r = new Random();
//游戏区
PictureBox box = new PictureBox();
//飞机
PictureBox fei = new PictureBox();
//按键区
PictureBox kai = new PictureBox();
//子弹生成器
Timer zi = new Timer();
//下落
Timer Fly = new Timer();
//敌机生成器
Timer zm = new Timer();
//尾翼
PictureBox wei = new PictureBox();
PictureBox weiyi = new PictureBox();
//音效
SoundPlayer ying = new SoundPlayer();
SoundPlayer yin = new SoundPlayer();
//SoundPlayer yinxiao = new SoundPlayer();
//血条
Label xue = new Label();
//积分器
Label defen = new Label();
//开始/暂停按钮
Label ting = new Label();
private void Form1_Load(object sender, EventArgs e)
{
this.Size = new Size(800,500);
this.BackColor = Color.Cyan;
this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
//游戏区
box.Width = 600;
box.Height = 450;
box.Location = new Point(0,10);
box.BackColor = Color.White;
this.Controls.Add(box);
//按键区
kai.Width = 200;
kai.Height = 400;
kai.BackColor = Color.Blue;
this.Controls.Add(kai);
kai.Location = new Point(0, 10);
kai.Left = box.Width + box.Left;
//开始/暂停按钮
//Label ting = new Label();
ting.Tag = "anan";
ting.Size = new Size(150, 40);
ting.BackColor = Color.Yellow;
ting.Text = "开始";
ting.Font = new Font("", 35);
ting.AutoSize = true;
kai.Controls.Add(ting);
ting.Left = kai.Width / 2 - ting.Width / 2;
ting.Click += Ting_Click;
//敌机生成器
//Timer zm = new Timer();
zm.Interval = 1000;
zm.Tick += Zm_Tick;
//zm.Start();
//下落
//Timer Fly = new Timer();
Fly.Interval = 10;
Fly.Tick += Fly_Tick;
//Fly.Start();
//飞机
fei.Tag = "fei";
fei.Image = Image.FromFile(@"../../img/Enemy2.png");
fei.Size = new Size(60,60);
fei.SizeMode = PictureBoxSizeMode.StretchImage;
fei.Left = box.Width/2 - fei.Width/2;
fei.Top = box.Height - fei.Height-40;
fei.BackColor = Color.Transparent;
box.Controls.Add(fei);
//尾翼
wei.Tag = "wei";
wei.Size = new Size(30, 40);
wei.Left = fei.Left + fei.Width / 2;
wei.Top = fei.Top + fei.Height;
wei.Image = imageList1.Images[0];
box.Controls.Add(wei);
weiyi.Tag = "weiji";
weiyi.Image = imageList2.Images[0];
weiyi.Size = new Size(30, 40);
weiyi.Left = fei.Left + fei.Width /2-28;
weiyi.Top = fei.Top + fei.Height;
box.Controls.Add(weiyi);
Timer yi = new Timer();
yi.Interval = 100;
yi.Tick += Yi_Tick;
yi.Start();
//子弹生成器
//Timer zi = new Timer();
zi.Interval = 500;
zi.Tick += Zi_Tick;
//zi.Start();
//键盘事件
this.KeyPress += Form1_KeyPress;
this.KeyDown += Form1_KeyDown;
//爆炸音效
ying.SoundLocation = "../../music/爆炸敌机.wav";
yin.SoundLocation = "../../music/爆炸死.wav";
//yinxiao.SoundLocation = "../../music/子弹.wav";
//血条
xue.Tag = "xue";
xue.Size = new Size(60,5);
xue.BackColor = Color.Red;
xue.Left = fei.Left + fei.Width - xue.Width;
xue.Top = fei.Top - xue.Height;
box.Controls.Add(xue);
//记录得分
defen.Tag = "de";
defen.Size = new Size(100, 50);
defen.BackColor = Color.Transparent;
defen.AutoSize = true;
defen.Location = new Point(30, 80) ;
defen.Font = new Font("", 20);
defen.Text = "分数:0分";
kai.Controls.Add(defen);
}
//按钮
private void Ting_Click(object sender, EventArgs e)
{
Label la = (Label)sender;
if (la.Text == "开始")
{
zi.Start();
Fly.Start();
zm.Start();
la.Text = "暂停";
}
else if (la.Text == "暂停")
{
zi.Stop();
Fly.Stop();
zm.Stop();
la.Text = "开始";
}
}
//尾翼动画
int t = 0, y = 0;
private void Yi_Tick(object sender, EventArgs e)
{
wei.Image = imageList1.Images[t];
t++;
if (t >= imageList1.Images.Count)
{
t = 0;
}
weiyi.Image = imageList2.Images[y];
y++;
if (y >= imageList2.Images.Count)
{
y = 0;
}
}
private void Zi_Tick(object sender, EventArgs e)
{
//子弹
PictureBox dan = new PictureBox();
dan.Tag = "zidan";
dan.Image = Image.FromFile(@"../../img/fire.png");
dan.Size = new Size(40, 40);
dan.BackColor = Color.Transparent;
dan.SizeMode = PictureBoxSizeMode.StretchImage;
dan.Left = fei.Left + fei.Width / 2 - dan.Width / 2;
dan.Top = fei.Top - dan.Height;
box.Controls.Add(dan);
}
//键盘事件
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode==Keys.Left)
{
fei.Left -= 15;
wei.Left -= 15;
weiyi.Left -= 15;
xue.Left -= 15;
if (fei.Left==0)
{
fei.Left = 15;
wei.Left = fei.Left + fei.Width / 2;
wei.Top = fei.Top + fei.Height;
weiyi.Left = fei.Left + fei.Width / 2 - 28;
weiyi.Top = fei.Top + fei.Height;
xue.Left = fei.Left + fei.Width - xue.Width;
xue.Top = fei.Top - xue.Height;
}
}
if (e.KeyCode == Keys.Right)
{
fei.Left += 15;
wei.Left += 15;
weiyi.Left += 15;
xue.Left += 15;
if (fei.Left+fei.Width>=box.Width)
{
fei.Left = box.Width - fei.Width;
wei.Left = fei.Left + fei.Width / 2;
wei.Top = fei.Top + fei.Height;
weiyi.Left = fei.Left + fei.Width / 2 - 28;
weiyi.Top = fei.Top + fei.Height;
xue.Left = fei.Left + fei.Width - xue.Width;
xue.Top = fei.Top - xue.Height;
}
}
if (e.KeyCode == Keys.Down)
{
fei.Top += 15;
wei.Top += 15;
weiyi.Top += 15;
xue.Top += 15;
if (fei.Top+fei.Height+wei.Height>=box.Height)
{
fei.Top = box.Height - fei.Height - wei.Height ;
wei.Left = fei.Left + fei.Width / 2;
wei.Top = fei.Top + fei.Height;
weiyi.Left = fei.Left + fei.Width / 2 - 28;
weiyi.Top = fei.Top + fei.Height;
xue.Left = fei.Left + fei.Width - xue.Width;
xue.Top = fei.Top - xue.Height;
}
}
if (e.KeyCode == Keys.Up)
{
fei.Top -= 15;
wei.Top -= 15;
weiyi.Top -= 15;
xue.Top -= 15;
if (fei.Top <= 0)
{
fei.Top = 15;
wei.Left = fei.Left + fei.Width / 2;
wei.Top = fei.Top + fei.Height;
weiyi.Left = fei.Left + fei.Width / 2 - 28;
weiyi.Top = fei.Top + fei.Height;
xue.Left = fei.Left + fei.Width - xue.Width;
xue.Top = fei.Top - xue.Height;
}
}
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
}
int a = 0;
int b = 0;
//下落
private void Fly_Tick(object sender, EventArgs e)
{
foreach (Control item in box.Controls)
{
if (item.Tag.ToString()=="zm")
{
item.Top += 2;
if (item.Top > box.Height)
{
a++;
if (item.Top>box.Top)
{
xue.Width -= 10;
}
if (xue.Width==0)
{
yin.Play();
zm.Stop();
zi.Stop();
Fly.Stop();
item.Dispose();
xue.Dispose();
fei.Dispose();
wei.Dispose();
weiyi.Dispose();
PictureBox baozha = new PictureBox();
baozha.Tag = 0;
baozha.Size = new Size(30, 30);
baozha.Location = new Point(fei.Left + fei.Width / 2 - baozha.Width / 2, fei.Top + fei.Height / 2 - baozha.Height / 2);
box.Controls.Add(baozha);
Timer zha = new Timer();
zha.Tag = baozha;
zha.Interval = 70;
zha.Tick += Zha_Tick;
zha.Start();
}
item.Dispose();
}
//敌机与飞机碰撞
if (item.Left >= fei.Left && item.Left <= fei.Left + fei.Width && item.Top >= fei.Top && item.Top <= fei.Left + fei.Height)
{
yin.Play();
zm.Stop();
zi.Stop();
Fly.Stop();
item.Dispose();
xue.Dispose();
fei.Dispose();
wei.Dispose();
weiyi.Dispose();
//爆炸效果
PictureBox baozha = new PictureBox();
baozha.Tag = 0;
baozha.Size = new Size(30, 30);
baozha.Location = new Point(fei.Left + fei.Width / 2 - baozha.Width / 2, fei.Top + fei.Height / 2 - baozha.Height / 2);
box.Controls.Add(baozha);
Timer zha = new Timer();
zha.Tag = baozha;
zha.Interval = 70;
zha.Tick += Zha_Tick;
zha.Start();
}
}
if (item.Tag.ToString() == "zidan")
{
//yinxiao.Play();
item.Top -= 5;
if (item.Top < -item.Height)
{
item.Dispose();
}
//敌机与子弹碰撞
foreach (Control yii in box.Controls)
{
if (yii.Tag.ToString() == "zm")
{
if (item.Left >= yii.Left && item.Left <= yii.Left + yii.Width && item.Top >= yii.Top && item.Top <= yii.Top + yii.Height)
{
b++;
defen.Text = "分数:" + b + "分";
ying.Play();
item.Dispose();
yii.Dispose();
//ying.Play();
//爆炸效果
PictureBox bao = new PictureBox();
bao.Tag = 0;
bao.Size = new Size(30, 30);
bao.Location = new Point(yii.Left + yii.Width / 2 - bao.Width / 2, yii.Top + yii.Height / 2 - bao.Height / 2);
box.Controls.Add(bao);
Timer zha = new Timer();
zha.Tag = bao;
zha.Interval = 70;
zha.Tick += Zha_Tick;
zha.Start();
}
}
}
}
}
}
//爆炸特效
private void Zha_Tick(object sender, EventArgs e)
{
Timer zha = (Timer)sender;
PictureBox picture = (PictureBox)zha.Tag;
picture.Image = imageList3.Images[(int)picture.Tag];
picture.Tag = (int)picture.Tag + 1;
if ((int)picture.Tag > 31)
{
zha.Dispose();
picture.Dispose();
}
}
private void Zm_Tick(object sender, EventArgs e)
{
//敌机
PictureBox lb = new PictureBox();
lb.Tag = "zm";
lb.Image = Image.FromFile(@"../../img/Enemy1.png");
lb.Size = new Size(30,30);
lb.SizeMode = PictureBoxSizeMode.StretchImage;
//背景透明
lb.BackColor = Color.Transparent;
//位置随机
lb.Left = r.Next(0,box.Width-lb.Width);
//从上到下
lb.Top = 0;
box.Controls.Add(lb);
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}