extremely simply WhakeTheHole

lynxAi

Member
Joined
Dec 7, 2019
Messages
15
Location
SiChuan China
Programming Experience
Beginner
C#:
public partial class Form1 : Form
    {
        int score = 0;
        int miss = 0;
        Random rabg = new Random();
        int location = 0;
        bool HitClick = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void Start_Click(object sender, EventArgs e)
        {
            pictureBox1.Enabled = true;
            timer1.Enabled = true;
            Start.Visible = false;
        }
        private void MoveMole(object sender, EventArgs e)
        {
            MoveMole();//应用自定义函数。
            lblhit.Text = "打中"(got hit) + score;
            lblmiss.Text = "错失"(miss) + miss;
            if (HitClick == true)
            { score++; }
            else
            { miss++; }

            if (score > 15)
            {
                timer1.Stop();
                score = 0;
                miss = 0;
                MessageBox.Show("你赢了!(you win)", "提示", MessageBoxButtons.OK);
                Start.Text = "开始(start)";
                Start.Visible = true;
            }

            if (miss > 10)
            {
                timer1.Stop();
                score = 0;
                miss = 0;
                MessageBox.Show("你输了(you lost)", "提示", MessageBoxButtons.OK);
                Start.Text = "开始";
                Start.Visible = true;
            }
        }

        private void HitMole_Click(object sender, EventArgs e)
        {
            score++;
            pictureBox1.Image = Properties.Resources.molehit;
            HitClick = false;
            pictureBox1.Enabled = false;

        }

        private void MoveMole()//自定义函数“移动鼹鼠”
        {
            pictureBox1.Image = Properties.Resources.molelive;
            HitClick = false;
            pictureBox1.Enabled = Enabled;
            location = rabg.Next(1, 7);

            switch (location)
            {
                case 1:
                    pictureBox1.Location = new Point(111, 60);
                    break;
                case 2:
                    pictureBox1.Location = new Point(343, 61);
                    break;
                case 3:
                    pictureBox1.Location = new Point(583, 59);
                    break;
                case 4:
                    pictureBox1.Location = new Point(97, 297);
                    break;
                case 5:
                    pictureBox1.Location = new Point(343, 298);
                    break;
                case 6:
                    pictureBox1.Location = new Point(583, 299);
                    break;
                default:
                    break;

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Enabled = false;
            this.Cursor = new Cursor("hammer.cur");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled == true)
            {
                timer1.Enabled = false;
                pictureBox1.Enabled = false;
                button2.Text = "继续";
            }
            else
            {
                timer1.Enabled = true;
                pictureBox1.Enabled = true;
                button2.Text = "暂停";
            }
        }
    }
}
批注 2019-12-15 213205.jpg



thought hit every moles but still have the number of miss, and the miss number grow up with the mole click,

Maybe there error:

if (HitClick == true)
{ score++; }
else
{ miss++; }
and some place.
 
I haven't cracked open the attachment above, but just guessing, I would guess that your MoveMole() event handler is attached to a timer and each time the timer ticks and you don't have HitClick set to true, you increase the miss count. So it depends on how you define a miss. Is a miss when the user clicks the mouse button and they don't hit a mole? Or is a miss each time a mole appears and moves without getting clicked on?
 
Yup, you've attached [il]MoveMole()[/il] as the event handler for the timer tick:
in Form1.Designer.cs:
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.MoveMole);

So it is as I said, you need to figure out how you define a miss:
A miss is when the user clicks at a location and there is no mole there; OR
A miss is when the mole is not clicked on before it moves to another location.

Right now, your implementation is using the second definition.
 
Back
Top Bottom