I need to draw sprites in random position in the screen, how can I do this?
class SpriteOrange(MyGame):
def __init__(self, image):
self.image = pygame.image.load(image)
self.x = 0
self.y = 0
def draw1(self, screen):
screen.blit(self.image, self.x, self.y)
def update(self):
self.x = random.randrange(0, 400)
self.y = random.randrange(0, 400)
You could create class SpriteOrange
with function draw(screen)
and update()
(to randomly change position):
class SpriteOrange():
def __init__(self, image):
self.image = pygame.image.load(image)
self.x = 0
self.y = 0
def draw(self, screen):
screen.blit(self.image, self.x, self.y)
def update(self):
self.x = random.randrange(0,400)
self.y = random.randrange(0,400)
You can create list of instances of SpriteOrange
self.oranges = []
for x in range(10):
self.oranges.append(SpriteOrange('orange.png'))
You can change positions:
for o in self.oranges:
o.update()
You can draw:
for o in self.oranges:
o.draw(self.screen)
If you need more read about pygame.sprite.Sprite and pygame.sprite.Group