1
votes

1st time pygamer here does anyone know how to add more than 1 copy of a sprite into your window?

class Spriterock:

    def __init__(self,x,y,width,height):

        self.x=x

        self.y=y

        self.width=width

        self.height=height

        self.i2 = pygame.image.load("rocks.png")

    def render(self):

            window.blit(self.i2, (self.x,self.y))

Sprite2=Spriterock(random.randrange(150,350),random.randrange(100,400),62,47)

im trying to make more of the rocks

EDIT

do you guys know how to apply the same properties to the clones as well? im using

def detectCollisionsrock(x1,y1,w1,h1,x2,y2,w2,h2):

    if (x2+w2>=x1>=x2 and y2+h2>=y1>=y2):

        return True

    elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1>=y2):

        return True

    elif (x2+w2>=x1>=x2 and y2+h2>=y1+h1>=y2):

        return True

    elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1+h1>=y2):

        return True

    else:

        return False

  collisionsrock=detectCollisionsrock(Sprite1.x,Sprite1.y,Sprite1.width,Sprite1.height,rock.x,rock.y,rock.width,rock.height,)
1
Do you want to be able to edit the sprites attributes? after you've rendered it? - p99will
does collision count as attribute ? - user3162148

1 Answers

1
votes

Your current code sample would only create one rock, since you have only created one instance of the Spriterock class. This line:

Sprite2=Spriterock(random.randrange(150,350),random.randrange(100,400),62,47)

creates the first and only instance in your code sample. The code above that line just contains the class definition that specifies how a Spriterock works but does not actually create one.

To create multiple instances, you should do something like this:

rock1 = Spriterock(random.randrange(150,350),random.randrange(100,400),62,47)
rock2 = Spriterock(random.randrange(150,350),random.randrange(100,400),62,47)
rock1.render()
rock2.render()
pygame.display.update()