I'm trying to make the ladybug sprite shoot out the egg sprite whenever a condition is true
In this case that condition would be when the ladybug sprite hits the leaf sprites
I've managed to make that sort of happen but I'm unable to have it repeat it self It is only shooting once, I've tried a few different ways to make it work but I have been unsuccessful.
import pygame
import sys
import random
pygame.init()
res = (1000, 600)
pygame.display.set_caption("placeholder")
screen = pygame.display.set_mode(res)
worldbg = pygame.image.load("worldbg.png").convert_alpha()
clock = pygame.time.Clock()
class Egg(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.x = random.randint(1, 1000)
self.y = random.randint(1, 600)
self.x_speed = 0.2
self.y_speed = 0.2
self.x_direction = 1
self.y_direction = 1
self.frame = 0
super().__init__()
self.sprites = []
self.is_animating = False
self.sprites.append(pygame.image.load("egg.png"))
self.sprites.append(pygame.image.load("egg.png"))
self.sprites.append(pygame.image.load("egg.png"))
self.sprites.append(pygame.image.load("egg.png"))
self.current_sprite = 0
self.image = self.sprites[self.current_sprite]
self.rect = self.image.get_rect()
self.rect.center = [x, y]
def animate(self):
self.is_animating = True
def kill(self):
pygame.sprite.Sprite.kill(self)
def update(self):
if self.is_animating:
self.current_sprite += 0.20
if self.current_sprite >= len(self.sprites):
self.current_sprite = 0
self.is_animating = False
self.image = self.sprites[int(self.current_sprite)]
# this makes the bullet act like a heatsinking missile
if self.x < player.rect.x + 40:
self.x_direction = 8
self.x_direction = 4
elif self.x > player.rect.x + 40:
self.x_direction = -8
self.x_direction = -4
if self.y > player.rect.y + 40:
self.y_direction = -8
self.y_direction = -4
elif self.y < player.rect.y + 40:
self.y_direction = 8
self.y_direction = 4
self.x_speed = random.randint(1, 2) * self.x_direction
self.y_speed = random.randint(1, 2) * self.y_direction
self.x += self.x_speed
self.y += self.y_speed
self.rect.x = self.x
self.rect.y = self.y
self.rect.clamp_ip(screen.get_rect())
eggmis = False
lvl4 = True
lvl4sr = 0
ladybug = Ladybug(0, 0)
ladybug_sprites = pygame.sprite.Group()
ladybug_sprites.add(ladybug)
ladybug.rect.x = 0
ladybug.rect.y = 0
ladybug.list = pygame.sprite.Group()
ladybug.list.add(ladybug)
egg = Egg(0, 0)
egg_sprites = pygame.sprite.Group()
egg_sprites.add(egg)
egg.rect.x = 0
egg.rect.y = 0
egg.list = pygame.sprite.Group()
egg.list.add(egg)
# while loop starts here but I'll skip that
if lvl4sr >= 12:
lvl4 = True
if lvl4 is True:
chl = pygame.sprite.spritecollide(ladybug, leaf_sprites, False, pygame.sprite.collide_circle_ratio(0.2))
if chl:
playerhp -= 20
eggmis = True
if eggmis is True:
ehp = pygame.sprite.collide_circle(egg, player)
if ehp is True:
pygame.sprite.Sprite.kill(egg)
egg_sprites.draw(screen)
egg_sprites.update()
lvl4sr -= 6
this makes the bullet fires one time once the condition is reached then doesn't work