C#如何實(shí)現(xiàn)打字小游戲-創(chuàng)新互聯(lián)

這篇文章主要講解了C#如何實(shí)現(xiàn)打字小游戲,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的申扎網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

C#如何實(shí)現(xiàn)打字小游戲

using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace 打字游戲
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }
  //創(chuàng)建飛機(jī)
  PictureBox plan = new PictureBox();
  Button kaiguan = new Button();
  Timer flytime = new Timer();//動(dòng)畫(huà)事件
  //創(chuàng)建的得分的label
  Label df = new Label();
  int x = 0;
  //實(shí)例化血條
  Label xt = new Label();
  Label xt1 = new Label();
  int xuetiao = 150;
  int dl = 0;
 
  //產(chǎn)生字母
  private void Form1_Load(object sender, EventArgs e)
  {
   this.KeyPreview = true;
   //游戲區(qū)域
   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 = "開(kāi)始游戲";
   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=="開(kāi)始游戲")
   {
    flytime.Start();
    timer2.Start();
    timer3.Start();
    kaiguan.Text = "暫停游戲";
   }
   else if (kaiguan.Text=="暫停游戲")
   {
    flytime.Stop();
    timer2.Stop();
    timer3.Stop();
    kaiguan.Text = "開(kāi)始游戲";
   }
   //得分欄
   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();//顯示動(dòng)畫(huà)的容器;
  //動(dòng)畫(huà)事件
  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();
  //鳥(niǎo)的移動(dòng)
  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)
   {
    //每一個(gè)lebel是一個(gè)字母;
    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)
  {
   //遍歷循環(huán)語(yǔ)法
   foreach (Control item in panel1.Controls)
   {
    //每一個(gè)對(duì)象都有一個(gè)方法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)
  {
   //在按下任意按鍵的時(shí)候,判斷一下本次按鍵值是否有對(duì)應(yīng)的字母;
   //按鍵鍵值
   //被動(dòng)行為
   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;
      //創(chuàng)建子彈
 
      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;
     }
    }
   }
  }
 }

}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)頁(yè)名稱(chēng):C#如何實(shí)現(xiàn)打字小游戲-創(chuàng)新互聯(lián)
標(biāo)題URL:http://muchs.cn/article30/csgjpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、小程序開(kāi)發(fā)、網(wǎng)站改版、電子商務(wù)外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)