C#飞机打字游戏的代码示例(winform版)
作者:Dust_SongYunfei 发布时间:2021-09-10 17:43:17
标签:C#,打字游戏
游戏界面
程序代码
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


猜你喜欢
- 字符串的编码方式UTF-8是Unicode的一种实现方式,也就是它的字节结构有特殊要求,所以我们说一个汉字的范围是0X4E00到0x9FA5
- 本文实例讲述了C#中lock的用法。分享给大家供大家参考。具体分析如下:lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断。这
- 目录一、首先模型本身需要特殊处理二、编写Shader三、讲解先上图看看效果:下面详细分享一下制作步骤吧:一、首先模型本身需要特殊处理二、编写
- Spring框架提供了事务管理的标准实现,且可以通过注解或者XML文件的方式声明和配置事务。通过异步事件的方式解耦服务调用,可以提高程序的响
- 在1.zip中增加一张新图片StorageFile jpg = await KnownFolders.PicturesLibrary.Get
- 本文实例讲述了Java 反射机制原理与用法。分享给大家供大家参考,具体如下:反射反射,程序员的快乐!1、什么是反射?Java反射就是在运行状
- 假如是在同一台机器上开发,前后端分离的工程中出现跨域问题的原因是,前端工程和后端工程运行在不同的端口上。只要协议、域名、端口有一个不同就会产
- 前言开发中很多需要javac 的程序依赖 JAVA_HOME环境变量.如果是手工下载源码安装的JDK,很容易知道JAVA_HOME的目录.
- 一、介绍以及编解码流程MediaCodec 类可用于访问低级媒体编解码器,即编码器/解码器组件。它是 Android 低级多媒体支持基础结构
- 本文实例讲述了C#判断字符串是否存在字母及字符串中字符的替换的方法。分享给大家供大家参考。具体实现方法如下:首先要添加对命名空间“using
- 一、C#正则表达式符号模式字符描述\转义字符,将一个具有特殊功能的字符转义为一个普通字符,或反过来^匹配输入字符串的开始位置$匹配输入字符串
- 前言水波纹特效,想必大家或多或少见过,在我的印象中,大致有如下几种: 支付宝 "咻
- AndroidStduio3.0使用gradle将module打包jar文件,首先需要安装gradle。打开控制台输入  
- springboot 异常与重定向在spring中,有两个重定向类型:301,永久性跳转302,暂时性跳转默认调用302。1.下面先通过一个
- SpringBoot @ComponentScan的使用SpringBoot的启动类中有一个@ComponentScan,之前项目由于这个注
- 本文实例讲述了JavaWeb 网上书店 注册和登陆功能。分享给大家供大家参考,具体如下:工具:Eclipse + Navicat源码地址:h
- java中对集合对象list的几种循环访问的总结如下 1 经典的for循环 public static voi
- 一、BufferedImage类介绍生成验证码图片主要用到了一个BufferedImage类,如下:创建一个DrawImage Servle
- 一、前言高效、合理的使用hibernate-validator校验框架可以提高程序的可读性,以及减少不必要的代码逻辑。接下来会介绍一下常用一
- 前言先放一个官网吧,其实本案例就是根据官网案例来的,只是进行了修改配置。Mybatis-plus官网一、搭建一个springboot项目&n