2

This is the list

places = ["London", "India", "America", "Australia", "Cambodia", "China", 
          "Dubai", "Egypt",  "France", "Germany", "Japan", "Jordan", 
          "Korea", "Myanmar", "Peru", "Russia", "Singapore", "Spain"]

I randomised the list:

random_place = random.choice(places)

Here I have tried to display random items from the list. I have tried using for loop and time.sleep function but it didn't work.

def text():
    game_font = pygame.freetype.SysFont("monospace", 35)
    text_surface, rect = game_font.render(random_place, (0, 0, 0))
    screen.blit(text_surface, (680, 420))
   
# Game loop
running = True
while running:
    screen.fill((255, 194, 102))  # RGB
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
   
    card()
    text()
    pygame.display.update()

It would be great if anyone could solve this issue.

not_speshal
  • 22,093
  • 2
  • 15
  • 30
Shreyas
  • 55
  • 5

2 Answers2

2

Use a timer event. In pygame exists a timer event. Use pygame.time.set_timer() to repeatedly create a USEREVENT in the event queue. The time has to be set in milliseconds.

Choose a new random card, when the timer event occurs:

random_place = random.choice(places)

timer_interval = 2000 # 2000 milliseconds = 2 seconds
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event, timer_interval)

running = True
while running:
    screen.fill((255, 194, 102))  # RGB
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == timer_event:
            random_place = random.choice(places)

    card()
    text()
    pygame.display.update()

Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to be between pygame.USEREVENT (24) and pygame.NUMEVENTS (32). In this case pygame.USEREVENT+1 is the event id for the timer event.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
2

I've added two small blocks of code to your code to make this work. It uses the time.time() method to retrieve the current time in seconds every iteration of the game loop:

import pygame
import random
import time

pygame.init()
pygame.font.init()

screen = pygame.display.set_mode((1000, 800))

places = ["London", "India", "America", "Australia", "Cambodia", "China", 
          "Dubai", "Egypt",  "France", "Germany", "Japan", "Jordan", 
          "Korea", "Myanmar", "Peru", "Russia", "Singapore", "Spain"]

def text():
    game_font = pygame.freetype.SysFont("monospace", 35)
    text_surface, rect = game_font.render(random_place, (0, 0, 0))
    screen.blit(text_surface, (680, 420))

interval = 2 # Set interval in seconds
last_time = time.time() # Get the current time in seconds
random_place = random.choice(places)

# Game loop
running = True
while running:
    screen.fill((255, 194, 102))  # RGB
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
   
#    card()
    # If 2 seconds has past since the last update to last_time
    if time.time() - last_time >= interval: 
        random_place = random.choice(places)
        last_time = time.time()
        
    text()
    pygame.display.update()

pygame.quit()

Output:

enter image description here

Red
  • 26,798
  • 7
  • 36
  • 58