当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现打字游戏

C#实现打字游戏

2020年05月13日  | 移动技术网IT编程  | 我要评论

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

思路:

1、有一个游戏界面,我用panel作为游戏界面
2、开始生成字母
打字游戏的字母是不断生成的,所以用计时器timer来生成字母
所有生成的字母设置tag方便寻找
3、字母下落
字母下落是一个持续的动作,所以也在计时器里做
在计时器里通过foreach遍历panel中的所有控件,同时通过tag找到字母,让字母下降
4、生成子弹
通过获取键盘事件生成子弹
5、子弹与字母相碰

代码:

private void form1_load(object sender, eventargs e)
    {
      this.panel1.backcolor = color.white;
      timer1.start();
      timer2.start();
      timer1.interval = 1000;
      timer2.interval = 100;

      
      fj.tag = "feiji";
      fj.size = new size(30, 40);
      fj.backcolor = color.black;
      fj.text = "飞机";
      fj.textalign = contentalignment.middlecenter;
      fj.forecolor = color.white;
      fj.location = new point(panel1.width / 2 - fj.width / 2, panel1.height - fj.height);
      panel1.controls.add(fj);

      
    }
    label fj = new label();
    random r = new random();

    private void timer1_tick(object sender, eventargs e)
    {
      label zm = new label();
      zm.tag = "zimu";
      zm.text = ((char)r.next(97, 123)).tostring();
      zm.font = new font("", r.next(20, 30));
      zm.autosize = true;
      zm.location = new point(r.next(0, panel1.width - zm.width), 0);
      zm.forecolor = color.fromargb(r.next(255), r.next(255), r.next(255));
      panel1.controls.add(zm);

      
    }

    private void timer2_tick(object sender, eventargs e)
    {
      foreach (control item in panel1.controls)
      {
        if (item.tag.tostring() == "zimu"||item.tag.tostring()=="zzm")
        {
          item.top += 5;
          if (item.top >= panel1.height)
          {
            item.dispose();
          }
        }else if (item.tag.tostring() == "zidan")
        {
          item.top -= 9;
          foreach (control con in panel1.controls)
          {
            if (con.tag.tostring() == "zzm")
            {
              if (con.top + con.height >= item.top)
              {
                con.dispose();
                item.dispose();

                soundplayer ply = new soundplayer();
                ply.soundlocation = ".../.../sound/mybomb.wav";
                ply.play();
              }
            }
          }
        }
      }
    }
    

    private void form1_keypress(object sender, keypresseventargs e)
    {
      foreach (control item in panel1.controls)
      {
        if (item.tag.tostring() == "zimu")
        {
          if (item.text == e.keychar.tostring())
          {
            label zd = new label();
            zd.tag = "zidan";
            zd.size = new size(20, 20);
            item.tag = "zzm";
            zd.backcolor = color.red;
            zd.location = new point(item.left + item.width / 2 - zd.width / 2, fj.top - fj.height);
            fj.left = item.left + item.width / 2 - fj.width / 2;
            panel1.controls.add(zd);
            return;
          }
        }
      }
    }

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网