Search code examples
c#winforms2d-games

Windows Forms and c# randomizing


So I'm making a game in windows forms. I have run into a slight hitch though. When the player is touching the basketball I want the basketball to change to a different lane. So in order to do this, I used the random and .next(1, 4) methods.

For some reason sometimes the basketball will appear on the same road as the player and I have tried everything to make it check for this but nothing works.

Here is the code:

private void Hlocal(object sender, EventArgs e)
        {
            // If the player is touching the basketball
            if (Halsey.Location.X == Basketball.Location.X)
            {
                scorenum += 1; // Score
                Score.Text = $"Score: {scorenum}"; // Scoreboard
                Broad = rnd.Next(1, 5); // Picks a random road
                label1.Text = $"{Broad}"; // for debugging
                // After it chooses a road I place it on the road
                switch (Broad)
                {
                    case 1:
                        Basketball.Location = new Point(57, Basketball.Location.Y);
                        break;

                    case 2:
                        Basketball.Location = new Point(263, Basketball.Location.Y);
                        break;

                    case 3:
                        Basketball.Location = new Point(469, Basketball.Location.Y);
                        break;

                    case 4:
                        Basketball.Location = new Point(675, Basketball.Location.Y);
                        break;
                }
            }
        }

Here is where it moves the character and changes Hroad var:

private void KeyHandler(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Right:
                    if (Halsey.Location.X == 675)
                    {
                        break;
                    }
                    Halsey.Location = new Point(Halsey.Location.X + 206, Halsey.Location.Y);
                    Hroad += 1;
                    label2.Text = $"{Hroad}";
                    break;
                case Keys.Left:
                    if (Halsey.Location.X == 57)
                    {
                        break;
                    }
                    Halsey.Location = new Point(Halsey.Location.X - 206, Halsey.Location.Y);
                    Hroad -= 1;
                    label2.Text = $"{Hroad}";
                    break;
            }
        }

Some clarifications:

Hlocal is an event called everythime the player moves.

Halsey is a picturebox which acts as the character sprite.

Basketball is a sprite which acts like a coin that adds points.

scorenum is the score

label1 is just for debugging

Broad is the road the basketball is on

Hroad is the road the player is on

and the basketball locations are the different roads

Currently, the x points to place sprites on the roads are: road1 - 57

road2 - 263

road3 - 469

road4 - 675


Solution

  • You can create a List<int> with the roads in it. Then remove the Hroad value which leaves a list of available roads. Then select a random one of those:

    List<int> roads = new List<int>{1,2,3,4};
    roads.Remove(Hroad);
    int Broad = roads[rand.Next(0, roads.Count)];