Laser Asteroid Collision¶
We have our lasers shooting at a reasonable frequency, now we need them to do something. Specifically we will make them destroy any asteroids that they collide with.
Planning¶
We have written very similar code to address the collision between an Asteroid and the Ship. This time, instead of ending the game, we simply want to destroy the asteroid.
Stating this clearly, upon collision between an Asteroid and a Laser we want:
the asteroid to be destroyed
the laser to keep moving across the screen
We need to work out how to destroy objects. Check the GameFame Documentation under the RoomObjects methods and you will find the delete_object
method that removes an object from the room.
We will place the code, summarised in the IPO table below, in the Laser class.
Coding¶
Remember in GameFrame handling collisions is a two step process. First you must register which object collisions to detect using the register_collision_object
method, and then create a methods called handle_collision
which holds the game logic associated with the collision.
Objects/Laser.py
¶
Open Objects/Laser.py
and add the highlighted code below.
1from GameFrame import RoomObject, Globals
2
3class Laser(RoomObject):
4 """
5 Class for the lasers shot by the Ship
6 """
7
8 def __init__(self, room, x, y):
9 """
10 Inistialise the laser
11 """
12 # include attributes and methods from RoomObject
13 RoomObject.__init__(self, room, x, y)
14
15 # set image
16 image = self.load_image("Laser.png")
17 self.set_image(image, 33, 9)
18
19 # set movement
20 self.set_direction(0, 20)
21
22 # handle events
23 self.register_collision_object("Asteroid")
24
25 def step(self):
26 """
27 Determine what happens to the laser on each tick of the game clock
28 """
29 self.outside_of_room()
30
31 def outside_of_room(self):
32 """
33 removes laser if it has exited the room
34 """
35 if self.x > Globals.SCREEN_WIDTH:
36 self.room.delete_object(self)
37
38 # --- Event handlers
39 def handle_collision(self, other, other_type):
40 """
41 Handles laser collisions with other registered objects
42 """
43 if other_type == "Asteroid":
44 self.room.delete_object(other)
Most of this code should be familiar, but we’ll investigate it anyway:
lines 22-23: registers collisions with
Asteroid
objects as an event that must be handledlines 38-44: handles registered collisions
line 43: handles collisions with
Asteroid
objectsline 44:
deletes the
other
object - in this case the asteroid the laser has collided withfrom the room that this ship (
self
) is in
Save Laser.py
then run MainController.py
to test that everything is working as planned.
Commit and Push¶
We have finished and tested another section of code so we should make a Git commit.
To do this:
In GitHub Desktop go to the bottom left-hand box and write into the summary Laser asteroid collision.
Click on Commit to main
Click on Push origin
Now the work from this lesson is committed and synced with the online repo.
Completed File States¶
Below are all the files we used in this lesson in their finished state. Use this to check if your code is correct.
Objects/Laser.py
¶
1from GameFrame import RoomObject, Globals
2
3class Laser(RoomObject):
4 """
5 Class for the lasers shot by the Ship
6 """
7
8 def __init__(self, room, x, y):
9 """
10 Inistialise the laser
11 """
12 # include attributes and methods from RoomObject
13 RoomObject.__init__(self, room, x, y)
14
15 # set image
16 image = self.load_image("Laser.png")
17 self.set_image(image, 33, 9)
18
19 # set movement
20 self.set_direction(0, 20)
21
22 # handle events
23 self.register_collision_object("Asteroid")
24
25 def step(self):
26 """
27 Determine what happens to the laser on each tick of the game clock
28 """
29 self.outside_of_room()
30
31 def outside_of_room(self):
32 """
33 removes laser if it has exited the room
34 """
35 if self.x > Globals.SCREEN_WIDTH:
36 self.room.delete_object(self)
37
38 # --- Event handlers
39 def handle_collision(self, other, other_type):
40 """
41 Handles laser collisions with other registered objects
42 """
43 if other_type == "Asteroid":
44 self.room.delete_object(other)