用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;
- button1.Visible = false;
-
下面返回窗体,写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; }
背景
地鼠
打中地鼠
里面的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;
}
}
}
}
上一篇: 在家无聊,不如学习自己搭建一个个人博客?
下一篇: 练习一下,写个“排雷”小游戏
推荐阅读