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 –
- Define the flag – We introduced a variable
collision_occurred
, which is initially set toFalse
. This flag keeps track of whether the collision has already happened. - 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
isTrue
orFalse
.- If it’s
False
and a collision happens, the score increments by 1, and the flag is set toTrue
. This prevents further increments until the enemy resets. - If the flag is
True
, no further points are added, even if the player remains in contact with the enemy.
- If it’s
- 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
MSc – Exploring CCTO and PDMS
Journal Publication Tips
BTE1522 DRE2213 – Week 2 – Data Types and Control Statements
In Week 2 of the BTE1522 DRE2213 course, the focus was on fundamental programming concepts, specifically data types and control statements in Python. These are critical building blocks for developing logical structures within any programming language and are key to enabling students to design functional applications.
- Data Types in Python
Understanding the various data types is essential in programming. Python offers several data types that students learned to apply during the class –- Integers (
int
): Whole numbers, like 1, 2, or 100. - Floating-point numbers (
float
): Numbers with decimal points, like 3.14 or 5.0. - Strings (
str
): Text data, enclosed in quotes, like “Hello, World!”. - Booleans (
bool
): True or False values, used for conditional logic. - Lists and Tuples: Students were introduced to lists, which are mutable sequences, and tuples, which are immutable sequences, to store multiple values.
- Integers (
- Control Statements in Python
Control flow structures help in decision-making and looping through tasks. The students explored:- If, Elif, and Else Statements: Conditional statements that allow the program to execute specific blocks of code based on certain conditions.
- While and For Loops: Used to repeat a block of code while a condition holds true.
Activity 1-3: Player Creation and Movement
Students applied their understanding of data types and control statements through practical coding exercises. In Activities 1-3, they created a basic player character and programmed its movement across the game window.
The coding exercise allowed students to implement:
- Variables to define the player’s position, size, and color.
- Control statements like if-elif-else to determine how the player moves in response to keyboard input.
- Basic boundary control to prevent the player from moving off-screen.
Moving to the Second Phase – Five Challenges
After completing the initial activities, students moved to a more challenging phase involving code modifications and analysis. They were tasked with modifying and analyzing code to address five distinct challenges, each designed to deepen their understanding of data types and control statements.
Challenge 1: Modify Movement with Control Statements
Objective – Students were introduced to control statements such as if, elif, and else. They modified the code to allow the player to move based on specific conditions, such as different key presses resulting in different player actions.
Challenge 2: Boundary Control with Conditional Statements
Objective – In this challenge, students explored boundary detection using if and else statements. The goal was to prevent the player from moving outside the game window. This reinforced their understanding of how conditions can control flow in a program.
Challenge 3: Change Player’s Color with Data Types
Objective – Students were introduced to the concept of variables and data types such as lists and tuples. The challenge was to change the player’s color based on certain conditions, like the player reaching specific coordinates.
Challenge 4: Score and Time Tracking with Variables
Objective – Students explored the use of variables and loops to add a scoring system and time limit. They learned how to create a variable that increments when the player performs specific actions and how to manage game time using a while loop.
Challenge 5: Advanced Activity: Collision Detection
Objective – The final challenge involved practicing control structures and Boolean data types to detect when the player collided with the screen edges. This challenge required students to think critically about game dynamics and how to implement collision logic.
Pedagogical Approaches in the Exercise
To ensure a thorough understanding, two pedagogical approaches were used throughout the five challenges:
- Code Modification/Generation
Students in DRE2213 were required to actively modify their existing code based on the challenge descriptions. This hands-on approach allowed them to understand the logic behind each task and improve their problem-solving skills by directly interacting with the code. - Code Analysis
In BTE1522, students were provided with completed code and asked to map it to the challenge objectives. This method allowed them to break down complex code structures, understand how different components work together, and link theoretical concepts to real-world applications.
Week 2 was an essential part of the course as it introduced fundamental programming concepts like data types and control statements in Python. Through interactive activities and challenges, students not only learned to implement these concepts but also developed problem-solving skills by engaging in code modification and analysis. These exercises laid a strong foundation for the more advanced topics to come in the course.
Nurul Oct 15th
DRE2213
BTE1522