Enemies Arrive
Part of the Sprites and Images section of Coddy's Pygame journey. Lesson 32 of 36.
Now the enemies. An Enemy sprite gets its picture and speed when it is made, so one class can draw every kind:
class Enemy(pygame.sprite.Sprite):
def __init__(self, image, x, y, speed):
super().__init__()
self.image = image
self.rect = self.image.get_rect(center=(x, y))
self.speed = speed
def update(self):
self.rect.y += self.speedA wave is several enemies, lined up above the window so they come in one after another, each 120 pixels above the last, at random x:
def spawn_wave(image, count, speed):
for i in range(count):
x = random.randint(40, 600)
enemies.add(Enemy(image, x, -40 - i * 120, speed))The first is centred at y = -40, the next at -160, and so on: all out of sight at the start. A function keeps the wave in one place; the last lesson calls it for every wave.
An enemy that falls past the bottom has to go. In the main loop, after enemies.update(), kill any enemy whose top is past 480. Later that will cost a life.
Challenge
Medium- Add
import random, loadenemy_red.pngintored_image, and writeEnemyandspawn_wave. - Make
enemies = pygame.sprite.Group(), then callspawn_wave(red_image, 6, 2). - Each frame, update the enemies, kill those whose
rect.topis past 480, and draw them before the lasers.
Try it yourself
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Star Patrol")
clock = pygame.time.Clock()
ship_image = pygame.image.load("assets/images/ship.png").convert_alpha()
laser_image = pygame.image.load("assets/images/laser.png").convert_alpha()
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = ship_image
self.rect = self.image.get_rect(midbottom=(320, 470))
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.rect.x -= 6
if keys[pygame.K_RIGHT]:
self.rect.x += 6
self.rect.clamp_ip(screen.get_rect())
class Laser(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = laser_image
self.rect = self.image.get_rect(midbottom=(x, y))
def update(self):
self.rect.y -= 10
if self.rect.bottom < 0:
self.kill()
player = Player()
lasers = pygame.sprite.Group()
last_shot = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
player.update()
keys = pygame.key.get_pressed()
now = pygame.time.get_ticks()
if keys[pygame.K_SPACE] and now - last_shot >= 250:
lasers.add(Laser(player.rect.centerx, player.rect.top))
last_shot = now
lasers.update()
screen.fill((12, 14, 30))
lasers.draw(screen)
screen.blit(player.image, player.rect)
pygame.display.flip()
clock.tick(60)
pygame.quit()
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Sprites and Images
4Sprites and Groups
The Sprite ClassGroupsRemove with kill()Catch with spritecollideGroup Against Group7Star Patrol
The Patrol ShipFire Lasers3Classes for Game Objects
Your First ClassMethodsSelf Is the ObjectA List of ObjectsObjects with ImagesA SubclassPractice on your own: Pygame playground