[Python] I am having trouble creating clones in pygame [duplicate]

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make a game in pygame where you should hit certain asteroids with a spaceship, but when I create the asteroids and try to write a number on them, the text prints on all asteroids.

    # the asteroid class
    class Asteroid(pygame.sprite.Sprite):
    def __init__(self, image):
    super().__init__()

    self.answer = random.randint(0, 5)

    self.image = image
    self.rect = image.get_rect()
    self.font = pygame.font.SysFont("Arial", 50)

    self.text_surface = self.font.render(str(self.answer), False, "black")
    self.text_rect = self.text_surface.get_rect(center=self.rect.center)

    def compare(self, value):
    if value == self.answer:
    return True
    else:
    return False

    def update(self, screen):
    screen.blit(self.image, self.rect)
    self.image.blit(self.text_surface, self.text_rect)

    ###############################################

    # creating and updating clones
    countdown -= 1
    if countdown <= 0:
    countdown = countdown_max
    asteroid = Asteroid(asteroid_img)
    asteroids.append(asteroid)
    asteroid.rect.x = size[0] + 75
    asteroid.rect.y = random.randint(0, size[1] - 150)

    for asteroid in asteroids:
    asteroid. Update(screen)


    EDIT: I only had the screen argument for the update, i forgot to change it.

    Continue reading...

Compartilhe esta Página