pythonpygameuser-interaction

Randomizing variable only after user interaction


sorry for the format of this question, my code, if its been asked before, etc. i'm a little new to coding with python, especially pygame, so i don't know exactly what i'm doing

i'm trying to make a game which randomizes a shape from a list and the user is expected to hover over only that shape, if they hover over another it'll be wrong i have figured out that part of the code and would like to consider my code near done, except when it randomizes the shape from a list it continues to randomize rather than waiting for a user interaction before randomizing the shape again, and it'll do thousands of random calls within seconds. is there a better way to randomize the shape?

currently, i have it like this:

shapes = [ "square", "circle", "rectangle" ]
shape = random.choice(shapes)

i also tried something like

import pygame
pygame.init()
import random
screen = pygame.display.set_mode((500, 500))
shapes = ["square", "circle", "rectangle"]
square = pygame.Rect(100,100,75,75)
circ = pygame.Rect(20,240, 120,120)
rect = pygame.Rect(250, 100, 100, 150)
shape = random.choice(shapes)
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    screen.fill(0)
    font = pygame.font.Font('freesansbold.ttf', 12)
    shapetext = font.render('Designated shape: ' + shape, True, (255,255,255), (0,0,0))
    shapetextRect = shapetext.get_rect()
    shapetextRect.center = (100,20)
    screen.blit(shapetext,shapetextRect)
    point = pygame.mouse.get_pos()
    square_collide = square.collidepoint(point)
    circ_collide = circ.collidepoint(point)
    rect_collide = rect.collidepoint(point)
    if shape == "square":
        if square_collide:
            shape = random.choice(shapes)
        elif rect_collide or circ_collide:
            shape = random.choice(shapes)
    elif shape == "circle":
        if circ_collide:
            shape = random.choice(shapes)
        elif rect_collide or square_collide:
            shape = random.choice(shapes)
    elif shape == "rectangle":
        if rect_collide:
            shape = random.choice(shapes)
        elif square_collide or circ_collide:
            shape = random.choice(shapes)
    pygame.draw.rect(screen, (255,255,255), square)
    pygame.draw.circle(screen, (255,255,255), (80,300), 60)
    pygame.draw.rect(screen, (255,255,255), rect)
    pygame.display.flip()
pygame.quit()
exit()

for the sake of length i cut out the part where collisions would make the shapes change different colors the collisions themselves are the user interactions you can see that the shape variable will just randomize very quickly after one user interaction in this case. i want it to randomize a shape, and then after the user hovers over a shape (whether it be right or wrong), randomize one more but not as quickly. shot in the dark here, would it have to recognize there is no more collision first? or is there something else i should do?

i also tried other combinations which tried to update the variable with a random choice, but while it was under that while True loop it would continually randomize and update and i don't really know what's wrong.

again, sorry for the inexperience in all of this. i can share the entire if that would help but since it's a little messy and long i opted to not in the question. thank you in advance for the help!


Solution

  • You should have a look into pygame.time.Clock with pygame.tick(fps). If you set the fps really low you come close to want you want without changing how the code works.

    clock = pygame.time.Clock()
    fps = 1  # which would be really slow, but you'll see what happen
    
    while run: 
       clock.tick(fps)
       ...
    

    Another possibility is to set a delay with pygame.time.delay(), for example everytime the game changes the shape, wait a second before doing anything. Here is a link to the docu. There is also clock explained.