BTE1522 DRE2213 – Week 3 – Control Statements and Functions

Let’s explore learning programming by troubleshooting Codes   😀  – Flags and Scoring Systems in Python

Today’s coding session was all about debugging and enhancing a game we’ve been developing step by step. We dove into Act 4, 5, 6 and 7, focusing on how to fix some key issues in the game logic—specifically how to properly handle scoring during collisions between the player and enemies.

PBL – ‘The Problem’

We already had a working player and enemy system in the game. The player can move left and right, while an enemy drops down from the top of the screen. The challenge was ensuring the player’s score only increased by one upon a collision with the enemy. Instead, the score was skyrocketing with every game frame where the player touched the enemy, adding several points instead of just one.

This type of issue is common when developing games, where collisions can occur over multiple frames. But we only want the score to increment once per collision event. To fix this, we introduced an important concept: the flag.

Introducing Flags in Python

In programming, a flag is a boolean variable (True/False) used to indicate whether a certain condition has been met. For our game, we needed a flag to signal whether a collision between the player and enemy had already occurred. This would prevent the score from increasing continuously while the player and enemy rectangles overlap.

Using a Flag to Control Scoring

Here’s how we used the flag –

  1. Define the flag – We introduced a variable collision_occurred, which is initially set to False. This flag keeps track of whether the collision has already happened.
  2. Check the flag during collision – Every time the game checks for a collision between the player and the enemy, it also checks whether collision_occurred is True or False.
    1. If it’s False and a collision happens, the score increments by 1, and the flag is set to True. This prevents further increments until the enemy resets.
    2. If the flag is True, no further points are added, even if the player remains in contact with the enemy.
  3. Reset the flag – Once the enemy moves off-screen and reappears at the top, the flag is reset to False, allowing for another score increment during the next collision.

p/s Score Board is being implemented this year. One of the ways to monitor students progress in class

BTE1522

DRE2213