0

I'm having trouble spawning in multiple cement block sprites. I am using only one image for the sprite I want to spawn in, but I want to be able to spawn multiple of the same cement block sprite. Right now I can only spawn in one cement block that changes positions randomly using the random module.

My GitHub just incase you want to look at the rest of the code: https://github.com/Enoc-Mena99/AutoPilot

my code:

import time
import pygame
import random

#screen height & width
WIDTH = 1000
HEIGHT = 400

screen = pygame.display.set_mode((WIDTH,HEIGHT))

#debris class
class Debris(pygame.sprite.Sprite):
    def __init__(self, scale, speed):
        pygame.sprite.Sprite.__init__(self)

        self.y = HEIGHT/2 - 200
        self.speed = speed

        #load debris
        self.images = []
        img = pygame.image.load('debris/cement.png').convert_alpha()
        img = pygame.transform.scale(img, (int(img.get_width()) * scale, (int(img.get_height()) * scale)))
        self.images.append(img)
        self.image = self.images[0]
        self.rect = self.image.get_rect()

    #draw debris to screen
    def draw(self):
        #random spawns
        rand_x = random.randint(1,1000)
        screen.blit(self.image,(rand_x,self.y))
        time.sleep(3)
J.R. BEATS
  • 111
  • 6
  • 2
    You have to create multiple instances of `Debris`. – Rabbid76 Oct 28 '21 at 16:49
  • Wow that simple. Thank you. I was pulling my hair out. Why is it always the simplest things that give programmers problems? LOL – J.R. BEATS Oct 28 '21 at 16:55
  • Update: I used a forloop to do this. Is this fine?#draw debris to screen def draw(self): #random spawns for x in range(100): rand_x = random.randint(1, 1000) screen.blit(self.image,(rand_x,self.y)) time.sleep(3) – J.R. BEATS Oct 28 '21 at 17:26

0 Answers0