Pygame Tutorial #5 - Projectiles
Key Takeaways
This video tutorial demonstrates how to create projectiles in Pygame using Python, covering the implementation of shooting mechanics from a character in a game development context.
Full Transcript
hey guys and welcome back to another YouTube video so I apologize as I've been pretty late getting up this tutorial I've been doing a lot of school work and making other videos so this hasn't been my top priority I also have been working on this for about two or three days trying to make sure that it's perfect and that I'm know where exactly where I'm gonna go with this series in terms of the future so I'm not going to make a video that's useless to say so in this tutorial what we're gonna be doing is we're gonna be doing some I called projectiles so we have our little character here right he walks around the screen I can jump up and down and that's great but we need to add some more to this game so what we're gonna be adding is projectiles so pretty much that means we're gonna change the key to jump to be up arrow key and then we're gonna make it so that when you click the spacebar he shoots bullets out of him now these bullets we're not gonna have a fancy animation anything cut for him ik coming out of his hands that's for another video that's a little bit more advanced but we are just gonna have hit bullets kind of coming out of his body and then we're gonna have them if they collide with the wall they're gonna disappear now this sounds pretty simple but I assure you it is not and there's a lot of things we have to take into account with this tutorial so let's just start with some of the issues we're gonna have with this so the way that we've coded this so far is if we'd run it again you can see when he walks and he stops he faces us now what if we were to shoot a bullet now I'm trying to shoot a bullet what direction is gonna is it gonna go well we don't know that because he's facing us right so we can't come out of the screen it has to either go left or right so we now need to make it so that when he stops he no longer looks at us he just stops and stays facing the way that he was going so we know when the bullets going left or when it's going right now we also have to address the problem what if we have multiple bullets so if we have multiple bullets we need to have a velocity for those bullets each bullet needs to have its own velocity so we know if that bullet is traveling left or if it's traveling right because for example if our character's looking left he shoots a bullet then he looks right and he shoots a bullet the other bullet needs to continue even left well the next bullet is moving right and you'll see what I mean as we get into the tutorial so the first thing we're gonna do because now we are writing our game and we're using objects and classes is we're going to a new class and this one is just gonna call he called or she let's call it mm-hm let's see projectile like that and then inside here you can just write objects you don't have to do that and then we write our knit function like so now inside here we're gonna give it the same characteristics as our players we're gonna give it X Y and that actually instead of width and height since this bullet is gonna be circular we're gonna give X Y radius color and then we're gonna do something called facing now what this is gonna do is gonna tell us whether the bullets moving left or moving right and that is extremely important so we have that next thing we're just gonna scroll down here we induce again it's kind of tedious but self-taught x equals x self dot y equals y and so on software radius equals radius self duck call their equals color and self tell facing equals facing now we're also gonna add one more in here that's gonna call itself the velocity or bell you can set this equal to whatever you want I'm gonna give it a positive value of eight for right now and this is just gonna be equal to 8 times facing and facing is either gonna be one or negative one so that's gonna determine whether it's moving left or whether it's moving right we're gonna give another method here we're just gonna call it draw I'm gonna put in the window and then we're just something you're just gonna draw a circle and pygame which is the following line pie game draw dot circle and then window call there so it's one me self that color we need our tuples so self that x self don't why I cannot type and then self theories and that's how you draw a circle in pining now if you didn't want your circle be filled in you would just do that comma one and that would make it so your circle is simply the outline of a circle so I'm actually filled in but we're gonna have our filled in for today like that and that's all we need for that class pretty straightforward and now we move into the more complex stuff so the first issue we're gonna have to address is our character moving so we need to build a track what direction he's facing right so if you doing left if he's moving right and what direction when he stationary that he's facing so I've already done this on another script so I'm just gonna kind of copy off what I've done here and explain to you guys as we go so here what we end up doing is we say well if he's not moving left if he's not moving right he must be standing still so we're just gonna blitt the image for him standing still no we don't want to do that we want to keep track of the last position where he is so either he was right or either he was left and then in not instance just draw him either looking left or looking right so to do this you know a little else statement here we're just gonna go we're gonna write if self thought right and then don't worry you guys are gonna think this isn't gonna work but I'm gonna change something in a second so just follow along I'm do walk write equals zero so this is just the first image pretty much of the walk right kind of animation we have so just like it looks like he's a he's looking in the right direction and that's all we need and then self though why and then else because if he's not right he must be been left because we're not gonna have a stationary position anymore we're just gonna do wind up lit we're gonna do walk left zero because again the first image and then the same thing so self the X and self thought why like that alright now we also need to add one more thing to our character so another attribute here we're just gonna do self thought standing and this is what's gonna track if our character is standing still or not now I know I'm going kind of fast here guys just because I want to get through some of this stuff and we've already gone over kind of how classes work and how some of the easier stuff works so I'm gonna go fast through this part and I'm gonna explain the more in-depth stuff yeah pretty much so now inside of our draw function we need to add something here so we need to see whether he's fate or whether he's standing still or whether he's moving now the way we did that before was by simply saying well if he's not moving he must be standing still but that would eliminate if he was looking left or right we wouldn't know in that instance cuz we're just drawing something that it's making him look straight at us either if he stopped left or if he stopped right so here we care what actually facing but we still need to know if he is standing still or not so we're gonna set this variable here self-thought standing equal to true or this attribute whatever you want to call it inside of the initial initializer and then up here we're gonna do if not standing and we'll make sure we add that self there so we don't get crash and what this is gonna do is it's just gonna check to make sure that well he's not standing pretty straightforward just tab this in here or format indent region if that seems not to work for you and then pretty much what's gonna happen now is if he's not standing still meeting he's moving we're gonna walk we're gonna run through our animation of him walking to the right or walking to the left and then if he is standing still we're gonna either blit him looking to the right or looking to the left and I hope I hope that makes sense so now we need to again go down into our main loop here and we need to change a few things so in our key dot left here we're gonna do man standing equals false because this means that he is indeed walking same thing here man duck standing equals false and then in this little else statement here we need to change a few things so we want to get rid of resetting knees because if we reset these we're not gonna know what way he was looking previously and we're just gonna do man dot standing is equal to true like so so this way we're gonna know if he's moving loud or if he's looking left if he's looking right so let's just test this out quickly make sure everything's working and we have a slight issue oh it's because I have a comma instead of a dot that's great okay so let's go back up there into window Blitz where's a comma there we go slight typo and here we go so that we can see when he stops he is still looking the way that he's trying to go like that so yeah that's all good now we can go to the projectile section so now that we've got our character he's walking properly he's facing the correct direction we have to make sure that he can shoot the bullets properly so we've already set up this projectile class here and what we have to do here and this is useful if you guys want to learn how to draw multiple objects at the same time or how to keep track of different objects on the screen so even if you're not that interested in including a projectile on your game I do recommend you do this so what we're gonna do now is we're going to introduce a new for loop into our wild and we're gonna create a new list so the first thing we need to do is actually create your list I'm going to call this bullets you're gonna set it to a blank list and this is what's gonna contain all of our bullets so we're we are gonna have multiple bullets if I only made it so what you have one bullet at a time that'd be much easier but I want to give you guys a bit of a challenge so we're gonna do this so we're gonna do four bullets and bullets like this so for every bullet that we have inside of that list and we're just gonna say if bullet is less than 500 so actually let's do bullet dot X because we already know that our bullet is gonna have a property of X because we're just gonna create some projectile objects in this in this list which I'm going to do later but I want to do this right now if bullet dot X is less than 500 meaning that we are not going to be going off the screen and bullet dot X is greater than 0 then we're gonna allow our bullet to be shot we're gonna ignore these slight pixels that might happen in terms like the width and stuff the reading with the character because it's so small the radius we're gonna be using that doesn't really matter that much and then we're gonna say pretty much move our bullet so we're gonna say bullet don't X plus equal bullet don't why all right pull it not not y thought bell so now this means that our bullet is gonna move in whatever direction we have it set to move in so maybe negative 8 or positive 8 and I'll show you how we're gonna set that direction again down below I'm kind of going in a reverse order that's fine just just try to follow along so now if our bullet is not less than 500 and it is not greater than 0 it must be off the screen so to fix this we don't want to have our bullet just floating on the edge of the screen over here we're gonna delete that bullet so we're gonna do is kind of a weird way to delete it but just follow along bullets start pop all right so this is the list pop just means remove an element and then we're gonna index so we're gonna do bullets dot index and then bullet so this is gonna find the index of the bullet in the list and then remove it from work wherever location that it's in pretty straightforward so now we're checking all these bullets but we need to shoot the bullet so again I mentioned that we're just gonna make it so that now to jump we're gonna use the up key so instead of having space we're gonna change that to up and then we're gonna add a new if statement in here now it's important that this is an if and not a and Elif if we were to add an elephant here if you're trying to move right or left it's not going to allow you to shoot as well so we're just gonna do a nice new if up here we knew it keys and then we're following the same thing we've done before pygame dot k underscore space like so and then we're gonna do down here if you Len of bullets so the length of our list bullets is less than and you guys can pick a number this is how many have many bullets you want to have on the screen at once I'm gonna do five I feel like that's probably good just to make sure my character maybe isn't spamming bullets all over the place is less than five then I'm gonna create a new bullet object now once you create this bullet object we're gonna draw the boat object and that's gonna be the next step but we just first need to create it like we created the man object we're gonna create a bold object so we're just gonna do bullets which is the list that we're gonna contain the object in dot a pen means adding to the end and then we're gonna create a new project uh like so now our projectile we have to remember what what we need to protect those who need an x and y so that's this x and y is gonna be the man dot x plus the man dot with integer division too and the reason we do this is just to make sure that our bullet is not coming for from the left side of the man or the right side the men it's coming from the middle of the man and the reason I'm rounding this is because I don't want to get a decimal number as that's gonna mess up when we're drawing the the object onto the screen now we're going to use our Y so same thing here so let's start with around this time a new man del y plus man dot height and then again integer division 2 so we have our X and our Y and now we need our what was it again radius or color so let's go up and check quickly cuz I forget ductile radius color and then facing so our radius I'm gonna set this to six you guys can set that to whatever you like and then after a radius we have our color so I'm gonna set this to black again feel free to change that to whatever you like and now our facing so now this is where we're gonna come out of the loop for a second and we're gonna do something that I forgot to do here it's gonna say if man don't left then we're gonna have our facing as negative one like that and then else facing equals zero all right not to you one like that then simply in here we can just put facing and we're ready to go now the reason I did this here is because if we are looking left then we want to make sure that our bullet is moving left now to move left we have to move negative direction because if you remember in PI game our X starts at zero here and it goes to 500 on our screen this way to the right so if we're here and we want to move backwards we have to subtract pretty straightforward and yeah so that's gonna allow us to shoot our bullets now now the thing is right now all our bullets are gonna work all the collisions gonna work with the walls but what about drawing the bullets we're not gonna see them right now so we have to make sure that we draw them so inside of our redraw game window function here where we draw everything remember I told you guys we're not gonna be drawing it down here we're gonna be drawing it up in the redraw game window function we're gonna do another for loop so you can do for again pull it in bullets like that we're just gonna do bullet dot draw easy enough now let's test the program and we have an issue self thought colors up that exile thought why self that radius named self is not defined now what issue could we have here oh and it's because I forgot to put itself there so again just go ahead and fix this I forgot to put self in the draw thing again easily avoidable errors but I just forget to fix them and while we're at it let's go to the draw function here that is the draw method and just let's add win in the brackets there sorry about that guys and we'll run the program and you see we have a guy and he shoots bullets like this and you can see they travel across the screen and again if I shoot them too right you'll see that they hit the wall and then they disappear like so and again if I try to shoot too many at once you hear me hitting my spacebar it's not gonna let me continue to shoot them until five of them until we have room in the list pretty much so again if I go here I'm spamming my spacebar right now it's not working until those other bullets get deleted from the list now you obviously are probably noticing the fact that I'm shooting multiple bullets at once when I hit the spacebar now this is not ideal this is not what we want and in the next video I'm gonna go over to how to fix this cuz I'm already at 60 minutes so I don't want to push this too much further but pretty much this is a pretty easy fix and in the next video we're gonna be incorporating enemies into our game and allowing these bullets if they hit the enemies to make them disappear to make them lose health and so on so again I apologize guys for this video coming out pretty late but I was working pretty hard on trying to get this and focus on what I really wanted to do with the series and again I've been working on other videos as well so if you guys like the video and you want to see the next one please leave a like and subscribe to the channel to help me out and I will see you again in another video [Music]
Original Description
In this video i explain how to shoot projectiles from your character in pygame using python. This series is about game development in python using the module pygame. It is geared towards intermediate python programmers and is intended to give you some ideas on how you can use pygame to create awesome games.
Download Code: https://techwithtim.net/tutorials/game-development-with-python/pygame-tutorial/projectiles/
To Download Images and Assets: https://techwithtim.net/tutorials/game-development-with-python/pygame-tutorial/animation/
Please LIKE and SUBSCRIBE for more content!
Playlist
Uploads from Tech With Tim · Tech With Tim · 40 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
▶
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
🎓
Tutor Explanation
DeepCamp AI