Introduction ¶
Creating a game can be a fulfilling, frustration experience. I want to share the journey of my child building a 2D Battle Royale game
The goal was simple: he saw a YouTube video where a creator was creating a game in python using ChatGPT and wanted to try himself.
Create a fun, competitive game where two players could control characters, shoot bullets, and see who wins.
Along the way, he encountered both technical challenges and rewarding moments that made the experience great.
(I did help a bit with image and sound placement)
Video demo ¶
Getting Started ¶
Here is the full ChatGPT log of the interaction, enjoy!: https://chatgpt.com/share/6780b0fd-fe3c-8005-8b20-642b8951cfa9
- Window: The game needed to be in a window
- Player: Each player has a colored rectangle to represent their character.
- Bullet: Small red bullets that fire in the direction the player is facing.
Key Features of the Game ¶
Player Movement and Shooting ¶
Each player can move using their respective control schemes:
- Player 1:
WASD
to move andSPACE
to shoot. - Player 2: Arrow keys to move and
ENTER
to shoot.
GPT added a cooldown period for shooting to prevent spam, ensuring strategic gameplay.
Health Bars ¶
A visual representation of player health appears above each character, dynamically updating when they take damage.
Directional Shooting ¶
One of the things GPT needed to improve was implementing directional shooting.
The solution it came up with was to have bullets move in the direction the player was facing when they fired:
up
,down
,left
, orright
By using pygame.math.Vector2
, it achieved smooth directional control.
Sound Effects ¶
To make the experience more immersive, I included a sound effect when shooting. The sound file shot.wav
plays every time a bullet is fired. If the file couldn’t be loaded, the game gracefully handles it by skipping the sound rather than crashing.
This ensures that even if the sound file is missing, the gameplay continues without interruptions.
Challenges and Solutions ¶
1. Bullet Direction Bug ¶
Initially, bullets would always shoot upward regardless of player direction. This was resolved by assigning a direction vector to each bullet based on the player’s last movement.
2. Missing Sound File ¶
If the sound file shot.wav
wasn’t in the correct directory, pygame
threw an error. It handled this by printing an error message and disabling sound instead of crashing.
3. Collision Detection ¶
To detect when a bullet hits a player, It used the colliderect()
function provided by pygame
. It checks if the rectangular areas of the bullet and the player overlap.
The Final Result ¶
The final game turned out to be a actually quite fun and engaging battle with:
- Smooth player movement.
- Realistic directional shooting.
- Health bar updates.
- A clear victory message when one player wins.
We did a couple of rounds of 1v1 between people in the family.
What He Learned ¶
My child was asking questions about Python, and I explained how to “read code”:
- Use clues and lanugae understanding for what
try
,class
,def
move(self, keys, left, right, up, down):
etc.
The below excerpt of code was a good excercise in understanding code and meaning for a child.
def move(self, keys, left, right, up, down):
if keys[left]:
self.rect.x -= self.speed
self.direction = pygame.math.Vector2(-1, 0) # Left
if keys[right]:
self.rect.x += self.speed
self.direction = pygame.math.Vector2(1, 0) # Right
if keys[up]:
self.rect.y -= self.speed
self.direction = pygame.math.Vector2(0, -1) # Up
if keys[down]:
self.rect.y += self.speed
self.direction = pygame.math.Vector2(0, 1) # Down
Conclusion ¶
Building this Python pygame
battle royale game was great for any child to explore creativity and problem-solving. Whether you’re a seasoned developer or new to game dev, projects like this offer endless opportunities to learn and grow. If you’re considering making your own game, take the leap—you’ll be amazed at what you can create!
The code ¶
The code is available on GitHub. Download via the link below:
Review the README for instructions on how to run it.