Spawning multiple instances of the same object concurrently in python

走远了吗. 提交于 2021-02-12 09:24:37

问题


Hey I'm a beginner programmer who is starting with python and am starting out by making a game in pygames. The game basically spawns in circles at random positions, and when clicked give you points. Recently i've hit a roadblock when I want to spawn in multiple instances of the same object(in this case circles) at the same time. I've tried stuff like sleep() and some other code related to counters, but it always results in the next circle spawned overriding the previous one(i.e the program spawns in circle 1, but when circle 2 comes in, circle 1 disappears). Anyone know a solution to this? Would really appreciate your help!

import pygame
import random
import time

pygame.init()

window = pygame.display.set_mode((800,600))

class circle():
    def __init__(self, color, x, y, radius, width,):
        self.color = color
        self.x = x
        self.y = y
        self.radius = radius
        self.width = width

    def draw(self, win, outline=None):
        pygame.draw.circle(win, self.color, (self.x, self.y, self.radius, self.width), 0)

run=True
while run:
    window.fill((0, 0, 0))
    pygame.draw.circle(window, (255, 255, 255), (random.randint(0, 800),random.randint(0, 600)), 20, 20)
    time.sleep(1)
    pygame.display.update()

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            run=False
            pygame.quit()
            quit()

回答1:


The previous drawn circle disappear, because the display is cleared in every frame by

window.fill((0, 0, 0))

Add a list for the circle objects:

circles = []

Use pygame.time.Clock / tick() to control the frames per second. tick() returns the number of milliseconds since the previous call:

clock = pygame.time.Clock()
FPS = 60

run=True
while run:
    delta_ms = clock.tick()

    # [...]

Count the past time (current_time += delta_ms) and create new Circle object when one second was elapsed and set the next time threshold (next_circle_time = current_time + 1000). Add the circle to the list.

current_time = 0
next_circle_time = 0

run=True
while run:
    delta_ms = clock.tick()

    current_time += delta_ms
    if  current_time > next_circle_time:
        next_circle_time = current_time + 1000 # 1000 milliseconds (1 second) 
        new_circle = Circle((255, 255, 255), random.randint(0, 800), random.randint(0, 600), 20, 20)
        circles.append(new_circle)

Draw the circles in a loop:

for c in circles:
    c.draw(window)

See the example:

import pygame
import random
import time

pygame.init()

window = pygame.display.set_mode((800,600))

class Circle():
    def __init__(self, color, x, y, radius, width):
        self.color = color
        self.x = x
        self.y = y
        self.radius = radius
        self.width = width

    def draw(self, win, outline=None):
        pygame.draw.circle(win, self.color, (self.x, self.y), self.radius, self.width)

circles = []

clock = pygame.time.Clock()
FPS = 60

current_time = 0
next_circle_time = 0

run=True
while run:
    delta_ms = clock.tick()

    current_time += delta_ms
    if  current_time > next_circle_time:
        next_circle_time = current_time + 1000 # 1000 milliseconds (1 second) 
        r = 20
        new_circle = Circle((255, 255, 255), random.randint(r, width-r), random.randint(r, height-r), r, r)
        circles.append(new_circle)

    window.fill((0, 0, 0))
    for c in circles:
        c.draw(window)
    pygame.display.update()

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            run=False
            pygame.quit()
            quit()


来源:https://stackoverflow.com/questions/62112754/spawning-multiple-instances-of-the-same-object-concurrently-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!