C#实现简单打字小游戏

 更新时间:2020年6月25日 10:34  点击:1888

本文实例为大家分享了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;
 
namespace 打字游戏
{
 public partial class Form1 : Form
 {
 public Form1()
 {
  InitializeComponent();
 }
 Random r = new Random();
 //游戏区
 Panel Gamearea = new Panel();
 //控制区
 Panel area = new Panel();
 //装鸟的盒子
 PictureBox Bird = new PictureBox();
 //字母出现定时器
 Timer zimu = new Timer();
 //飞鸟与字母下落定时器
 Timer fly = new Timer();
 //开始/暂停按钮
 Button button = new Button();
 //积分器
 Label scoring = new Label();
 //血条
 Label Bar = new Label();
 //尾翼
 PictureBox wei = new PictureBox();
 PictureBox weiyi = new PictureBox();
 //装字母的盒子
 List<Label> zmbox = new List<Label>();
 private void Form1_Load(object sender, EventArgs e)
 {
  //button设置
  this.KeyPreview = true;
  //最大化窗口
  this.WindowState = FormWindowState.Maximized;
  //背景图
  this.BackgroundImage = Image.FromFile("../../img/背景 (2).jpg");
  //拉伸
  this.BackgroundImageLayout = ImageLayout.Stretch;
  //游戏区设置
  Gamearea.Size = new Size(1000,750);
  //游戏区位置
  Gamearea.Location = new Point(30,30);
  this.Controls.Add(Gamearea);
  Gamearea.Tag = "game";
  //控制区设置
  area.Size = new Size(300,750);
  //控制区位置
  area.Location = new Point(Gamearea.Left+Gamearea.Width+20,30);
  area.BackColor = Color.Cyan;
  this.Controls.Add(area);
  //开始/暂停按钮
  //Button button = new Button();
  button.Text = "开始";
  this .KeyPreview = true;
  //字体大小
  button.Font = new Font("",20);
  //按钮大小
  button.Size = new Size(100,50);
  //按钮颜色
  button.BackColor = Color.Blue;
  //按钮位置
  button.Location = new Point(100,20);
  area.Controls.Add(button);
  //按钮点击事件
  button.Click += Button_Click;
  //积分器
  //Label scoring = new Label();
  scoring.Text = "积分:0";
  scoring.Font = new Font("",15);
  scoring.Location = new Point(100,100);
  area.Controls.Add(scoring);
  //装鸟的盒子设置
  Bird.Tag = "niao";
  Bird.Size = new Size(200,200);
  Bird.Location = new Point(0,0);
  //动画放入盒子
  Bird.Image = imageList1.Images[0];
  Gamearea.Controls.Add(Bird);
  //飞鸟与字母下落定时器
  //Timer fly = new Timer();
  fly.Interval = 80;
  fly.Tick += Fly_Tick;
  //fly.Start();
  //字母出现定时器
  //Timer zimu = new Timer();
  zimu.Interval = 1000;
  zimu.Tick += Zimu_Tick;
  //zimu.Start();
  //键盘
  this.KeyPress += Form1_KeyPress1;
  //飞机
  //PictureBox plane = new PictureBox();
  plane.Tag = "plane";
  //盒子大小
  plane.Size = new Size(100,100);
  //放进图片
  plane.Image = Image.FromFile("../../img/RP03.png");
  //图片自适应
  plane.SizeMode = PictureBoxSizeMode.StretchImage;
  //飞机位置
  plane.Location = new Point(Gamearea.Width/2-plane.Width/2,Gamearea.Height-plane.Height-60);
  Gamearea.Controls.Add(plane);
  //血条
  //Label Bar = new Label();
  Bar.Tag = "bar";
  Bar.Size = new Size(100, 10);
  Bar.BackColor = Color.Red;
  //位置
  Bar.Left = plane.Left + plane.Width - Bar.Width;
  Bar.Top = plane.Top - Bar.Height;
  Gamearea.Controls.Add(Bar);
  //尾翼  
  wei.Tag = "wei";  
  wei.Size = new Size(30, 40);
  //位置
  wei.Left = plane.Left + plane.Width / 2;  
  wei.Top = plane.Top + plane.Height; 
  //图片集放进盒子
  wei.Image = imageList3.Images[0];  
  Gamearea.Controls.Add(wei);  
  weiyi.Tag = "weiji";
  //图片集放进盒子
  weiyi.Image = imageList3.Images[0];  
  weiyi.Size = new Size(30, 40);
  //位置
  weiyi.Left = plane.Left + plane.Width /2-28;  
  weiyi.Top = plane.Top + plane.Height;
  Gamearea.Controls.Add(weiyi);
 }
 //按钮设置
 private void Button_Click(object sender, EventArgs e)
 {
  if (button.Text=="开始")
  {
  fly.Start();
  zimu.Start();
  button.Text = "暂停";
  }
  else if (button.Text=="暂停")
  {
  fly.Stop();
  zimu.Stop();
  button.Text = "开始";
 
  }
 }
 //飞机
 PictureBox plane = new PictureBox();
 //键盘事件
 private void Form1_KeyPress1(object sender, KeyPressEventArgs e)
 {
  //遍历游戏区内所有控件
  foreach (Control item in Gamearea.Controls)
  {
  //找到name为Label的控件
  if (item.GetType().Name == "Label")
  {
   //判断按键与字母是否相同
   if (item.Text == e.KeyChar.ToString()&& item.Tag.ToString() == "zm")
   {
   ////消除字母
   //item.Dispose();
   plane.Left = item.Left + item.Width / 2 - plane.Width / 2;
   //血条随飞机
   Bar.Left = plane.Left + plane.Width - Bar.Width;
   Bar.Top = plane.Top - Bar.Height;
   //尾翼随飞机
   wei.Left = plane.Left + plane.Width / 2;
   wei.Top = plane.Top + plane.Height;
   weiyi.Left = plane.Left + plane.Width / 2 - 28;
   weiyi.Top = plane.Top + plane.Height;
   item.Tag = "bj";
   //创造子弹
   PictureBox bullet = new PictureBox();
   bullet.Tag = "bullet";
   bullet.Size = new Size(10,30);
   //放进图片
   bullet.Image = Image.FromFile("../../img/Ammo1.png");
   //图片自适应
   bullet.SizeMode = PictureBoxSizeMode.StretchImage;
   //子弹位置
   bullet.Location = new Point(plane.Left+plane.Width/2-bullet.Width/2,plane.Top-bullet.Height);
   Gamearea.Controls.Add(bullet);
   return;
   }
  }
  }
 }
 
 //字母
 private void Zimu_Tick(object sender, EventArgs e)
 {
  //判断飞鸟出现屏幕字母开始下落
  if (Bird.Left >= 0 && Bird.Left <= Gamearea.Width - Bird.Width)
  {
  Label zm = new Label();
  zm.Tag = "zm";
  //随机字母
  zm.Text =((char)r.Next(97,123)).ToString();
  //随机大小
  zm.Font = new Font("",r.Next(10,45));
  //字体自适应
  zm.AutoSize = true;
  //随机颜色
  zm.ForeColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
  //随着鸟的位置下落
  zm.Top = Bird.Height;
  zm.Left = Bird.Left + Bird.Width / 2 - zm.Width / 2;
  //添加进游戏区
  Gamearea.Controls.Add(zm);
  zmbox.Add(zm);
  }
 }
 //播放飞鸟动画
 int bo = 0;
 //积分
 int fen = 0;
 int t = 0, y = 0;
 private void Fly_Tick(object sender, EventArgs e)
 {
  //飞鸟动画播放
  Bird.Image = imageList1.Images[bo];
  bo++;
  //图片播放完重零开始重新播放
  if (bo >= imageList1.Images.Count)
  {
  bo = 0;
  }
  //遍历控件
  foreach (Control item in Gamearea.Controls)
  {
  //鸟飞
  if (item.Tag.ToString()=="niao")
  {
   item.Left += 10;
   //超出边界重新开始飞
   if (item.Left>=Gamearea.Width)
   {
   item.Left = -item.Width;
   }
  }
  //字母下落
  if (item.Tag.ToString() == "zm"||item.Tag.ToString()=="bj")
  {
   item.Top += 10;
   //超出下边界清除
   if (item.Top+item.Height>=Gamearea.Height)
   {
   
   item.Dispose();
   Bar.Width -= 10;
   //判断血条为零时
   if (Bar.Width==0)
   {
    fly.Stop();
    zimu.Stop();
    //弹框
    if ( MessageBox.Show("游戏结束,积分为:"+fen,"Game Over", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Retry)
    {
    fly.Stop();
    zimu.Stop();
    //满血条
    Bar.Width = 100;
    //按钮变开始
    button.Text = "开始";
    //积分清零
    scoring.Text = "积分:0";
    //鸟回归原位
    Bird.Location = new Point(0, 0);
    //清屏
    qingping();
    }
    else
    {
    this.Close();
    }
   }
   }
  }
  //子弹上升
  if (item.Tag.ToString() == "bullet")
  {
   item.Top -= 10;
   //超出上边界清除
   if (item.Top==-item.Top)
   {
   item.Dispose();
   }
   //重新遍历
   foreach (Control letter in Gamearea.Controls)
   {
   //找到字母
   if (letter.Tag.ToString() == "bj")
   {
    //判断字母与子弹相遇
    if (item.Top <= letter.Top + letter.Height && item.Left + item.Width / 2 == letter.Left + letter.Width / 2)
    {
    item.Dispose();
    letter.Dispose();
    //积分自加
    fen++;
    //写入控制区
    scoring.Text = "积分:" + fen;
    //创造爆炸
    PictureBox detonate = new PictureBox();
    //记录播放图片从零开始
    detonate.Tag = 0;
    //盒子大小
    detonate.Size = new Size(50,50);
    //将图片放进盒子
    detonate.Image = imageList2.Images[0];
    //图片自适应
    detonate.SizeMode = PictureBoxSizeMode.StretchImage;
    //爆炸位置
    detonate.Location = new Point(letter.Left + letter.Width / 2 - detonate.Width / 2, letter.Top + letter.Height / 2 - detonate.Height / 2);
    Gamearea.Controls.Add(detonate);
    //爆炸timer
    Timer zha = new Timer();
    //爆炸图片放进timer
    zha.Tag = detonate;
    zha.Interval = 100;
    zha.Tick += Zha_Tick;
    zha.Start();
    }
   }
   }
  }
  //尾翼动画播放
  wei.Image = imageList3.Images[t];
  t++; 
  if (t >= imageList3.Images.Count) 
  {
   t = 0; 
  }
  weiyi.Image = imageList3.Images[y]; 
  y++; 
  if (y >= imageList3.Images.Count) 
  {
   y = 0; 
  }
  }
 }
 
 private void Zha_Tick(object sender, EventArgs e)
 {
  //获取timer
  Timer zha = (Timer)sender;
  //从盒子获取图片集
  PictureBox picture = (PictureBox)zha.Tag;
  //获取imageList2中图片
  picture.Image = imageList2.Images[(int)picture.Tag];
  //图片播放
  picture.Tag = (int)picture.Tag + 1;
  //一组爆炸图播完清除  
  if ((int)picture.Tag == 32)
  {
  zha.Dispose();
  picture.Dispose();
  
  }
 }
 //清屏字母
 private void qingping()
 {
  foreach (Label lazm in zmbox)
  {
  lazm.Dispose();
  }
 }
 }
}

更多有趣的经典小游戏实现专题,也分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • 经典实例讲解C#递归算法

    这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#中list用法实例

    这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25