Warning - This site is moving to https://getthecodingbug.anvil.app
Topics covered
Movement / Controls
Staying on screen
Enemy sprites
Spawning Enemies
Movement / Controls
This is going to be a keyboard controlled game, so we want the player to move when the Left or Right arrow keys are pressed. In "player.py", replace the "def update(self):" function with the following:
def update(self): self.speedx = 0 keystate = pygame.key.get_pressed() if keystate[pygame.K_LEFT]: self.speedx = -8 if keystate[pygame.K_RIGHT]: self.speedx = 8 self.rect.x += self.speedx
Staying on screen
We need to make sure that our sprite doesn’t go off the screen. So add the following to the Player update():
if self.rect.right > WIDTH: self.rect.right = WIDTH if self.rect.left < 0: self.rect.left = 0
Now if the ship ever tries to move to a position that is past the left or right edges of the screen, it will stop.
Another option would be to wrap around the screen - teleporting the sprite from one side to the other when it hits the edge - but for this game, stopping on the edges feels more appropriate.
Enemy sprites
Next we will create some enemies for our space ship. At this point, we don’t need to worry about what our enemy sprites are, we just want to get them on the screen. You might decide your game is about a spaceship dodging meteors or a unicorn dodging flying pizzas - as far as the code is concerned, it doesn’t matter.
Start by creating a new file and saving it as "enemy.py". We’ll start by defining the sprite properties:
from common import * class Enemy(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((30, 40)) self.image.fill(RED) self.rect = self.image.get_rect() self.rect.x = random.randrange(WIDTH - self.rect.width) self.rect.y = random.randrange(-100, -40) self.speedy = random.randrange(1, 8)
This will create a new enemy sprite at the top of the screen in a random position.
As we are using "random" we must add "import random" to the top of "common.py".
Next add this function to "enemy.py" to control the movement of the enemy down the screen. Notice how "randrange" is used to control its random coordinates and speed.
def update(self): self.rect.y += self.speedy if self.rect.top > HEIGHT + 10: self.rect.x = random.randrange(WIDTH - self.rect.width) self.rect.y = random.randrange(-100, -40) self.speedy = random.randrange(1, 8)
Spawning Enemies
We are going to want to have many enemies, so we’re going to make a new group called "enemies" to hold them all. This is also going to make our lives easier in later steps. Then we spawn a number of "enemies" and add them to the groups, so add these lines after "all_sprites.add(player)" in "main.py":
enemies = pygame.sprite.Group() for i in range(8): m = Enemy() all_sprites.add(m) enemies.add(m)
Save all your files and run "main.py" and see what happens.
This is great, but it’s a little boring to just have the enemies all moving straight down.
Let’s add a little bit of movement in the x direction as well. Add this line to the end of the "init" function in "enemy.py":
self.speedx = random.randrange(-3, 3)
def update(self): self.rect.x += self.speedx self.rect.y += self.speedy if self.rect.top > HEIGHT + 10 or self.rect.left < -25 or \ self.rect.right > WIDTH + 20: self.rect.x = random.randrange(WIDTH - self.rect.width) self.rect.y = random.randrange(-100, -40) self.speedy = random.randrange(1, 8)
and change the "def update" function to this:
Notice the "if" statement, above, has a trailing "\", this is how to tell Python that there is more on the line below.
In the next section, we’ll learn how to detect when two sprites run into each other (collide) and enable the player to shoot back at the enemies.
Click this link to move on to the next section: Lesson 9 d