天天看點

用c#做的打地鼠小遊戲,整理一下上課學的

小遊戲

用c#做的打地鼠小遊戲,整理一下上課學的
  • 首先,我們要建立一個窗體應用程式,搭建好窗體的架構,有三個lable,分别是打地鼠,遊戲時間和遊戲得分,還有一個button按鈕,text屬性改為開始遊戲.
  • 在每個土堆上拖入一個picturebox用于随機顯示地鼠,再添加兩個計時器timer,一個imagelist存儲照片,一共兩張照片一張沒有被打中的,一張打中地鼠時要顯示的圖檔,為了友善我把圖檔放最後了
  • 架構搭好了,輕按兩下button按鈕開始遊戲,編輯事件

    先在button事件外面定義兩個變量

    int count=0;//表示得分

    int time = 0;//時間

    下面是button事件裡面的代碼

    • button1.Visible = false;

      timer1.Enabled = true;

      timer2.Enabled = true;

      label3.Text = “遊戲時間:20”;

      label4.Text = “遊戲得分:0”;

      label1.Text = “打地鼠”;

      count = 0;

      time = 20;

  • 下面傳回窗體,寫timer1的事件,點選timer1,選擇事件,輕按兩下裡面的tick事件,進入代碼視窗
  • 同樣,在外面定義一個全局變量 int num=0; 下面是timer1裡的代碼
    Random rd = new Random();//Random.Next(Int) 傳回一個小于所指定最大值的非負随機數
                    int num= rd.Next(6);
                     switch(num)
                     {
                         case 0:invisiable();pictureBox1.Visible=true; break;
                         case 1: invisiable(); pictureBox2.Visible = true; break;
                         case 2: invisiable(); pictureBox3.Visible = true; break;
                         case 3: invisiable(); pictureBox4.Visible = true; break;
                         case 4: invisiable(); pictureBox5.Visible = true; break;
                         case 5: invisiable(); pictureBox6.Visible = true; break;
                     }
               
  • 為了減少代碼,單獨給invisible寫了一個函數,接下來直接寫一個invisible函數用于隐藏地鼠,别忘了在外面定義一個變量

    int flag = 0;

    private void invisiable()

    {

    pictureBox1.Visible = false;

    pictureBox2.Visible = false;

    pictureBox3.Visible = false;

    pictureBox4.Visible = false;

    pictureBox5.Visible = false;

    pictureBox6.Visible = false;

    pictureBox1.Image = imageList1.Images[0];

    pictureBox2.Image = imageList1.Images[0];

    pictureBox3.Image = imageList1.Images[0];

    pictureBox4.Image = imageList1.Images[0];

    pictureBox5.Image = imageList1.Images[0];

    pictureBox6.Image = imageList1.Images[0];

    flag = 0;

    }

  • 然後給pictruebox1添加事件,輕按兩下pictruebox1,代碼如下
PictureBox pb = (PictureBox)sender;
            pb.Image = imageList1.Images[1];
                if (flag == 0) 
            {
                flag = 1;
                count++;
                label4.Text = "遊戲得分: "+count;
                }
           
  • 傳回窗體界面,給pictruebox1到pictruebox6都添加這個事件(單擊每個圖檔框,在事件裡click下拉菜單中選擇pictruebox1)
  • 最後給timer2添加tick事件了
  • if(time==0)
         {
             timer1.Enabled = false;
             invisiable();
             label1.Text = "遊戲結束";
             button1.Visible = true;
             button1.Text = "再來一次";
    
         }
         else
         {
             time--;
             label3.Text = "遊戲時間: " + time;
         }
               

背景

用c#做的打地鼠小遊戲,整理一下上課學的

地鼠

用c#做的打地鼠小遊戲,整理一下上課學的

打中地鼠

用c#做的打地鼠小遊戲,整理一下上課學的

裡面的lable和batton仔細看一下,一定要改成你自己的,畢竟每個人建的都不一樣,前段時間受傷了,剛才看到評論裡留言要源碼的就直接補充到這裡了,順便說一聲不好意思,我保證老師看見不會打死我。

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 Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }
        int count=0;
        int time = 0;
        private void button1_Click(object sender, EventArgs e)
        {
        button1.Visible = false;
        timer1.Enabled = true;
        timer2.Enabled = true;
        label3.Text = "遊戲時間:20";
        label4.Text = "遊戲得分:0";
        label1.Text = "打地鼠";
        count = 0;
        time = 20;
            
    }
    int num=0;
    private void timer1_Tick(object sender, EventArgs e)
    {
        Random rd = new Random();
       int num= rd.Next(6);
        switch(num)
        {
            case 0:invisiable();pictureBox1.Visible=true; break;
            case 1: invisiable(); pictureBox2.Visible = true; break;
            case 2: invisiable(); pictureBox3.Visible = true; break;
            case 3: invisiable(); pictureBox4.Visible = true; break;
            case 4: invisiable(); pictureBox5.Visible = true; break;
            case 5: invisiable(); pictureBox6.Visible = true; break;
        }
    }
    int  flag = 0;
    private void invisiable()
    {
        pictureBox1.Visible = false;
        pictureBox2.Visible = false;
        pictureBox3.Visible = false;
        pictureBox4.Visible = false;
        pictureBox5.Visible = false;
        pictureBox6.Visible = false;
        pictureBox1.Image = imageList1.Images[0];
        pictureBox2.Image = imageList1.Images[0];
        pictureBox3.Image = imageList1.Images[0];
        pictureBox4.Image = imageList1.Images[0];
        pictureBox5.Image = imageList1.Images[0];
        pictureBox6.Image = imageList1.Images[0];
        flag = 0;

    }
   

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        PictureBox pb = (PictureBox)sender;
        pb.Image = imageList1.Images[1];
            if (flag == 0) 
        {
            flag = 1;
            count++;
            label4.Text = "遊戲得分: "+count;


        }
    }

    private void timer2_Tick(object sender, EventArgs e)
    {
        if(time==0)
        {
            timer1.Enabled = false;
            invisiable();
            label1.Text = "遊戲結束";
            button1.Visible = true;
            button1.Text = "再來一次";

        }
        else
        {
            time--;
            label3.Text = "遊戲時間: " + time;
        }
    }
}
}