Pygame Shmup Part 3: Collisions (and Bullets!)

KidsCanCode · Beginner ·🌐 Frontend Engineering ·10y ago

Key Takeaways

Pygame is used to create a Shmup game with collision detection and bullet shooting mechanics, leveraging concepts from frontend engineering.

Full Transcript

welcome back to the pame game development series we're continuing to work on our schmu game and this time around we're going to talk about collisions which means how do you tell when one thing on the screen runs into another thing okay currently we have a player Sprite and we have a bunch of enemies running around flying around the screen in different directions and now we need to have something happen if one of those enemy red squares runs into our player and for now we're just going to say if it happens then the game is over um if you get hit you are dead so how do we do that well what we need to do is we need to look at the rectangle of the player and we need to look at the rectangle of the mob and say do those two things overlap now that can start to get complicated because you have a lot of coordinates to keep track of the left side the right side the top side the bottom of this one and of this one and then I need to go through and do it for every single one that's on the screen to see if any of them hit it and that can get uh kind of tedious and require a lot of code but fortunately pame has a nice handy uh command that we can use and we're going to do that in our update section of our game Loop what we're going to do is after we update all the Sprites we're going to check to see if uh a mob hit the player okay and how we do that is we're going to use a command called py game. sprite. Sprite Collide and the way that the Sprite Collide command works is you need to put in the parentheses you need to put what Sprite you want to check so we want to check the player Sprite then you need to put what group you want to check against so we're going to check the mobs group and remember we made this group that has all the mobs in it and this will tell us whether any of the mobs hit the player and then we're also going to put in here um you can say false or true and what that controls is whether the mob that you do hit or whichever thing in this group you hit should be deleted or not um we're going to make a game over so it doesn't really matter but we can use that later for for other things so this command when you run it will give you back a list so we're going to call this list hits so this will be a list of any of the mobs that hit the player if none of them did that list will be empty won't have anything in it um it's possible you might run into more than one mob at the same time if they're you know coming at you from two different directions but this will be a list of any of the ones that you hit so then all we have to say is if if hits so what this does is when you have a list if a list is empty that is false that doesn't have anything in it if hits has something in it then we're just going to set running equal to false so now anytime I hit get hit by a mob running will equal false the game will will end the game will be over so let's see how that works so I'm dodging oh I got hit I'm instantly dead well now that we know how to do collisions we can add something else that's going to collide on the screen and that is we need to be able to shoot so we can shoot back at these enemies and tell when the bullets hit the mobs so that we know we can delete the mobs so that means we're going to need one more Sprite okay I have my player Sprite I have my mob Sprite now I need to add a Sprite for my bullets and this is again going to be a pame Sprite this should all start to be looking pretty familiar to you um as we've done it few times now we'll start making these more and more unique as we add things to them oops and then um so for this one for this one's image I want this one to be fairly small right this is going to be just a little bullet that's going to shoot out from the front of my player so I'm going to make it 10 pixels wide and 20 pixels tall and I'm going to fill it with yellow little yellow bullet and we'll get the wreck now what about where we put it well we want this bullet to spawn right in front of the player right so if I'm if I'm over here on the right side of the screen the bullet should come out the front of my ship and if I'm over here on this side it should come out the front too right so so we need it to spawn at a different place wherever we happen to be when we hit the space bar and fired so so I'm going to add an X and A Y in here to the init function so we know whenever we spawn a bullet we'll just tell it spawn at a particular location and we'll figure that out from where the player is so we'll just set the rect do bottom okay equal to Y and we'll set the rect do Center X to X and then it'll be centered right on wherever we said to to to center it and then we need to oops and then we need to have a speed y so the bullet's going to travel upwards on the screen so that needs to be negative and we'll make it fairly fast um again we can tweak that later and decide how fast we want it to go and the update for this Sprite is going to be fairly simple the wct uh will move upwards at the speed and then we want to just kill it if it moves off um the top of the screen right you missed you didn't hit anything the bullet's just going to need to get um deleted so we can do that by just saying if the bottom of the of this Sprite bullet Sprite goes off the top of the screen then self. kill kill is a command that takes any Sprite and just deletes it removes it from any groups that it might be in um so it's just completely gone okay and so that will be our that will be our bullet um now we need a way to shoot so we we're going to need another key so we're going to do that initially to keep things simple we're just going to make it so that each time you press the space bar the player will shoot a bullet um so we're going to need to add another event in here in our event section and the event we're going to add is a key down event okay key down means a key was pressed downwards onto the keyboard um there are also key up events for when you let go and the key goes back up but we want to fire anytime you press the the space bar down so if so if there's a key down event and the key that was pressed was py game. kpace that's the name of the space bar then I'm going to say player. shoot okay now obviously we don't want to run this because if we do pressing space is going to result in an error because the player doesn't have anything called Shoot the player doesn't have a a function called Shoot defined yet so we're going to add that now now that we can spawn a bullet we will add a shoot command that spawn the bullet anytime we press space so let's say shoot and shoot is going to spawn a new bullet in this new bullet we just need to tell it where to spawn well we're going to spawn at the the X we're going to spawn at is the center X of the player so it's centered on the player and the Y remember we said to put the bottom of the bullet at some y so the Y will say is the self. re. toop of the player so put the bottom of the bullet at the top of the player okay make sure we add the bullet to the all sprite's list so that it will get drawn and and updated and then also just like we did with the mobs we're going to make a group called bullets and we're going to put this bullet in that group okay kind of for the same reason we did with the mob we needed to have a group for it to be in so that we could do the Collision okay so I'm going to scroll down here and add another group called bullets and that's a Sprite group okay so now if I run it I should be able to hit the space bar and spawn some bullets oops looks like I forgot to add yellow up here in my colors I thought I had done that already but I guess I left that one out so yellow I like yellow for bullets yellow is just 255 255 Z all right now I've got some bullets and if I tap the space bar fast I can fire a bunch right slowly I get a few okay now I still have to dodge or else I'm GNA hit one and get killed okay we're almost there now just like we checked to see if the mob hit the player we're also Al going to add right here a check to see if a bullet hit a mob okay and this one is a little different because we have a group of bullets and we have a group of mobs and any of those bullets might hit any of those mobs so we want I want a list of all those collisions so this command we're going to make hits equal to pygame Dos sprite. group collide and that command lets you Collide two different groups together right this other one Sprite Collide was Collide a Sprite with a group this one is going to take two groups so we're going to take the mob's group and the bullets group and see if any of them hit okay and then I want to put true so that any mob that gets hit gets deleted and I'm going to put true again which is for the second group any of the bullets that gets hit gets deleted so if a bullet runs into a mob both of them will get deleted okay and that will be fine although what will happen is we'll start running it is see I'm killing the bullets right or I'm killing the Mobs with the bullets but now there's no more mobs because I have deleted all of them so we need to if any mobs get destroyed make sure we spawn some more and that's easy enough because right we we have this we had eight of them originally right we put them all in the group so anytime we delete one so we're going to say for hit in hits right there might be more than one I might have killed two mobs or three so however many disappeared I'm going to make this lip Loop go through this list of hits and do do this however many times there were I'm just going to spawn a new mob real quick add it to the all Sprites uh where it's supposed to be and add it to the mobs um where it's also supposed to be okay so if I killed three mobs then this Loop will go through three times and spawn three more new ones so I'll basically always have eight of them right and now I can sort of run along and be hitting them and they keep coming and now we sort of start to have a game right I can defend myself and if I mess up and miss one it's game over okay that'll do it for this time around uh next time I think we'll be ready to replace these little red and green rectangles with some real graphics

Original Description

We have a player. We have enemies. What happens when they run into each other (and how do we tell)? We will also add the ability to shoot back! In each video in this series, we'll add another feature to the Shmup game until we have a full game experience with graphics, animations, sound, and much more! Link to the Pygame Template file: https://github.com/kidscancode/pygame_tutorials/blob/master/pygame%20template.py Support me on Patreon: http://patreon.com/kidscancode Links: * Installing Python: http://kidscancode.org/python-install.html * Installing Pygame: http://kidscancode.org/blog/2015/09/pygame_install/ * Setting up Atom: https://www.youtube.com/watch?v=uve1tjVIQ6c&ab_channel=KidsCanCode
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from KidsCanCode · KidsCanCode · 22 of 60

1 Learning to Code with Python: Lesson 1.1 - What is Programming?
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
2 Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
3 Learning to Code with Python: Lesson 1.3 - Variables
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
4 Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
5 Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
6 Learning to Code with Python: Lesson 1.6 - Functions
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
7 Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
8 Learning to Code with Python: Lesson 1.8 - Number Guessing Game
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
9 KidsCanCode - Patreon Intro Video
KidsCanCode - Patreon Intro Video
KidsCanCode
10 Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
11 Learning to Code with Python: Lesson 1.10 - Secret Codes
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
12 Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
13 Learning to Code with Python: Lesson 2.2 Simple Animation
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
14 Learning to Code with Python: Lesson 2.3: Animating More Objects
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
15 Learning to Code with Python: Lesson 2.4: More Fun with Animation
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
16 Extra: Setting up the Atom Editor for Python
Extra: Setting up the Atom Editor for Python
KidsCanCode
17 Game Development 1-1: Getting Started with Pygame
Game Development 1-1: Getting Started with Pygame
KidsCanCode
18 Game Development 1-2: Working with Sprites
Game Development 1-2: Working with Sprites
KidsCanCode
19 Game Development 1-3: More About Sprites
Game Development 1-3: More About Sprites
KidsCanCode
20 Pygame Shmup Part 1: Player Sprite and Controls
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
21 Pygame Shmup Part 2: Enemy Sprites
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
▶ Pygame Shmup Part 3: Collisions (and Bullets!)
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
23 Pygame Shmup Part 4: Adding Graphics
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
24 Pygame Shmup Part 5: Improved Collisions
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
25 Pygame Shmup Part 6: Sprite Animation
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
26 Pygame Shmup Part 7: Score (and Drawing Text)
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
27 Pygame Shmup Part 8: Sound and Music
Pygame Shmup Part 8: Sound and Music
KidsCanCode
28 Pygame Shmup Part 9: Shields
Pygame Shmup Part 9: Shields
KidsCanCode
29 Pygame Shmup Part 10: Explosions
Pygame Shmup Part 10: Explosions
KidsCanCode
30 Pygame Shmup Part 11: Player Lives
Pygame Shmup Part 11: Player Lives
KidsCanCode
31 Pygame Shmup Part 12: Powerups
Pygame Shmup Part 12: Powerups
KidsCanCode
32 Pygame Shmup Part 13: Powerups (part 2)
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
33 Pygame Shmup Part 14: Game Over Screen
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
34 Pygame Platformer Part 1: Setting Up
Pygame Platformer Part 1: Setting Up
KidsCanCode
35 Pygame Platformer Part 2: Player Movement
Pygame Platformer Part 2: Player Movement
KidsCanCode
36 Pygame Platformer Part 3: Gravity and Platforms
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
37 Pygame Platformer Part 4: Jumping
Pygame Platformer Part 4: Jumping
KidsCanCode
38 Pygame Platformer Part 5: Scrolling the Window
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
39 Pygame Platformer Part 6: Game Over
Pygame Platformer Part 6: Game Over
KidsCanCode
40 Pygame Platformer Part 7: Splash & End Screens
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
41 Pygame Platformer Part 8: Saving High Score
Pygame Platformer Part 8: Saving High Score
KidsCanCode
42 Pygame Platformer Part 9: Using Spritesheets
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
43 Pygame Platformer Part 10: Character Animation (part 1)
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
44 Pygame Platformer Part 11: Character Animation (part 2)
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
45 Pygame Platformer Part 12: Platform Graphics
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
46 Pygame Platformer Part 13: Improved Jumping
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
47 Pygame Platformer Part 14: Sound and Music
Pygame Platformer Part 14: Sound and Music
KidsCanCode
48 Pygame Platformer Part 15: Powerups
Pygame Platformer Part 15: Powerups
KidsCanCode
49 Pygame Platformer Part 16: Enemies
Pygame Platformer Part 16: Enemies
KidsCanCode
50 Pygame Platformer Part 17: Using Collision Masks
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
51 Pygame Platformer Part 18: Scrolling Background
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
52 Pygame Platformer Part 19: Wrapping Up
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
53 Gamedev In-depth Topics: 4-way vs. 8-way Movement
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
54 Gamedev In-depth Topics: Time-based vs. Frame-based Movement
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
55 Gamedev In-depth Topics: Non-integer Movement
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
56 Tile-based game Part 1: Setting up
Tile-based game Part 1: Setting up
KidsCanCode
57 Tile-based game Part 2: Collisions and Tilemap
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
58 Tile-based game Part 3: Smooth Movement
Tile-based game Part 3: Smooth Movement
KidsCanCode
59 Tile-based game Part 4: Scrolling Map / Camera
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
60 Tile-based game Part 5: Player Graphics
Tile-based game Part 5: Player Graphics
KidsCanCode

This video teaches how to add collision detection and bullet shooting mechanics to a Shmup game using Pygame, covering key concepts in frontend engineering. By following along, viewers can learn how to create interactive game elements and design engaging game interfaces. The video provides a comprehensive overview of the game development process, from setting up the game environment to implementing core gameplay mechanics.

Key Takeaways
  1. Set up the Pygame environment
  2. Create a player object
  3. Create enemy objects
  4. Implement collision detection
  5. Add bullet shooting mechanics
  6. Test and refine the game
💡 Collision detection is a crucial aspect of game development, and Pygame provides an efficient way to implement it. By using Pygame's built-in functions, developers can create engaging and interactive game experiences.

Related Reads

Up next
Sports on the Go: Africa makes history at 2026 FIFA World Cup
CGTN Africa
Watch →