I'm new to programming and I need a way to limit the randomization of the variable food_choice so it doesn't print tons of images on my pygame screen.
Here is a section of my code. I believe the problem is because my function draw_food() is inside of the main loop, meaning that everytime the game runs with the determined tick, the function draw_food() is then run again, picking another food, causing an endless randomization. I need to way to limit this randomization to once per x seconds.
def draw_food():
food_choice = random.choice(food_types)
for object in object_list[:]:
screen.blit(food_choice, (foodx, foody))
object.deter -= 0.035
if object.deter < 1:
object_list.remove(object)
def draw_game_window():
screen.blit(bg, (0, 0))
draw_food()
cat.draw(screen)
pygame.display.update()
# main loop
cat = player(200, 355, 67, 65)
run = True
object_list = []
time_interval = 5000 # 200 milliseconds == 0.2 seconds
next_object_time = 0
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_game_window()
current_time = pygame.time.get_ticks()
if current_time > next_object_time:
next_object_time += time_interval
object_list.append(Object())
pygame.quit()```