I'd like the flower image to sense the collision with the basket image. I've tried colliderect and sprite.collide_rect(). the program runs, but it doesn't seem to print("it works") onto the terminal, which I'm assuming is because it doesn't detect the collision.I'd like to know how I can change it so that the fleur object can detect the player.
class Basket(pygame.sprite.Sprite):
def __init__(self,x,y, name):
super().__init__()
self.x=x
self.y=y
self.image = pygame.image.load(name).convert_alpha()
self.rect = self.image.get_rect()
def render(self):
main_screen.blit(self.image, (self.x,self.y))
player = Basket(playerXPosition, playerYPosition, 'basket.png')
class flower(pygame.sprite.Sprite):
def __init__(self, x, y, img):
super().__init__()
self.image = pygame.image.load(img).convert_alpha()
self.image.set_colorkey(None)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.counter = 0
def move(self):
distance = 80
speed = 2
if self.counter >= 0 and self.counter <= distance:
self.rect.y += speed
elif self.counter >= distance and self.counter <= distance*2:
self.rect.y += speed
else:
self.counter = 0
self.counter += 1
flower_list = pygame.sprite.Group()
run = True
while run:
flower_x_position = random.randint(0,1000)
flowerYposition = random.randint(-600,-300)
fleur = flower( flower_x_position,flowerYposition, 'sakura.png')
for event in pygame.event.get():
flower_list.add(fleur)
if event.type == pygame.QUIT:
run = False
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
player.x -= player_velocity
if keys[pygame.K_d]:
player.x += player_velocity
else:
print("error")
for f in flower_list:
f.move()
if pygame.sprite.collide_rect(fleur, player):
print("I work!")
main_screen.fill(white)
main_screen.blit(background_for_main_game, (0,0))
player.render()
flower_list.draw(main_screen)
pygame.display.flip()
pygame.display.update()
clock.tick(40)