I Need Help To Make A pygame Code Work

I’m trying to make a red light green light game, but I forgot how random works in pygame, I have a square (the player) and the lights, red, yellow, and green, yellow always comes before red, but I just don’t know how to make it work with the randomly choosing green or yellow. Please help!

If you use random.choice it should give you a random selection from a list.

lights = [yellow_light, green_light]
next_light = random.choice(lights)

Should I write it here or in a different spot, I don’t know much about this, sorry.

That looks like it would work.

Also, please post the code as text in a codeblock - you do this by enclosing it in 3 backticks ``` like that.

```
code
```

Like that.

Some pointers to move forward

  • random.randint(0, 1) can be used like that.
  • Based on your current state you need to do the state transition
    • when “green or red”
      • select randomly the new target
      • change the states based on the selected
        • next_light == red and current_light == green => change state to yellow
        • add the other cases
    • when yellow
      • change state to red
  • having 60 FPS limit is good
    • when your state transitions works try moving to a pygame.time based version instead of the modulus 60 on the counter