1

im fairly new to coding and python, i was messing around with pygame and i was wondering if theres a way i could limit the amount of circles that spawn in this game im making? when i run the code, it just spawns in circles all over the place really fast. i tried doing the time.sleep thing, but all it does is slow down the entire game.

import pygame
import random
import time

pygame.init()
y = 0
x = 0
point = 0
is_blue = True
WHITE = (255, 255, 255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode([500, 500])



def food():
    pygame.draw.circle(screen, WHITE, (random.randint(1, 400), random.randint(1, 400)), 5)


done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            is_blue = not is_blue

    pygame.display.set_caption("Collect the balls to win!")

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]:
        y -= 3
    if pressed[pygame.K_DOWN]:
        y += 3
    if pressed[pygame.K_LEFT]:
        x -= 3
    if pressed[pygame.K_RIGHT]:
        x += 3

    if x <= -1:
        x = 0
    if x >= 441:
        x = 440
    if y <= -1:
        y = 0
    if y >= 441:
        y = 440

    screen.fill((0, 0, 0))
    if is_blue:
        color = (0, 128, 255)
    else:
        color = (255, 100, 0)

    pygame.draw.rect(screen, color, pygame.Rect(x, y, 30, 30))
    food()
    pygame.display.flip()
    clock.tick(144)
bruh3436
  • 49
  • 3
  • keep track of how many ... only spawn if its less than some number – Joran Beasley Oct 31 '21 at 08:27
  • Is the answer acceptable? See [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). – Rabbid76 Nov 03 '21 at 17:45

1 Answers1

0

You have to use a list. Create a list for the food positions:

food_list = []

Fill the list in a loop:

while len(food_list) < 10:
    food_list.append((random.randint(1, 400), random.randint(1, 400)))

Draw the foods in the list in a loop:

for food_pos in food_list:
        pygame.draw.circle(screen, WHITE, food_pos, 5)

You can also spawn the food with an time interval. Use pygame.time.get_ticks() to measure the time. Define a time interval after which a new object should appear. Create an object when the point in time is reached and calculate the point in time for the next object:

food_list = []
time_interval = 1000 # 1000 milliseconds == 1 seconds
next_food_time = pygame.time.get_ticks() 

done = False
while not done:
    # [...]

    current_time = pygame.time.get_ticks()
    if current_time > next_food_time and len(food_list) < 10:
        next_food_time += time_interval
        food_list.append((random.randint(1, 400), random.randint(1, 400)))

See also Spawning multiple instances of the same object concurrently in python


Complete example:

import pygame
import random
import time

pygame.init()
y = 0
x = 0
point = 0
is_blue = True
WHITE = (255, 255, 255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode([500, 500])


food_list = []
time_interval = 1000 # 1000 milliseconds == 1 seconds
next_food_time = pygame.time.get_ticks() 

done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            is_blue = not is_blue

    pygame.display.set_caption("Collect the balls to win!")

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]:
        y -= 3
    if pressed[pygame.K_DOWN]:
        y += 3
    if pressed[pygame.K_LEFT]:
        x -= 3
    if pressed[pygame.K_RIGHT]:
        x += 3

    if x <= -1:
        x = 0
    if x >= 441:
        x = 440
    if y <= -1:
        y = 0
    if y >= 441:
        y = 440

    current_time = pygame.time.get_ticks()
    if current_time > next_food_time and len(food_list) < 10:
        next_food_time += time_interval
        food_list.append((random.randint(1, 400), random.randint(1, 400)))

    screen.fill((0, 0, 0))
    if is_blue:
        color = (0, 128, 255)
    else:
        color = (255, 100, 0)

    pygame.draw.rect(screen, color, pygame.Rect(x, y, 30, 30))
    for food_pos in food_list:
        pygame.draw.circle(screen, WHITE, food_pos, 5)
    pygame.display.flip()
    clock.tick(144)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174