Hi I'm new to using pygame and I'm trying to build a game with sprites of puppies moving randomly around the screen and bouncing off the boundaries.
I'm using the method update() within my sprite class on my group of sprites ' puppies ' to update the sprites' individual locations, however when I run the code all of the sprites seem to move together so I'm wondering if I should be structuring the code differently or putting the sprites in individual groups?
Here's the code:
import pygame, random
pygame.init()
class puppy(pygame.sprite.Sprite):
def __init__(self, pos_x, pos_y,filepath, speed=[2,2]):
super().__init__()
self.image = pygame.image.load(filepath)
self.rect=self.image.get_rect()
self.rect.center = [pos_x,pos_y]
self.speed = speed
def update(self):
if (self.rect.x < 0) or (self.rect.x > 850):
self.speed[0] *= -1
if (self.rect.y < 0) or (self.rect.y > 615):
self.speed[1] *= -1
self.rect.x = self.rect.x + self.speed[0]
self.rect.y = self.rect.y + self.speed[1]
puppies = pygame.sprite.Group()
dogimages = ['pupp1.png','pupp2.png','pupp3.png','pupp4.png','pup5.png']
for i in range(5):
new_pup=puppy(random.randint(75,925),random.randint(75,675),dogimages[i])
puppies.add(new_pup)
screen = pygame.display.set_mode((1000, 750))
screen.fill((255,255,255))
clock=pygame.time.Clock()
background = pygame.image.load('bg.jpeg')
running=True
while running:
clock.tick(60)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.blit(background, (0, 0))
puppies.draw(screen)
puppies.update()
pygame.display.update()