C#飞机打字游戏的代码示例(winform版)
作者:Dust_SongYunfei 时间:2021-09-10 17:43:17
游戏界面
程序代码
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 容器 游戏区对象
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>(); // 实例化泛型和对象labels用来存储字母
List<PictureBox> picts = new List<PictureBox>(); // 实例化泛型对象picts用来存储 *
private void Form1_Load(object sender, EventArgs e)
{
this.Size = new Size(1000, 700);
//this.FormBorderStyle= FormBorderStyle.FixedToolWindow; // 去掉窗体图标
this.Text = "字母大战";
this.BackColor = Color.FromArgb(80, 00, 80);
// this.BackgroundImage = Image.FromStream(@"");
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
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);
// 尾气1定时器
Timer weiqitimer1 = new Timer();
weiqitimer1.Tag = feijwei1;
weiqitimer1.Tick += Weiqitimer1_Tick;
weiqitimer1.Start();
//尾气2定时器
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);
}
//飞机尾气定时器1
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;
}
}
//飞机尾气定时器2
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)
{
//Label btn = (Label)sender; // 获取始发者
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中
Label lb = new Label();
lb.Text = ((char)rand.Next(97, 123)).ToString(); // 97-123 随机ASCLL字母
lb.Font = new Font("", rand.Next(20, 30)); // 字体随机15-20
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); // 字母添加到labels中
}
// 控件字母下落, * 上升
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) // 字母超过bg高度 字母删除
{
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弹框第一个参数为弹框内容,第二参数为标题,第三个参数为按钮,第四个参数为图标
MessageBox.Show("游戏结束"+"得分为:"+count+"分","游戏结束",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
count = 0;
defeng.Text = "得分: " + count + "分";
}
}
}
//判断 *
if (item.Tag.ToString()=="zidan")
{
item.Top -= 5;
if (item.Top<0) // 当 * 超出bg高度 * 删除
{
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的属性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; // 将bombBox存储到bombTimer的Tag属性中
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; // BombTimer的发起者 即bombTimer,重新bombtimer名
PictureBox picture = (PictureBox)bombtimer.Tag;// 获取bombTimer的Tag属性,即bombBox
picture.Image = imageList1.Images[(int)picture.Tag];// 添加图片
picture.Tag = (int)picture.Tag + 1; // 索引加1
if ((int)picture.Tag>31) // 超过索引为31时,定时器清除,图片清除
{
bombtimer.Dispose();
picture.Dispose();
}
}
// 键盘事件
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
// 事件 e参数
foreach (Control item in BG.Controls)
{
// 判断按键值和字母对应
if (item.Text==e.KeyChar.ToString()&& item.Tag.ToString()=="zimu")
{
item.Tag = "biaoji";
//飞机的移动 飞机的left=字母的left+字母的width-飞机的width/2;
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); // * 添加到picts中
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();// 关闭窗体
}
}
}
来源:https://blog.csdn.net/dust__/article/details/103117508
标签:C#,打字游戏
0
投稿
猜你喜欢
使用Java将一个List运用递归转成树形结构案例
2021-10-14 15:18:30
C#策略模式(Strategy Pattern)实例教程
2022-11-29 07:35:07
Java基于JDK 1.8的LinkedList源码详析
2021-07-15 03:29:26
配合Swagger使用绝佳的两款直观易用JSON可视化工具
2021-10-14 23:50:23
SpringCloud断路器Hystrix原理及用法解析
2022-03-20 16:47:41
解析C#多线程编程中异步多线程的实现及线程池的使用
2021-10-23 02:36:23
Android音视频开发之MediaExtactor使用教程
2023-03-20 19:25:11
java异常与错误处理基本知识
2023-11-25 10:44:59
C#中英文混合字符串截取函数
2023-01-19 06:02:55
C# GDI+实现时钟表盘
2023-06-20 07:11:32
C#用记事本编写简单WinForm窗体程序
2021-12-19 13:02:31
JAVA设计模式之备忘录模式原理与用法详解
2023-08-24 13:23:37
java中TESTful架构原理分析
2022-03-02 21:12:10
Java拦截器Interceptor和过滤器Filte的执行顺序和区别
2022-06-01 20:37:11
Android DragVideo实现播放视频时任意拖拽的方法
2022-05-24 03:45:48
Volatile关键字的使用案例
2022-01-27 16:37:05
Java剑指offer之删除链表的节点
2023-05-19 15:30:27
浅析Java中JSONObject和JSONArray使用
2022-06-05 14:58:30
Java中四种访问权限资料整理
2021-12-04 13:23:58
C# WinForm-Timer控件的使用
2023-11-20 06:54:00