Mechanic - Unfair Punishment

In the Game Design lesson, we identified when a game involved unfair punishment, it undermines the players experience of interactivity. We also identified a mechanic that unfairly punished the player:

Unfair punishment

Solution

A laser shot at an asteroid can pass through the asteroid and hit an astronaut

Have the asteroid disappear when it collides with the first object

In this lesson we will remove this unfair punishment.

Planning

We have already identified a solution for this problem: Have the asteroid disappear when it collides with the first object. Fortunately, we already know the tool to achieve this, since we have used it before. We have used delete_object() many times throughout our code, so how should we apply it here? To answer that we need to consider two questions:

  • What object needs to be deleted?

    • the Laser

  • When event trigger the deletion?

    • Either of the two Laser collisions (asteroid or astronaut)

What does that look like in a IPO table?

laser deletion IPO

We simply need to include the delete_object() method in both the asteroid collision event handler and the astronaut collision event handler. Let’s get coding.

Coding

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        self.register_collision_object("Astronaut")
25        
26    def step(self):
27        """
28        Determine what happens to the laser on each tick of the game clock
29        """
30        self.outside_of_room()
31        
32    def outside_of_room(self):
33        """
34        removes laser if it has exited the room
35        """
36        if self.x > Globals.SCREEN_WIDTH:
37            self.room.delete_object(self)
38            
39    # --- Event handlers
40    def handle_collision(self, other, other_type):
41        """
42        Handles laser collisions with other registered objects
43        """
44        if other_type == "Asteroid":
45            self.room.asteroid_shot.play()
46            self.room.delete_object(other)
47            self.room.score.update_score(5)
48        elif other_type == "Astronaut":
49            self.room.astronaut_shot.play()
50            self.room.delete_object(other)
51            self.room.score.update_score(-10)
52        self.room.delete_object(self)

Let’s unpack that code:

  • line 52: removes self (this instance of Laser)

    • we could have placed the delete_object() in both the asteroid and astronaut collisions, but since we want to use it on all laser collisions, we can simply place it in the handle_collision() method.

Save and close Objects\Laser.py.

Testing

Now run MainController.py to test that the laser disappears when it hits both astronauts and asteroids.

Commit and Push

We have finished and tested another section of code so we should make a Git commit.

To do this:

  1. In GitHub Desktop go to the bottom left-hand box and write into the summary Added lasers.

  2. Click on Commit to main

  3. 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        self.register_collision_object("Astronaut")
25        
26    def step(self):
27        """
28        Determine what happens to the laser on each tick of the game clock
29        """
30        self.outside_of_room()
31        
32    def outside_of_room(self):
33        """
34        removes laser if it has exited the room
35        """
36        if self.x > Globals.SCREEN_WIDTH:
37            self.room.delete_object(self)
38            
39    # --- Event handlers
40    def handle_collision(self, other, other_type):
41        """
42        Handles laser collisions with other registered objects
43        """
44        if other_type == "Asteroid":
45            self.room.asteroid_shot.play()
46            self.room.delete_object(other)
47            self.room.score.update_score(5)
48        elif other_type == "Astronaut":
49            self.room.astronaut_shot.play()
50            self.room.delete_object(other)
51            self.room.score.update_score(-10)
52        self.room.delete_object(self)