C打字游戏案例详细教程
目录
C#打字游戏案例详细教程
C#打字游戏案例详细教程
全部控件的实现以及控件的事件,属性都是使用代码实现,没有拖拉工具箱控件,请熟悉C#属性赋值以及事件绑定。需要源码关注私信我,感谢!!!
话不多说先放图!!!
运行截图↓
设计界面截图↓
实现的效果:1.随机字母a-z(数字同理)看完代码请自行拓展;
2.键盘按到对应键时飞机跟着移动并发射子弹;
3.子弹与字母碰撞,产生爆炸效果;
4.得分面板,消失字母后加分,关卡同理设计;
5.开始暂停游戏通过一个Button实现,也可以用Lable,Button按钮会和定义的键盘事件起冲突,解决方法: this.KeyPreview = true;
6.没按对中字母血条减少,血条为空清空屏幕字母并弹窗,点(重试)数据初始化开始新的一局,点(取消)关闭窗体
7.蓄力值进度条一满,释放大招清空屏幕
话不多说上代码!!!
该项目用到的全局变量↓
// 游戏区域
Panel Game = new Panel();
// 侧边栏区域
Panel cebian = new Panel();
// 己方飞机
Panel plean = new Panel();
// 随机数
Random rad = new Random();
// 控制创建字母计时器-zimu1add
Timer zimuadd = new Timer();
// 控制字母下落计时器-zimutop
Timer zimutop = new Timer();
// 开始游戏按钮
Button btnstart = new Button();
// 得分label
Label lab = new Label();
// 记录分数
int score = 0;
// 进度条
ProgressBar HS = new ProgressBar();
// lab进度条背景
Label HSback = new Label();
// lab进度条
Label HSjindu = new Label();
// 记录血量
int xt = 100;
// 蓄力值
int xy = 0;
// 定义list泛型集合来存储全部字母
List<Label> zimulab = new List<Label>();
窗体的加载事件
private void Form1_Load(object sender, EventArgs e)
{
// 解决button按钮影响键盘事件问题
this.KeyPreview = true;
this.Size = new Size(1000,600);
this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
this.BackColor = Color.White;
// 游戏区域Panel-Game
Game.Size = new Size(800,540);
Game.BackColor = Color.Pink;
Game.Location = new Point(10,10);
this.Controls.Add(Game);
// 添加飞机-plean在游戏区域
plean.Size = new Size(60,60);
plean.Tag = "plean";
plean.Top = Game.Height - plean.Height;
plean.Left = Game.Width / 2 - plean.Width / 2;
plean.BackgroundImage = Image.FromFile(@"../../img/plane_1.png");
plean.BackgroundImageLayout = ImageLayout.Stretch;
Game.Controls.Add(plean);
// 添加游戏设置侧边栏
cebian.Size = new Size(150,540);
cebian.BackColor = Color.Aqua;
cebian.Left = 820;
cebian.Top = 10;
this.Controls.Add(cebian);
// 开始游戏按钮添加进侧边栏
btnstart.Font = new Font("萍方",15);
btnstart.Size = new Size(80,30);
btnstart.AutoSize = true;
btnstart.Cursor = Cursors.Hand;
btnstart.Location = new Point(10, 10);
btnstart.Text = "开始游戏";
btnstart.Click += Btnstart_Click;
cebian.Controls.Add(btnstart);
// 侧边栏显示得分
lab.Font = new Font("萍方",15);
lab.AutoSize = true;
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 = 10;
// 系统自带进度条血条实现
Label la = new Label();
la.Font = new Font("萍方", 10);
la.AutoSize = true;
la.ForeColor = Color.Red;
la.Text = "血量👇";
la.Location = new Point(10, 90);
cebian.Controls.Add(la);
HS.Location = new Point(10,110);
HS.Size = new Size(100,20);
HS.Value = xt;
cebian.Controls.Add(HS);
// 自定义蓄力值实现
Label xu = new Label();
xu.Text = "蓄力值";
xu.Font = new Font("",10);
xu.AutoSize = true;
xu.ForeColor = Color.Blue;
xu.Location = new Point(10,130);
cebian.Controls.Add(xu);
HSback.Size = new Size(100,20);
HSback.Location = new Point(10, 150);
HSback.BackColor = Color.White;
HSjindu.Size = new Size(xy,20);
HSjindu.Location = new Point(10, 150);
HSjindu.BackColor = Color.Red;
cebian.Controls.Add(HSjindu);
cebian.Controls.Add(HSback);
// 窗口键盘事件
this.KeyPress += Form1_KeyPress;
}
Btnstart按钮的单击事件
// 开始/暂停
private void Btnstart_Click(object sender, EventArgs e)
{
if (btnstart.Text=="开始游戏")
{
zimuadd.Start();
zimutop.Start();
btnstart.Text = "暂停";
}else if (btnstart.Text == "暂停")
{
zimuadd.Stop();
zimutop.Stop();
btnstart.Text = "开始游戏";
}
}
Zimuadd计时器用来随机字母
// 控制字母添加
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,31));
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,Game.Width-zimu.Width);
Game.Controls.Add(zimu);
// 添加zimu进泛型集合中
zimulab.Add(zimu);
}
Zimutop计时器用来让字母下落并实现子弹上升与爆炸效果图的产生 (主要逻辑)
// 控制字母下落,子弹上升计时器
private void Zimutop_Tick(object sender, EventArgs e)
{
foreach (Control item in Game.Controls)
{
// 字母下落
if (item.Tag.ToString()=="zimu"||item.Tag.ToString()=="biaoji")
{
item.Top += 3;
if (item.Top>Game.Height)
{
item.Dispose();
// 记录血条与得分
xt -= 10;
HS.Value = xt;
if (xt==0)
{
zimuadd.Stop();
zimutop.Stop();
// 判断是否游戏结束
if (MessageBox.Show("游戏结束,你的得分:" + score,"Game Over",
MessageBoxButtons.RetryCancel,MessageBoxIcon.Warning)
==DialogResult.Retry)
{
// 数据初始化
btnstart.Text = "开始游戏";
score = 0;
lab.Text = "得分:" + score + "分";
xt = 100;
HS.Value = xt;
// 调用清空屏幕方法
Qingping();
}
else
{
this.Close();
}
}
}
}
// 子弹上升
if (item.Tag.ToString()=="zidan")
{
item.Top -= 10;
if (item.Top<-item.Height)
{
item.Dispose();
}
foreach (Control it in Game.Controls)
{
if (it.Tag.ToString()== "biaoji")
{
if (item.Top<=it.Top+it.Height&&
item.Left+item.Width/2==it.Left+it.Width/2)
{
item.Dispose();
it.Dispose();
// 蓄力值进度条增加
xy += 10;
HSjindu.Width = xy;
if (xy == 100)
{
// 清空屏幕,蓄力值为0
xy = 0;
HSjindu.Width = xy;
Qingping();
}
score += 10;
lab.Text="得分:" + score + "分";
// 爆炸图片
PictureBox bomimgs = new PictureBox();
bomimgs.Size = new Size(30,30);
bomimgs.Tag = 0;
bomimgs.Image = imageList1.Images[0];
bomimgs.Left = it.Left+it.Width/2-bomimgs.Width/2;
bomimgs.Top = it.Top + it.Height / 2-bomimgs.Height/2;
Game.Controls.Add(bomimgs);
// 控制爆炸图片切换的计时器-timerbom
Timer timerbom = new Timer();
timerbom.Tag = bomimgs;
timerbom.Interval = 30;
timerbom.Start();
timerbom.Tick += Timerbom_Tick;
}
}
}
}
}
}
窗体的键盘事件
// 键盘事件
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
foreach (Control item in Game.Controls)
{
if (item.Text==e.KeyChar.ToString()&&item.Tag.ToString()=="zimu")
{
item.Tag = "biaoji";
plean.Left = item.Left+item.Width/2-plean.Width/2;
PictureBox fire = new PictureBox();
fire.Tag = "zidan";
fire.Size = new Size(10,30);
fire.Image = Image.FromFile(@"../../img/Ammo1.png");
fire.SizeMode = PictureBoxSizeMode.StretchImage;
fire.Left = plean.Left+plean.Width/2-fire.Width/2;
fire.Top = plean.Top-fire.Height;
Game.Controls.Add(fire);
return;
}
}
}
Timerbom计时器用来实现爆炸效果图的切换实现爆炸效果
// 爆炸图片切换计时器
private void Timerbom_Tick(object sender, EventArgs e)
{
Timer timbom = (Timer)sender;
PictureBox bomimgs = (PictureBox)timbom.Tag;
bomimgs.Tag=((int)bomimgs.Tag) + 1;
bomimgs.Image = imageList1.Images[(int)bomimgs.Tag];
if ((int)bomimgs.Tag>=31)
{
bomimgs.Dispose();
timbom.Dispose();
}
}
自定义清空屏幕方法
// 清空屏幕方法
private void Qingping()
{
// 遍历泛型集合zimulab
foreach (Label zimu in zimulab)
{
zimu.Dispose();
}
}