Question C#game. whack a mole

lynxAi

Member
Joined
Dec 7, 2019
Messages
15
Location
SiChuan China
Programming Experience
Beginner
批注 2019-12-08 105728.jpg

the simply whake the mole


private void moveMole()
{
isHit = false;
Mole.Enabled = true;
Mole.Image = Properties.Resources.Mole;
Mole.BackColor = System.Drawing.Color.Transparent;
locationNum = rabg.Next(1,7);

switch (locationNum)
{
case 1:
Mole.Location = new Point(79, 75);
break;
case 2:
Mole.Location = new Point(258,76);
break;
case 3:
Mole.Location = new Point(436,76);
break;
case 4:
Mole.Location = new Point(84, 342);
break;
case 5:
Mole.Location = new Point(257,344);
break;
case 6:
Mole.Location = new Point(434,345);
break;
default:
break;
}


each hole are link a new point, But RUN this game:
批注 2019-12-08 105807.jpg

the mole can't appear on the hole.

HELP ME!!
 

Attachments

  • 批注 2019-12-08 105701.jpg
    批注 2019-12-08 105701.jpg
    56.5 KB · Views: 43
Last edited:
First things first, don't post pictures of code. Code is text so post it as text, inside code formatting tags. That way, it is easy to read and also easy to copy if we want to run it ourselves.
 
Last edited:
How do we know those positions you have hard coded are correct and match up correctly with the holes that are rendered on screen if you don't show us the code for rendering the screen?

Are you sure you've not setup some kind of transform on the GDI context that does the rendering?
 
As for the issue, it's hard for us to say because we just see some code and a result and there's no particular reason that we should think that the result isn't what you should expect from that code. If it were me, I'd probably do it rather differently. I'd have six PictureBox controls and just move the Image about, e.g.
C#:
private Random rng = new Random();
private PictureBox[] molePictureBoxes;
private PictureBox molePictureBox;
private Image moleImage = Properties.Resources.Mole;

private void Form1_Load(object sender, EventArgs e)
{
    // Create an array of all the PictureBoxes that could display the mole.
    molePictureBoxes = new[]
                       {
                           molePictureBox1,
                           molePictureBox2,
                           molePictureBox3,
                           molePictureBox4,
                           molePictureBox4,
                           molePictureBox6
                       };
}

// This method handles the Click event for all six PictureBoxes.
private void molePictureBoxes_Click(object sender, EventArgs e)
{
    // Check whether the selected PictureBox was clicked.
    if (sender == molePictureBox)
    {
        // The user whacked the mole.
    }
}

private void MoveMole()
{
    // Generate a random index into the PictureBox array.
    var index = rng.Next(molePictureBoxes.Length);

    // Select the PictureBox at that index.
    molePictureBox = molePictureBoxes[index];

    foreach (var pictureBox in molePictureBoxes)
    {
        // Display the mole in the current PictureBox if and only if it is the selected PictureBox.
        pictureBox.Image = pictureBox == molePictureBox
                               ? moleImage
                               : null;
    }
}
 
Last edited:
What are the locations of the holes? And how are the holes locations generated? What are you using the rabg.Next(1,7); for?
 
Here's a variation if you want the mole to never appear in the same place twice in a row:
C#:
private void MoveMole()
{
    // Choose from all PictureBoxes the first time and non-selected PictureBoxes other times.
    var index = rng.Next(molePictureBox == null
                             ? molePictureBoxes.Length
                             : molePictureBoxes.Length - 1);

    molePictureBox = molePictureBoxes.Except(new[] {molePictureBox}).ElementAt(index);

    foreach (var pictureBox in molePictureBoxes)
    {
        pictureBox.Image = pictureBox == molePictureBox
                               ? moleImage
                               : null;
    }
}
 
Back
Top Bottom