打字游戏
游戏界面


程序代码
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;
using System.Media;
namespace 飞机大战
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Panel BG = new Panel();
Panel cebainqu = new Panel();
PictureBox player = new PictureBox();
Random rand = new Random();
Timer CreateTime = new Timer();
Timer Flytime = new Timer();
Label btn = new Label();
Label defeng = new Label();
Label xuetiao = new Label();
int count = 0;
SoundPlayer baozhasound = new SoundPlayer(@"../../music/game_over.wav");
PictureBox feijwei1 = new PictureBox();
PictureBox feijwei2 = new PictureBox();
List<Label> labels = new List<Label>();
List<PictureBox> picts = new List<PictureBox>();
private void Form1_Load(object sender, EventArgs e)
{
this.Size = new Size(1000, 700);
this.Text = "字母大战";
this.BackColor = Color.FromArgb(80, 00, 80);
this.Left = Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2;
this.Top = Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2;
BG.Width = 800;
BG.Height = 600;
BG.BackColor = Color.White;
this.Controls.Add(BG);
BG.Location = new Point(20, 20);
CreateTime.Interval = 1000;
CreateTime.Tick += CreateTime_Tick;
Flytime.Interval = 40;
Flytime.Tick += Flytime_Tick;
player.Size=new Size(80, 80);
player.Top = BG.Height - player.Height-50;
player.Left = BG.Width / 2 - player.Width / 2;
player.Image = Image.FromFile(@"../../img/YP03.png");
player.SizeMode = PictureBoxSizeMode.StretchImage;
player.Tag = "player";
BG.Controls.Add(player);
feijwei1.Size = new Size(15, 30);
feijwei1.Tag = 0;
feijwei1.Top = player.Top + player.Height+feijwei1.Height/2-15;
feijwei1.Left = player.Left + player.Width / 2 -feijwei1.Width-5;
feijwei1.Image = imageList2.Images[0];
BG.Controls.Add(feijwei1);
feijwei2.Size = new Size(15, 30);
feijwei2.Tag = 0;
feijwei2.Top= player.Top + player.Height + feijwei1.Height / 2 -15;
feijwei2.Left = player.Left + player.Width / 2 + feijwei1.Width-5;
feijwei2.Image = imageList3.Images[0];
BG.Controls.Add(feijwei2);
Timer weiqitimer1 = new Timer();
weiqitimer1.Tag = feijwei1;
weiqitimer1.Tick += Weiqitimer1_Tick;
weiqitimer1.Start();
Timer weiqitimer2 = new Timer();
weiqitimer2.Tag = feijwei2;
weiqitimer2.Tick += Weiqitimer2_Tick;
weiqitimer2.Start();
this.KeyPress += Form1_KeyPress;
cebainqu.Width = 160;
cebainqu.Height = 600;
cebainqu.Location = new Point(820,20);
cebainqu.BackColor = Color.FromArgb(180, 15, 123);
this.Controls.Add(cebainqu);
btn.Location = new Point(20, 20);
btn.BorderStyle = BorderStyle.FixedSingle;
btn.AutoSize = true;
btn.Text = "游戏开始";
btn.Cursor = Cursors.Hand;
btn.Font = new Font("", 15);
btn.ForeColor = Color.FromArgb(97,177,48);
btn.BackColor = Color.FromArgb(191,143,243);
cebainqu.Controls.Add(btn);
btn.Click += Btn_Click;
defeng.Font = new Font("", 15);
defeng.Location = new Point(20, 50);
defeng.AutoSize = true;
defeng.Text = "得分: "+count+"分";
cebainqu.Controls.Add(defeng);
Label xueTiao = new Label();
xueTiao.Text = " 能量";
xueTiao.Size = new Size(100, 30);
xueTiao.Font = new Font("楷体",20);
xueTiao.ForeColor = Color.Yellow;
xueTiao.Location = new Point(20, 70);
cebainqu.Controls.Add(xueTiao);
xuetiao.Size = new Size(100, 20);
xuetiao.BackColor = Color.Red;
xuetiao.Location = new Point(20, 100);
cebainqu.Controls.Add(xuetiao);
Label xuetdi = new Label();
xuetdi.Size = new Size(100, 20);
xuetdi.BackColor = Color.White;
xuetdi.Location = new Point(20, 100);
cebainqu.Controls.Add(xuetdi);
}
private void Weiqitimer1_Tick(object sender, EventArgs e)
{
Timer weiqi1 = sender as Timer;
PictureBox weiQi = weiqi1.Tag as PictureBox;
weiQi.Image = imageList2.Images[(int)weiQi.Tag];
weiQi.Tag = (int)weiQi.Tag + 1;
if ((int)weiQi.Tag > 1)
{
weiQi.Tag = 0;
}
}
private void Weiqitimer2_Tick(object sender, EventArgs e)
{
Timer weiqi2 = sender as Timer;
PictureBox weiQi = weiqi2.Tag as PictureBox;
weiQi.Image = imageList3.Images[(int)weiQi.Tag];
weiQi.Tag = (int)weiQi.Tag + 1;
if ((int)weiQi.Tag>1)
{
weiQi.Tag = 0;
}
}
private void Btn_Click(object sender, EventArgs e)
{
if (btn.Text=="游戏开始")
{
CreateTime.Start();
Flytime.Start();
btn.BackColor = Color.Red;
btn.ForeColor = Color.White;
btn.Text = "游戏暂停";
}
else if(btn.Text=="游戏暂停")
{
CreateTime.Stop();
Flytime.Stop();
btn.BackColor = Color.FromArgb(191, 143, 243);
btn.ForeColor = Color.FromArgb(97, 177, 48);
btn.Text = "游戏开始";
}
}
private void CreateTime_Tick(object sender, EventArgs e)
{
Label lb = new Label();
lb.Text = ((char)rand.Next(97, 123)).ToString();
lb.Font = new Font("", rand.Next(20, 30));
lb.ForeColor =Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
lb.Top = 0;
lb.Left = rand.Next(0, BG.Width - lb.Width);
lb.AutoSize = true;
lb.BackColor = Color.Transparent;
lb.Tag = "zimu";
BG.Controls.Add(lb);
labels.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 += 5;
if (item.Top > BG.Height)
{
item.Dispose();
xuetiao.Width -= 10;
if (xuetiao.Width==0)
{
CreateTime.Stop();
Flytime.Stop();
qing();
zdqing();
xuetiao.Size = new Size(100, 20);
defeng.Text = "得分: " + count + "分";
btn.Text = "游戏开始";
MessageBox.Show("游戏结束"+"得分为:"+count+"分","游戏结束",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
count = 0;
defeng.Text = "得分: " + count + "分";
}
}
}
if (item.Tag.ToString()=="zidan")
{
item.Top -= 5;
if (item.Top<0)
{
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();
PictureBox bombBox = new PictureBox();
bombBox.Tag = 0;
bombBox.Size = new Size(50, 50);
bombBox.Location = new Point(zm.Left + zm.Width / 2 - bombBox.Width / 2, zm.Top - zm.Height / 2 + bombBox.Height / 2);
BG.Controls.Add(bombBox);
Timer bombTimer = new Timer();
bombTimer.Tag = bombBox;
bombTimer.Interval = 10;
bombTimer.Tick += BombTimer_Tick;
bombTimer.Start();
count++;
defeng.Text = "得分: " + count.ToString() + "分";
baozhasound.Play();
}
}
}
}
}
}
private void BombTimer_Tick(object sender, EventArgs e)
{
Timer bombtimer = (Timer)sender;
PictureBox picture = (PictureBox)bombtimer.Tag;
picture.Image = imageList1.Images[(int)picture.Tag];
picture.Tag = (int)picture.Tag + 1;
if ((int)picture.Tag>31)
{
bombtimer.Dispose();
picture.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;
feijwei1.Left = player.Left + player.Width / 2 - feijwei1.Width - 5;
feijwei2.Left = player.Left + player.Width / 2 + feijwei1.Width - 5;
PictureBox bullet = new PictureBox();
bullet.Size = new Size(8,28);
bullet.Tag = "zidan";
bullet.Image = Image.FromFile(@"../../img/Ammo1.png");
bullet.SizeMode = PictureBoxSizeMode.StretchImage;
bullet.Left = player.Left + player.Width / 2 - bullet.Width / 2;
bullet.Top = player.Top - bullet.Height;
BG.Controls.Add(bullet);
picts.Add(bullet);
return;
}
}
}
private void qing()
{
foreach (Label item in labels)
{
item.Dispose();
}
}
private void zdqing()
{
foreach (PictureBox item in picts)
{
item.Dispose();
}
}
private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}