Video summary
PyGame Scrolling Shooter Game Beginner Tutorial in Python - PART 4 | Shooting Bullets
Main summary
Key takeaways
Summary
The tutorial adds shooting mechanics to a PyGame scrolling shooter. The instructor builds a reusable Bullet sprite class, lets the player fire bullets, and extends the game so bullets can collide with characters, reduce health, and trigger death animations.
Adding bullets
- Load the bullet image before the main game loop with
pygame.image.load(...).convert_alpha()to handle transparency correctly. The instructor also addsconvert_alpha()to the soldier animation images. - Create a
Bulletclass that inherits frompygame.sprite.Sprite. Each bullet stores its position (xandy), direction, shared speed value of 10, image, and a rectangle to track its position. - Create a
bullet_groupwithpygame.sprite.Group(). The group lets the game update and draw all bullets together. - Call the group’s
update()anddraw(screen)methods in the game loop. The bullet class’supdate()method moves each bullet horizontally according to its direction and speed. - Remove bullets after their rectangles move completely off either side of the screen by calling
self.kill(). This prevents them from accumulating in memory.
Letting the player shoot
- Add a
shootinput variable: set it toTruewhen Space is pressed andFalsewhen it is released. - When shooting is active, create a bullet at the player’s position and add it to the bullet group.
- Position the bullet in front of the character rather than at its center. The instructor offsets it by part of the player’s width and multiplies the offset by the player’s direction, so the bullet appears on the correct side when the player turns.
- Move bullet creation into a
shoot()method on the soldier class so both the player and enemies can use the same shooting logic.
Controlling firing rate and ammunition
- Give each soldier a
shoot_cooldownvalue. A shot is allowed only when the cooldown reaches zero. After firing, set it to 20 and count it down during updates to create a delay between shots. - Add
ammoandstart_ammovalues to each soldier. Current ammo decreases after each shot, while the starting value can be used to restore ammo later. - In this example, the player and enemy each start with 20 bullets. Characters can shoot only while they have ammo remaining.
Detecting hits and handling health
- Use PyGame’s sprite-collision function to check whether a bullet overlaps the player or an enemy. Remove bullets when they hit a living character.
- Check whether the target is alive before registering a hit. This prevents dead characters’ remaining rectangles from continuing to block bullets.
- The bullet class does not track who fired a bullet, so its collision logic applies to both player and enemy bullets. Friendly fire is possible with this setup.
- Add health and maximum-health values to soldiers. In the example, characters start with 100 health; bullets reduce player health by 5 and enemy health by 25. These values can be adjusted to change the game’s difficulty.
Adding character death
- Add a
check_alive()method that detects when health reaches zero or less. It clamps health to zero, stops the character by setting its speed to zero, marks it as not alive, and switches its action to the death animation. - Add a death animation to the animation list. The existing animation-loading loop then loads it along with the other animations.
- Update animation handling so the death animation stops on its final frame instead of looping. Other animations, such as idle, running, and jumping, continue to loop normally.
- Call the alive check from the soldier’s general
update()method. The enemy’s update method must also be called so its animation and death behavior are processed.
The tutorial’s main design lesson is to use reusable classes and sprite groups: bullets share common behavior, soldiers share shooting and health logic, and the main loop can update and draw grouped objects concisely.
Speakers and sources
- Coding With Russ — the tutorial’s instructor and sole speaker featured in the subtitles.
Rate this summary
Your feedback will help improve summaries.
Improve this summary
Reprocess with a stronger model when the summary feels incomplete or inaccurate.
Translate summary in another language
Ask questions to this video
Chat for follow-up questions, clarifications, and source-backed answers.