欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现打字小游戏

程序员文章站 2022-05-14 22:13:47
本文实例为大家分享了c#实现打字小游戏的具体代码,供大家参考,具体内容如下using system;using system.drawing;using system.windows.forms; n...

本文实例为大家分享了c#实现打字小游戏的具体代码,供大家参考,具体内容如下

C#实现打字小游戏

using system;
using system.drawing;
using system.windows.forms;
 
namespace 打字游戏
{
 public partial class form1 : form
 {
  public form1()
  {
   initializecomponent();
  }
  //创建飞机
  picturebox plan = new picturebox();
  button kaiguan = new button();
  timer flytime = new timer();//动画事件
  //创建的得分的label
  label df = new label();
  int x = 0;
  //实例化血条
  label xt = new label();
  label xt1 = new label();
  int xuetiao = 150;
  int dl = 0;
 
  //产生字母
  private void form1_load(object sender, eventargs e)
  {
   this.keypreview = true;
   //游戏区域
   this.windowstate = formwindowstate.maximized;
   this.text = "打字游戏";
   panel1.size = new size(1000, 760);
   panel1.backcolor = color.white;
 
   timer1.start();
   bird.tag = "b";
   bird.width = 229;
 
 
   bird.height = 111;
   bird.sizemode = pictureboxsizemode.stretchimage;//
   bird.location = new point(0, 0);
   panel1.controls.add(bird);
   bird.image = imagelist1.images[0];
 
   
   flytime.interval = 80;
   //事件的添加:+、=、tab、tab;
   flytime.tick += flytime_tick;
 
   plan.tag = "plan";
   plan.size = new size(100, 100);
   //
   //plan.backcolor = color.red;
   plan.location = new point(panel1.width / 2 - plan.width / 2, panel1.height - plan.height);
   plan.image = image.fromfile(@"../../img/bp03.png");
   panel1.controls.add(plan);
 
   
   kaiguan.text = "开始游戏";
   kaiguan.location = new point(1200,300);
   this.controls.add(kaiguan);
   kaiguan.click += kaiguan_click;
 
   
  }
 
  private void kaiguan_click(object sender, eventargs e)
  {
   if (kaiguan.text=="开始游戏")
   {
    flytime.start();
    timer2.start();
    timer3.start();
    kaiguan.text = "暂停游戏";
   }
   else if (kaiguan.text=="暂停游戏")
   {
    flytime.stop();
    timer2.stop();
    timer3.stop();
    kaiguan.text = "开始游戏";
   }
   //得分栏
   df.size = new size(130, 20);
   df.location = new point(1138, 210);
   df.tag = "df";
   df.text = "得分:0分";
   df.autosize = true;
   this.controls.add(df);
   //血条栏
   xt.size = new size(130,20);
   xt.location = new point(1200,500);
   xt.backcolor = color.white;
   xt1.size = new size(130,20);
   xt1.location=new point(1200,500);
   xt1.backcolor = color.red;
   xt.tag = "xt";
   xt1.tag = "xt1";
   this.controls.add(xt);
   this.controls.add(xt1);
 
 
  }
 
  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 = -1;
   }
 
  }
 
  random r = new random();
  //鸟的移动
  private void timer2_tick(object sender, eventargs e)
  {
   bird.left += 2;
   if (bird.left >= panel1.width)
   {
    bird.left = -bird.width;
   }
  }
  //字母生成
  private void timer1_tick_1(object sender, eventargs e)
  {
   if (bird.left >= 0 && bird.left <= panel1.width - bird.width)
   {
    //每一个lebel是一个字母;
    label lb = new label();
    lb.text = ((char)r.next(97, 123)).tostring();
    lb.font = new font("", r.next(20, 40));
    lb.tag = "lb";
    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 timer3_tick(object sender, eventargs e)
  {
   //遍历循环语法
   foreach (control item in panel1.controls)
   {
    //每一个对象都有一个方法gettype
    if (item.gettype().name == "label")
    {
     item.top += 3;
     if (item.top >= panel1.height)
     {
      item.dispose();
      xuetiao -= 10;
      xt.width = xuetiao;
      dl++;
      if (xt.width==0)
      {
       flytime.stop();
       timer2.stop();
       timer3.stop();
       messagebox.show("game over");
      }
      
     }
 
    }
    if(item.gettype().name== "picturebox")
    {
     if (item.tag.tostring()=="zd")
     {
      item.top -= 7;
      if (item.top+item.height<=-item.height)
      {
       item.dispose();
      }
      foreach (control ad in panel1.controls)
      {
       if (ad.tag.tostring()=="lba")
       {
        if (ad.top+ad.height>=item.top&&item.left==ad.left+ad.width/2-item.width/2)
        {
         item.dispose();
         ad.dispose();
         x += 10;
         df.text = x.tostring()+"分";
 
         picturebox baz = new picturebox();//装播放爆炸图片的盒子;
         baz.tag = 0;
         baz.image = imagelist2.images[0];
         baz.size = new size(90,90);
         baz.sizemode = pictureboxsizemode.stretchimage;
         baz.location = new point(ad.left + ad.width / 2 - baz.width/2,ad.top+ad.height/2-baz.height/2) ;
         panel1.controls.add(baz);
 
         timer bofang = new timer();
         bofang.start();
         bofang.interval = 60;
         bofang.tag = baz;
         bofang.tick += bofang_tick;
;         
        }
       }
      }
      
     }
    }
 
   }
 
  }
 
 
  private void bofang_tick(object sender, eventargs e)
  {
   timer mnb = (timer)sender;
   picturebox picture = (picturebox)mnb.tag;
   picture.image = imagelist2.images[(int)picture.tag];
   picture.tag = (int)picture.tag + 1;
   if ((int)picture.tag>=31)
   {
    picture.dispose();
 
    mnb.dispose();
   }
 
  }
 
  //按键消除
  private void form1_keypress(object sender, keypresseventargs e)
  {
   //在按下任意按键的时候,判断一下本次按键值是否有对应的字母;
   //按键键值
   //被动行为
   foreach (control item in panel1.controls)
   {
    if (item.gettype().name == "label")
    {
     if (item.text == e.keychar.tostring()&&item.tag.tostring()=="lb")
     {
      item.tag = "lba";
      plan.left = item.left + item.width / 2 - plan.width / 2;
      //创建子弹
 
      picturebox bullet = new picturebox();
      bullet.tag = "zd";
      bullet.size = new size(6,20);
      bullet.image = image.fromfile(@"../../img/ammo4.png");
      bullet.location = new point(plan.left + plan.width / 2 - bullet.width / 2, plan.top - bullet.height);
      bullet.sizemode = pictureboxsizemode.stretchimage;
      panel1.controls.add(bullet);
      return;
     }
    }
   }
  }
 }

}

C#实现打字小游戏

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

相关标签: C# 打字