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();
}
Random sj = new Random();
//游戏界面
PictureBox gm = new PictureBox();
//动物图片
PictureBox tu = new PictureBox();
//计分器
Label defen = new Label();
//随机字母
Timer timer1 = new Timer();
//字母下落
Timer timer2 = new Timer();
//动物向右走
Timer timer4 = new Timer();
Timer timer3 = new Timer();
//记录掉下分数
Label ji = new Label();
ProgressBar HS = new ProgressBar();
// lab进度条背景
/* Label HSback = new Label();
// lab进度条
Label HSjindu = new Label();*/
// 记录血量
int xt = 100;
private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
this.Text = "飞机大战";
this.BackgroundImage = Image.FromFile("../../img/2.jpg");
this.BackgroundImageLayout = ImageLayout.Stretch;
//游戏界面
//PictureBox gm = new PictureBox();
gm.Width = 1000;
gm.Height = 600;
gm.Tag = "gm";
gm.Location = new Point(50,25);
gm.BackColor = Color.Green;
this.Controls.Add(gm);
/*//随机字母
Timer timer1 = new Timer();*/
timer1.Interval = 800;
//timer1.Start();
timer1.Tick += Timer1_Tick;
/*//字母下落
Timer timer2 = new Timer();*/
timer2.Interval = 20;
//timer2.Start();
timer2.Tick += Timer2_Tick;
//键盘消失
this.KeyPress += Form1_KeyPress;
//动物动图
gm.Controls.Add(tu);
tu.Image = imageList1.Images[0];
tu.Width = 160;
tu.Height = 100;
tu.Tag = "tu";
tu.Left = gm.Width - tu.Width;
/*Timer timer3 = new Timer();*/
timer3.Start();
timer3.Interval = 50;
timer3.Tick += Timer3_Tick;
/*Timer timer4 = new Timer();*/
//timer4.Start();
timer4.Interval = 70;
timer4.Tick += Timer4_Tick;
//PictureBox wf = new PictureBox();
wf.Size = new Size(100,80);
wf.Image = Image.FromFile("../../img/Boss.png");
wf.SizeMode = PictureBoxSizeMode.StretchImage;
wf.Location = new Point(gm.Width/2-wf.Width/2,gm.Height-wf.Height);
wf.Tag = "wfei";
gm.Controls.Add(wf);
//计分器
//Label defen = new Label();
defen.Location = new Point(gm.Right + 10, 30);
defen.Size = new Size(100,40);
defen.BackColor = Color.White;
defen.Font = new Font("",20);
defen.Text = x + "得分";
this.Controls.Add(defen);
//记录掉下分数
//Label ji = new Label();
ji.Location = new Point(gm.Right + 10, 260);
ji.Size = new Size(170, 40);
ji.BackColor = Color.White;
ji.Font = new Font("", 20);
ji.Text = "记录分数" + b;
this.Controls.Add(ji);
//开始暂停按钮
//Label ks = new Label();
ks.Location = new Point(gm.Right + 10, 100);
ks.Size = new Size(120, 40);
ks.BackColor = Color.White;
ks.Font = new Font("", 20);
ks.Text = "开始游戏";
this.Controls.Add(ks);
ks.Click += Ks_Click;
//游戏重置
Label zixin = new Label();
zixin.Text = "游戏重置";
zixin.AutoSize = true;//字体自适应大小
zixin.Size = new Size(100, 40);
zixin.Location = new Point(gm.Right + 10, 170);
zixin.Font = new Font("", 30, FontStyle.Bold);
this.Controls.Add(zixin);
zixin.Click += Zixin_Click;
// 系统自带进度条血条实现
Label laa = new Label();
laa.Font = new Font("", 10);
laa.AutoSize = true;
//laa.ForeColor = Color.Red;
laa.Text = "血量";
laa.Location = new Point(gm.Right + 10, 330);
this.Controls.Add(laa);
HS.Location = new Point(gm.Right + 10, 360);
HS.Size = new Size(100, 20);
HS.Value = xt;//血量位置
this.Controls.Add(HS);
// 自定义血条实现
/* HSjindu.Size = new Size(100, 20);
HSjindu.Location = new Point(gm.Right + 10, 390);
HSjindu.BackColor = Color.Red;
HSback.Size = new Size(xt, 20);
HSback.Location = new Point(10, 320);
HSback.BackColor = Color.Black;*/
}
//重置
/*
* 1.先创建这个 List<Label> congzhhidj = new List<Label>();
* 2.然后给congzhhidj.Add(字母生成的名字);
* 3.private void cz()创建一个事件,在foreach便利每一个字符,然后清除congzhhidj的所有内容
* 然后在重置添加一个点击按钮调用cz,停止字母生成和下落,开始按钮从新定义
*/
List<Label> congzhhidj = new List<Label>();
private void cz()
{
foreach (Label item in congzhhidj)
{
item.Dispose();
}
}
private void Zixin_Click(object sender, EventArgs e)
{
timer1.Stop();
timer2.Stop();
timer4.Stop();
cz();
ks.Text = "开始游戏";
}
Label ks = new Label();
private void Ks_Click(object sender, EventArgs e)
{
if (ks.Text == "开始游戏")
{
timer1.Start();
timer2.Start();
timer4.Start();
ks.Text = "暂停";
}
else if (ks.Text == "暂停")
{
timer1.Stop();
timer2.Stop();
timer4.Stop();
ks.Text = "开始游戏";
}
}
//得分记录数字
int x = 0;
PictureBox wf = new PictureBox();
private void Timer4_Tick(object sender, EventArgs e)
{
tu.Left -= 5;
if (tu.Left <= 0)
{
tu.Left =gm.Width;
}
}
int a = 0;
private void Timer3_Tick(object sender, EventArgs e)
{
tu.Image = imageList1.Images[a];
a++;
if (a>=10)
{
a = 0;
}
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
foreach (Control item in gm.Controls)
{
if (item.GetType().Name=="Label")
{
if (item.Text == e.KeyChar.ToString()&&item.Tag.ToString()=="la")
{
//item.Dispose();
wf.Left = item.Left + item.Width / 2 - wf.Width / 2;
//标记,使子弹对应一个字母发射
item.Tag = "bj";
PictureBox zidan = new PictureBox();
zidan.Size = new Size(20, 30);
zidan.Tag = "zd";
zidan.Image = Image.FromFile("../../img/zidan.png");
zidan.SizeMode = PictureBoxSizeMode.StretchImage;
zidan.Location = new Point(wf.Left + wf.Width / 2 - zidan.Width / 2, wf.Top - wf.Height/2);
gm.Controls.Add(zidan);
return;
}
}
}
}
int b = 0;
private void Timer2_Tick(object sender, EventArgs e)
{
foreach (Control itme in gm.Controls)
{
if (itme.GetType().Name=="Label")
{
itme.Top += 3;
int x = 0;
if (itme.Top>=gm.Height)
{
/* b++;
ji.Text = "记录分数" + b;
if (b>=20)
{
timer1.Stop();
timer2.Stop();
timer3.Stop();
timer4.Stop();
MessageBox.Show("掉落20个");
}
itme.Dispose();*/
//int x = 0;
x++;
ji.Text = "记录分数为" + x;
xt -= 10;
HS.Value = xt;//每次减20
HS.Width = xt;//宽每次减20
if (xt == 0)//100每次减20最后减到0,进入这个条件
{
timer1.Stop();
timer2.Stop();
timer3.Stop();
timer4.Stop();
MessageBox.Show("以掉落5个,游戏结束");//弹框
/* Label ji = new Label();
ji.Location = new Point(50,150);
ji.Text = "记录分数为"+x;
kaishi.Controls.Add(ji);*/
}
itme.Dispose(); //大
}
}
if (itme.GetType().Name== "PictureBox")
{
if (itme.Tag.ToString()=="zd")
{
itme.Top -= 3;
if (gm.Top-itme.Height >= itme.Top)
{
itme.Dispose();
}
foreach (Control itemm in gm.Controls)
{
//if (itemm.Tag.ToString()=="la")//也可以就是不好弄接下来子弹对应一个字母
if (itemm.GetType().Name == "Label")
{
/* if (itme.Left<= itemm.Left+ itemm.Width/2- itme.Width/2&& itme.Top<=itemm.Top+itemm.Height)*/
if(itme.Top <= itemm.Top + itemm.Height && itme.Left + itme.Width / 2 <= itemm.Left + itemm.Width / 2)
{
//控制记录分数
x++;
defen.Text = x + "得分";
itme.Dispose();
itemm.Dispose();
if (x>=5)
{
itme.Top = -20;
timer1.Interval = 500;
timer4.Interval = 50;
timer2.Interval = 30;
}
//爆炸效果
PictureBox baozha = new PictureBox();
baozha.Size = new Size(50,50);
baozha.SizeMode = PictureBoxSizeMode.StretchImage;
baozha.Tag = 0;
baozha.Location = new Point(itemm.Left + itemm.Width / 2 - baozha.Width / 2, itemm.Top + itemm.Height/2-baozha.Height/2);
gm.Controls.Add(baozha);
Timer tbaozha = new Timer();
tbaozha.Interval = 70;
tbaozha.Start();
tbaozha.Tag = baozha;
tbaozha.Tick += Tbaozha_Tick;
}
}
}
}
}
}
}
private void Tbaozha_Tick(object sender, EventArgs e)
{
Timer s1=(Timer)sender;
PictureBox s2=(PictureBox)s1.Tag;
s2.Image = imageList2.Images[(int)s2.Tag];
s2.Tag = (int)s2.Tag + 1;
if ((int)s2.Tag==32)
{
s1.Dispose();
s2.Dispose();
}
}
private void Timer1_Tick(object sender, EventArgs e)
{
if (tu.Left >= 0 && tu.Left <= gm.Width - tu.Width)
{
Label label = new Label();
label.Text =((char) sj.Next(97,123)).ToString();
label.Font = new Font("",sj.Next(20,30));
label.Tag = "la";
/*label.Top = tu.Height;
label.Left = sj.Next(0, gm.Width - label.Width);//字体的left位置随机*/
label.Top = tu.Height;
label.Left = tu.Left + tu.Width / 2 - label.Width / 2;
/*label.Top = tu.Height;
label.Left = tu.Left + tu.Width / 2 - label.Width / 2;*/
label.AutoSize = true;//自适应字体大小
congzhhidj.Add(label);
gm.Controls.Add(label);
}
}
}
}
打字游戏(代码+注释)
最新推荐文章于 2023-01-01 19:49:34 发布