5. Arcade games

5.1. Collisions

Arcade games need to know when one sprite has hit another sprite. Most of this code is copied from Program 4.3 and Program 4.6.

Program 5.1 Collisions
 1from rlzero import *
 2
 3WIDTH = 500
 4HEIGHT = 500
 5
 6alan = Sprite('alien.png')
 7alan.pos = (400, 50)
 8box = Rectangle(20, 20, 100, 100)
 9
10def draw():
11    draw_rectangle_rec(box, RED)
12    alan.draw()
13
14def update():
15    if keyboard.right:
16        alan.x = alan.x + 2
17    elif keyboard.left:
18        alan.x = alan.x - 2
19    box.x = box.x + 2
20    if box.x > WIDTH:
21        box.x = 0
22    if alan.colliderect(box):
23        print("hit")
24
25
26run()
27

Exercise

Add vertical movement (as you did in Exercise Program 4.6).

Advanced

Make the box chase the alien.

Advanced

Print number of times the box hits the alien (i.e. the score).

5.2. Chase

Instead of moving constantly to the right we can make the movement conditional with an if statement so the box chases the alien. Most of this code is copied from Program 5.1. New lines are highlighted. We have also changed what happens when the box catches the alien: the program now exits and you must run it again to play again. This may not be what you want in your game!

Program 5.2 Alien chase
 1from rlzero import *
 2
 3WIDTH = 500
 4HEIGHT = 500
 5
 6alan = Sprite("alien.png")
 7alan.pos = (400, 50)
 8box = Rectangle(20, 20, 100, 100)
 9
10def draw():
11    draw_rectangle_rec(box, RED)
12    alan.draw()
13
14def update():
15    if keyboard.right:
16        alan.x = alan.x + 2
17    elif keyboard.left:
18        alan.x = alan.x - 2
19    if box.x < alan.x:
20        box.x = box.x + 1
21    if box.x > alan.x:
22        box.x = box.x - 1
23    if alan.colliderect(box):
24        exit()
25
26run()

Exercise

Add vertical movement (as you did in previous exercise).

Advanced

Draw a new enemy image. Save it as enemy.png in your mu_code/images folder. Load it as an Actor(‘enemy’) instead of the Rect().

5.3. Powerup

Instead of an enemy the box here is a powerup that the player must collect. When he does it disappears and moves to a new location.

Program 5.3 Collect the powerups
 1from rlzero import *
 2import random
 3
 4WIDTH = 500
 5HEIGHT = 500
 6
 7alan = Sprite("alien.png")
 8alan.pos = (400, 50)
 9box = Rectangle(20, 20, 100, 100)
10score = 0
11
12def draw():
13    draw_rectangle_rec(box, GREEN)
14    alan.draw()
15
16def update():
17    global score
18    if keyboard.right:
19        alan.x = alan.x + 2
20    elif keyboard.left:
21        alan.x = alan.x - 2
22    if alan.colliderect(box):
23        box.x = random.randint(0, WIDTH)
24        box.y = random.randint(0, HEIGHT)
25        score = score + 1
26        print("Score:", score)
27
28run()

Exercise

Add vertical movement.

Exercise

Draw a new powerup image. Save it as powerup.png in the same folder as your program. Load it as a Sprite("powerup.png") instead of the Rectangle().

Advanced

Combine this program with the enemy from Program Program 5.2 and the background from Program 4.5 and whatever else you want to make your own game.

5.4. Sounds

RLZero comes one sound effect: eep.wav. If you want more you will have to create them (or download them) yourself and save them in the same folder as your program.

This program plays a sound when you press space.

Program 5.4 Sound
 1from rlzero import *
 2
 3my_sound = Sound("eep.wav")
 4
 5def draw():
 6    clear_background(WHITE)
 7
 8def update():
 9    if keyboard.space:
10        my_sound.play()
11
12run()

Exercise

Download a .wav audio file and play it in the program.

5.5. Simple animation

This program changes the image of the Sprite to create a simple animation when he is hit.

RLZero comes with the image file alien_hurt.png. If you want more you will have to create them (or download them) yourself and save them in the same folder as your program.

Most of this code is copied from Program 5.1

Program 5.5 Sound and animation upon collision
 1from rlzero import *
 2
 3WIDTH = 500
 4HEIGHT = 500
 5alan = Sprite("alien.png")
 6alan.pos = (0, 50)
 7box = Rectangle(20, 20, 100, 100)
 8eep = Sound("eep")
 9
10def draw():
11    draw_rectangle_rec(box, RED)
12    alan.draw()
13
14def update():
15    if keyboard.right:
16        alan.x = alan.x + 2
17    elif keyboard.left:
18        alan.x = alan.x - 2
19    box.x = box.x + 2
20    if box.x > WIDTH:
21        box.x = 0
22    if alan.colliderect(box):
23        alan.image_file = "alien_hurt.png"
24        eep.play()
25    else:
26        alan.image_file = "alien.png"
27
28run()

Exercise

Record your own sound effect and add it to the game.

Advanced

Add more boxes or sprites that move in different ways for the player to avoid.

Advanced

Add a second alien controlled by different keys or gamepad for player 2.

5.6. Mouse clicks

This uses a function call-back for event-based input. It is similar to Program 5.5 but:

  • The box has been removed.

  • There is an on_mouse_down() special function that is called automatically when the player click the mouse.

  • The score is displayed.

See Program 2.17 for more about functions.

Program 5.6 Getting input from mouse clicks
 1from rlzero import *
 2
 3alan = Sprite("alien.png")
 4alan.pos = (0, 50)
 5eep = Sound("eep")
 6
 7score = 0
 8
 9def draw():
10    alan.draw()
11    draw_text("Score "+str(score), 0, 0, 20, WHITE)
12
13def update():
14    if keyboard.right:
15        alan.x = alan.x + 2
16    elif keyboard.left:
17        alan.x = alan.x - 2
18    alan.image = 'alien.png'
19
20def on_mouse_down(pos, button):
21    global score
22    if button == MOUSE_BUTTON_LEFT and alan.collidepoint(pos):
23        alan.image = 'alien_hurt.png'
24        eep.play()
25        score = score + 1
26
27run()
28

5.7. Mouse movement

Program 5.7 Getting input from mouse movement
 1from rlzero import *
 2
 3# wiggle your mouse around the screen!
 4
 5alan = Sprite("alien.png")
 6
 7def draw():
 8    alan.draw()
 9
10def on_mouse_move(pos):
11    alan.pos = pos
12
13run()

Exercise

What happens if you change on_mouse_move to on_mouse_down?

Advanced

Make a game with one alien controlled by mouse and another controlled by keyboard