Python/Pygame make text inside a for loop move constantly

Viewed 71
while game_loop == True:
    cur = pygame.mouse.get_pos()
    click =pygame.mouse.get_pressed()
    
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT:
            terminate()

                
    game_display.fill(white)
    



        

    x_change =  random.randint(-25,25)
    y_change = random.randint(-25,25)
    
    x += x_change
    y += y_change


    
    text_correct(very_big_font,correct_character,black,cool_red,game_display,x,y,action = "correct" )
    
    for i in range (0, len(wrong_character_list)):
        x2 = random_character_list_spot[i+1][0]
        y2 = random_character_list_spot[i+1][1]
        text_correct(very_big_font, wrong_character_list [i],black,cool_red,game_display,x2,y2,action = "wrong")        
        x2 += x_change
        y2 += y_change
    
    pygame.display.update()
    Clock.tick(FPS)

The "text_correct" moves fine. The x_change and y_change decide how the text is going to move. But I do not know how to make it move inside the for loop. Wrong_character_list is a list containg the alphabet, and you have to click the "correct_character" But I can make the ones inside the foor loop move. It just stays in place. Does anybody know how?

1 Answers

You have to update the positions in the list:

for i in range(len(wrong_character_list)):
    x2, y2 = random_character_list_spot[i+1]
    text_correct(very_big_font, wrong_character_list[i],
        black,cool_red,game_display,x2,y2,action = "wrong")        
    x2 += x_change
    y2 += y_change
    random_character_list_spot[i+1] = [x2, y2] # <--- update position

If you want to move it differently, then you have to compute x_change and y_change in the loop:

for i in range(len(wrong_character_list)):
    x_change =  random.randint(-25,25)
    y_change = random.randint(-25,25) 

    x2, y2 = random_character_list_spot[i+1]
    text_correct(very_big_font, wrong_character_list[i],
        black,cool_red,game_display,x2,y2,action = "wrong")        
    x2 += x_change
    y2 += y_change
    random_character_list_spot[i+1] = [x2, y2] # <--- update position
Related