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();
}
//1.随机产生字母
//2.字母下落
//3.按下对应的按键,字母消失
Random rad = new Random();
//创建字母计时器
Timer zimuadd = new Timer();
//字母下落计时器
Timer zimutop = new Timer();
//得分
Label lab = new Label();
//记录分数
int score = 0;
//记录未被消失的字母
int x = 0;
//进度条
ProgressBar HS = new ProgressBar();
//进度条背景
Label HSback = new Label();
//进度条
Label HSjindu = new Label();
//记录血量
int xt = 100;
//定义list泛型集合
List<Label> zimulab = new List<Label>();
private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
this.Text = "打字游戏";
//路径分为两种路径:
//相对路径:指程序从目前自身所运行位置开始,寻找目标位置
//绝对路径
this.BackgroundImage = Image.FromFile("../../img/1.jpg");
this.BackgroundImageLayout = ImageLayout.Stretch;
//游戏区域
panel1.Size = new Size(1000,600);
panel1.BackColor = Color.White;
//鸟
bird.Tag = "bird";
bird.Width = 100;
bird.Height = 80;
bird.SizeMode = PictureBoxSizeMode.StretchImage;
bird.Location = new Point(0,0);
panel1.Controls.Add(bird);
//bird.Image = imageList1.Images[0];
//鸟的动画
//timer2.Start();
//字母的下落动画
// timer1.Start();
//字母
Timer flytime = new Timer();
flytime.Interval = 80;
//事件的添加:+、=、tab、tab
flytime.Tick += Flytime_Tick;
flytime.Start();
//飞机
plane.Size = new Size(80, 80);
plane.SizeMode = PictureBoxSizeMode.StretchImage;
plane.Location = new Point(panel1.Width / 2 - plane.Width / 2, panel1.Height - plane.Height);
plane.Image = Image.FromFile("../../img/RP03.png");
plane.Tag = "plan";
panel1.Controls.Add(plane);
timer3.Start();
//游戏设置栏
cebian.Size = new Size(150, 400);
cebian.BackColor = Color.Green;
cebian.Left = 1020;
cebian.Top = 15;
this.Controls.Add(cebian);
//开始游戏按钮
btnstart.Font = new Font("", 20);
btnstart.AutoSize = true;
btnstart.Location = new Point(10, 20);
btnstart.Text = "开始游戏";
cebian.Controls.Add(btnstart);
btnstart.Click += Btnstart_Click;
// 开始游戏按钮添加进侧边栏
btnstart.Font = new Font("", 20);
btnstart.AutoSize = true;
btnstart.Click += Btnstart_Click;
lab.Text = "得分:0分";
lab.Location = new Point(10, 50);
cebian.Controls.Add(lab);
zimuadd.Tick += Zimuadd_Tick;
zimuadd.Interval = 1000;
zimutop.Tick += Zimutop_Tick;
zimutop.Interval = 1000;
//系统自带进度血条实现
btnstart.Font = new Font("", 20);
btnstart.AutoSize = true;
btnstart.Click += Btnstart_Click;
lab.Text = "得分:0分";
lab.Top = 70;
lab.Font = new Font("",16);
//lab.Location = new Point(70, 50);
cebian.Controls.Add(lab);
Label la = new Label();
la.Font = new Font("", 10);
la.AutoSize = true;
la.Text = "血量值:";
//la.Location = new Point(70, 110);
HS.Left = 10;
HS.Top = 120;
HS.Size = new Size(100, 20);
HS.Value = xt;
cebian.Controls.Add(HS);
}
private void Zimutop_Tick(object sender, EventArgs e)
{
foreach (Control item in panel1.Controls)
{
//字母下落
if (item.Tag.ToString() == "zimu" || item.Tag.ToString() == "biaoji")
{
item.Top += 1;
if (item.Top > panel1.Height)
{
item.Dispose();
//记录学条和得分
x++;
xt = xt - 5;
//xt -= 10;
HS.Value = xt;
HSjindu.Width = xt;
if (xt == 0)
{
zimuadd.Stop();
zimutop.Stop();
MessageBox.Show("游戏结束,你的得分是:" + score);
if (xt == 0)
{
btnstart.Text = "GameStart";
score = 0;
lab.Text = "得分:" + score + "分";
xt = 100;
HS.Value = xt;
// Qingping();
}
else
{
this.Close();
}
}
}
}
}
}
private void Zimuadd_Tick(object sender, EventArgs e)
{
Label zimu = new Label();
zimu.Tag = "zimu";
zimu.Text = ((char)rad.Next(97, 123)).ToString();
zimu.Font = new Font("", rad.Next(20, 40));
zimu.ForeColor = Color.FromArgb(rad.Next(255), rad.Next(255), rad.Next(255));
zimu.BackColor = Color.Transparent;
zimu.Top = 0;
zimu.AutoSize = true;
zimu.Left = rad.Next(0, panel1.Width - zimu.Width);
panel1.Controls.Add(zimu);
zimulab.Add(zimu);
}
private void Btnstart_Click(object sender, EventArgs e)
{
Label btn = (Label)sender;
if (btn.Text == "开始游戏")
{
Flytime.Start();
timer1.Start();
timer2.Start();
btn.Text = "暂停";
}
else if (btn.Text == "暂停")
{
Flytime.Stop();
timer1.Stop();
timer2.Stop();
btn.Text = "开始游戏";
}
}
Timer Flytime = new Timer();
Button btn = new Button();
Label btnstart = new Label();
Panel cebian = new Panel();
PictureBox plane = new PictureBox();
PictureBox bird = new PictureBox();//显示动画的容器
//鸟的动画
int index = 0;
private void Flytime_Tick(object sender, EventArgs e)
{
index++;
bird.Image = imageList1.Images[index];
if (index>=10)
{
index =0;
}
}
Random r = new Random();
private void timer1_Tick(object sender, EventArgs e)
{
if (bird.Left>=0&&bird.Left<=panel1.Width-bird.Width)
{
//每一个Label就是一个字母
Label lb = new Label();
lb.Tag = "zimu";
lb.Text = ((char)r.Next(97, 123)).ToString();
lb.Font = new Font("", r.Next(18, 25));
lb.AutoSize = true;
//计算机中的三原色是:红、绿、蓝
lb.ForeColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
lb.Top = bird.Height;
lb.Left = bird.Left + bird.Width / 2 - lb.Width / 2;
panel1.Controls.Add(lb);
}
}
//鸟
private void timer2_Tick(object sender, EventArgs e)
{
bird.Left += 2;
if (bird.Left>=panel1.Width)
{
bird.Left = -bird.Width;
}
}
private void timer3_Tick(object sender, EventArgs e)
{
//遍历循环语法(寻找)
foreach (Control item in panel1.Controls)
{
//每一个对象都有一个方法GetType
if (item.GetType().Name == "Label")
{
item.Top += 1;
if (item.Top >= panel1.Height)
{
item.Dispose();
//记录学条和得分
x++;
xt = xt - 5;
//xt -= 10;
HS.Value = xt;
HSjindu.Width = xt;
if (xt == 0)
{
zimuadd.Stop();
zimutop.Stop();
MessageBox.Show("游戏结束,你的得分是:" + score);
if (xt == 0)
{
btnstart.Text = "GameStart";
score = 0;
lab.Text = "得分:" + score + "分";
xt = 100;
HS.Value = xt;
// Qingping();
}
else
{
this.Close();
}
}
}
}
else if (item.GetType().Name == "PictureBox")
{
if (item.Tag.ToString() == "zd")
{
item.Top -= 5;
/* if (panel1.Top - item.Height >= item.Top)
{
item.Dispose();
}*/
foreach (Control lb in panel1.Controls)
{
if (lb.Tag.ToString()=="biaoji")
{
if ( item.Top <=lb.Top+lb.Height)
{
lb.Dispose();
item.Dispose();
//每次击中就要爆炸
PictureBox bomb = new PictureBox();
bomb.Size = new Size(50, 50);
bomb.Location = new Point(lb.Left + lb.Width / 2 - bomb.Width / 2, lb.Top + lb.Height / 2 - bomb.Height / 2);
bomb.Tag = 0;
bomb.Image = imageList2.Images[0];
panel1.Controls.Add(bomb);
Timer tbaozha = new Timer();
tbaozha.Interval = 70;
tbaozha.Start();
tbaozha.Tag = bomb;
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 Form1_KeyPress(object sender, KeyPressEventArgs e)
{
//在按下任意键的时候,判断一下本次的按键键值是否有对应的字母
//按键键值=e.KeyChar
//事件:被动行为
//事件参数:e参数---什么事件,e就是什么参数
foreach (Control item in panel1.Controls)
{
if (item.GetType().Name=="Label")
{
/* if (item.Text==e.KeyChar.ToString()&&item.Tag.ToString()=="zimu")*/
if (item.Text == e.KeyChar.ToString())
{
item.Tag = "biaoji";
//则先控制飞机移动到字母正下方
plane.Left = item.Left + item.Width / 2 - plane.Width / 2;
//创建子弹
PictureBox bullet = new PictureBox();
bullet.Size = new Size(6, 30);
bullet.Tag = "zd";
bullet.Image = Image.FromFile("../../img/Ammo1.png");
bullet.SizeMode = PictureBoxSizeMode.StretchImage;
bullet.Left = plane.Left + plane.Width / 2 - bullet.Width / 2;
bullet.Top = plane.Top - bullet.Height;
panel1.Controls.Add(bullet);
//跳出当前事件,return以下的事件中的代码全部无效
return;
}
}
}
}
}
}