Bulit
Bulit

Reputation: 995

Can't see enemy sprite after launching the game

I'm learning python and right now im in pygame. Got some tutorial game that i write from begining from tutorial but after autor launch it in tut there is enemy spring moving from right to left. I was looking for a bug whole day is there any change in my code but cant see any, but after I launch my app (using Replit may be important) there is only player sprite but no enemy. What did i do wrong? And why my enemy didn't spawn?

Code Below

class Enemy(pygame.sprite.Sprite):
def __init__(self):
    super(Enemy, self).__init__()
    self.surf = pygame.Surface((20, 10))
    self.surf.fill((255, 255, 255))
    self.rect = self.surf.get_rect(
        center=(
            random.randint(screen_WIDTH + 20, screen_WIDTH + 100),
            random.randint(0, screen_HEIGHT),
        )
    )
    self.speed = random.randint(5, 20)

# Move the sprite based on speed
# Remove the sprite when it passes the left edge of the screen
def update(self):
    self.rect.move_ip(-self.speed, 0)
    if self.rect.right < 0:
        self.kill()


# Create groups to hold enemy sprites and all sprites
# - enemies is used for collision detection and position updates
# - all_sprites is used for rendering
enemies = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)

Main Loop

while running:
# Look at every event in the queue
for event in pygame.event.get():
    if event.type == KEYDOWN:
        if event.key == K_ESCAPE:
            running = False
        elif event.type == QUIT:
            running = False
        # Add a new enemy?
        elif event.type == addEnemy:
            # Create the new enemy and add it to sprite groups
            new_enemy = Enemy()
            enemies.add(new_enemy)
            all_sprites.add(new_enemy)

# Get the set of keys pressed and check for user input
pressed_keys = pygame.key.get_pressed()

player.update(pressed_keys)

enemies.update()

# Fill the screen with black
screen.fill((0, 0, 0))
# Draw all sprites
for entity in all_sprites:
    screen.blit(entity.surf, entity.rect)

if pygame.sprite.spritecollideany(player, enemies):
  player.kill()
  print("You lose")
  running = False

# Update the display
pygame.display.flip()

Upvotes: 0

Views: 50

Answers (1)

Bulit
Bulit

Reputation: 995

Ok I found a problem. Sometimes someone has to show you a place and you start looking nearby. Thanks to @JanWilamowski, everything was fine except for the event triggering addEnemy ... one indent too many.

Upvotes: 1

Related Questions