Pygame Tutorial - Projectile Motion (Part 1)

Tech With Tim · Intermediate ·🛠️ AI Tools & Apps ·8y ago

Key Takeaways

This video tutorial demonstrates how to simulate projectile motion using Pygame, a Python library for game development, and applies basic physics and math concepts to create a 2D animation of a golf ball shot.

Full Transcript

hey guys and welcome back to another YouTube video so in today's video I'm going to be doing a another tutorial in Python using PI game so this one is gonna be projectile motion so in one of my previous videos I showed you guys a game that I made it was like a mini golf game where it had a ball that could jump around the screen something like this right now obviously it was bouncing and it was a little bit more advanced but it ran off this basic principle of something called projectile motion now if you're not in physics or not in math projectile motion is just pretty much how a projectile moves in two dimensions at least the way that we're gonna be doing it with no air resistance and a constant gravity acceleration and no acceleration in the in the x-direction so pretty much you can see here this is what I'm gonna be showing you guys how to do today now it seems pretty simple but it's a lot more advanced than it looks and it deals with some principles I usually learn in grade 11 and grade 12 and it kind of a prerequisite for this is I'd suggest you're probably in high school and with decent math background so you can kind of understand what's going on if you don't um have a good math background or you're not good at math don't worry you can still follow along with the tutorial and just copy what I'm showing but don't stress too much about trying to understand it all if you're if you don't have great math background so this is probably gonna be a little bit of a longer video as I am gonna explain somewhat in depth how all this works and then it's probably gonna be in about two parts as well so I'm gonna try to keep this first part maybe about 10-15 minutes and then same for the second part so let's get started right away so like I mentioned we're gonna be doing this in Python so let's open up a new file and we're gonna start with our imports we're gonna need to import pie game we're gonna need to import math now we're going to create a basic pie game screen again I'm gonna go kind of fast for some of these beginner steps or these beginning steps just because it's pretty straightforward and if you guys don't know how to use PI game you might want to go watch my PI game programming tutorials first they might help out with this video so we're just gonna set up our screen here and I mean you guys can make it as wide as you want I'm just gonna set mine equal to 1200 by 500 - like that and then well we don't need to set a caption that's fine so we're gonna start by just creating a class now this class is just gonna be our object which is a ball which can be shorter on the screen I'm gonna call it ball you guys can call whatever you want and then we'll start off with our net function like this and we'll do self X Y and then radius and cover because it's gonna be a ball so we don't need a width and height Finch do self taught x equals x straight forward so if the wife is y and so on all right now we need our other function here our draw function it's gonna take two parameters here self and the window and then inside here which could do my game dot draw dot circle I think that's how you spell Circle and then for the circle if you guys don't know draw a circle pretty much it just takes window color I believe let's do self self dot color and then it takes a position this is gonna be the middle of the circle the position so self dot X and self dot Y like so and then it takes your radius so self dot radius now this is gonna seem weird but I'm gonna do minus 1 for this radius here actually so now what we're gonna do is we're just gonna copy this alright and we're gonna paste it again now the reason I'm doing this is so that we can get an outline for our circle so for this next one we're gonna do radius minus one so this means it's gonna draw one fewer so that we it looks like we have a one pixel outline and I'm just gonna set this equal to black hard-coded in here so this way we start by drawing a black circle and then on top of it we draw a white circle with one less of the radius so we see that black outline and you guys will see what I'm talking about when we're moving the ball around on the screen now we're gonna put it in a function here that we're gonna start working on a little bit later we're gonna call it ball path I mean it doesn't really matter what you call it but that's what I like to call it and we're gonna put the parameters in and then we'll we'll come back to it in a bit so just fill this in for now power angle and time and some of you guys in physics might see what we're gonna be doing with this function where should I leave it right now we're gonna make it a static static method as well sorry not function method inside here we know such a straight path okay now I know I'm whizzing through this right now but this is just because this stuff is pretty basic and this is just setting up our screen and setting up everything which I don't really need to go over in depth I hope you guys know that already so redraw window and then we'll just do window fill and I guess we'll give it like a nice gray background here I mean you can make it whatever you want and then we're gonna just do hi game dot display dot update and we'll include the rest of our stuff in there after so now let's let's get into our our main loop here and our global variables so we'll just do run equals false well run oops what am I saying Rennick was true well run and then we'll do our main event loop so for event and pygame don't event dot gets and then if event dot type equals equals pi game dot quits and you guys know what to do here we're just gonna do run equals false and that side of our loop will do my game don't quit like so all right so let's take a quick breather here for a second and go over what we've done right now so we've just created a class which is our ball object or will not object ball class and then we've created our redraw window function which we're gonna use to redraw the window so we'll put that into our main loop here I'm going to call that every frame and then we've created a just the event loop in here just to see if we quit then it's gonna quit so now what we need to do is we need to create our global variables and we're gonna have to create one called well it's gonna have to be the ball object so I'm gonna call mine golf ball you can call yours whatever you want and we're gonna set equal to a ball object I'm gonna put mine at 300 and then 494 don't ask why hunt number I'll tell you guys in a second and then the color will be 255 255 255 so fives our radius this is our Y and this our X all right so now we have our golf ball object so now we can go back into redraw window and we can just do golf ball dot draw and put win in there and we're almost ready to go here so I'll just show you if I run the program right now everything should be working you can see we have a little white golf ball and it's on the screen now obviously it's not moving around yet but that's what we've accomplished so far all right so now we're gonna go into the next step so now that we have the golf ball drawn and we've got through all the tedious stuff we now need to figure out some of the math that we're gonna be using - well propel the golf ball so we now need to figure out also when we are gonna propel the golf ball so the way that I like to do it is I let just like to have a line between my mouse and the golf ball and then that line is gonna indicate what the angle we're shooting at in the amount of power that we're using now this doesn't work perfectly because it's kind of exponential the longer the line gets exponentially the amount of power but for our purposes it works fine so I'm gonna create a variable out here actually we'll create it in here which is going to be set equal to the line we're gonna set equal to a list and we're gonna do two positions in here like this now the first position is gonna be where at vollis so we're gonna do ball art golf ball golf pulled out X and golf ball know why like that and then the next position is going to be the position of our cursor on the screen so to get that position we have chef to do pause is equal to PI game dot Mouse don't get underscored pause like so and then in here you can just simply put pause now what's gonna happen here is we're now going to create a list it's just gonna have it's gonna have that line going between the ball and our cursor and this way we can decide where we're gonna be shooting it and we can just see it better now that we have the line we need to draw the line so to draw the line we have to go back into our redraw window function we're just gonna do pygame don't draw dot line then inside here we're gonna start with our window our color which I guess we're just gonna be block in this case and then we're gonna do we need the two coordinates so we're gonna do line 0 like that and blind 1 and that should be it for the line and let's try our program again and see what we get and yes now we get a line like this and did I say to make the line black well I made it white so that's okay you guys can make whatever color you want does really matter and you can see it just kind of follows along as I move my mouse so we can determine now what angle are gonna be shooting at and the direction of or the power of that shot so now that we have that done it's time to get into some of the trickier stuff so let's start off here by setting X equal to 0 and y equal to 0 these are going to be the variables that we use to determine where our ball was shot from so once we click the mouse button which has got what we're gonna be using to shoot the ball we're gonna store the position of that bowl in these variables that we can then access them after we're also gonna need a variable called time it's gonna set this equal to 0 we're in a variable called power it's gonna be set to 0 again angle same thing 0 and then shot and this is gonna be equal to false all right we'll do shoot actually so now we need to see when we're gonna be shooting so I said if we press the mouse button you guys can make it space you can make whatever you want we can do if event or event dot mouse button down oops so this just means any mouse button can be middle mouse button left mouse button right mouse button then we're gonna shoot so we're going to shoot is equal to true now there's something here we have to do first you have say if shoot equals equals false this that we're not already shooting because if we do that then it's gonna mess things up if we're already shooting and we try to shoot again hope that makes sense when we shoot here we're now gonna have to set our X Y time power angle and this is where we get into the more complex stuff so this parts easy but just set X equal to golf ball about X and then we're gonna set our y equal to golf ball don't why so we know where we initially shot from and now we have to get our power so we'll actually we'll set time equal to 0 here - and now for our power so I mentioned that our power is gonna be the the distance of that line so if I know cell equals 0 for now if I run the program you can see that well we have an issue just because I haven't finished yet but the longer the line gets the more power we're gonna have well even was that issue mouse button down there's apparently no Oh if event dot type people didn't so I went down my bad there guys so make sure you fix that mistake it's just right here so let's run it again make sure that works mouse button down is not defined event type equals equals all right just give me a second to figure this one out ah Pegeen don't menace button down that's what we need it sorry about that all right so ho let's run hit one more time hopefully this one we get it working there we go you can see so we have the line like this we're gonna shoot with a lot less power and if we have the line like this we're gonna shoot with more power clearly so how do we do that now the first thing we need to do is we need to get the length of the line so I'm not sure if any of you guys remember from grade 9 or 10 the length of the line formula if you don't don't worry I'm gonna show it to you right now pretty much it's the square root so we need math dot square root of the change in X plus the change in Y and both of those are squared so I'm going to just start by doing this just to remind myself so we're gonna square something and add it and then we're going to swear something else now this is where our line function comes in handy our line list sorry so in our line right here we have a tuple and then we have another tuple so we need to find this minus this right as a position so let's start by doing that let we're gonna start with Y here so we'll just do line and then we're gonna do 1 1 minus line and then 0 1 like that so the reason we've done this is because our first position is the starting position of our line and where the golf ball is the second position is our mouse so if our mouse is over here then we need to pretty much subtract the the ending position by the starting position to figure out how much we changed in distance it's not really that big of a deal because our ball is only going to be ever shot from a certain Y level but if we had our ball elevated or landed on top of something then this would definitely matter all right so now the next part we need the X so we're just going to line and then in this case 1 0 to access the x- line and then 0 0 like that alright so we've got that now that's gonna be our power but the thing is if we if we do this then you're gonna notice we're gonna have power going from like say for example blanco this line is 10 and then up here it's five hundred five hundred power means we're moving out five hundred meters per second which means you won't even be able to see the ball move across the screen because it's gonna move so fast so we're gonna actually just divide all of this by eight so that we get smaller numbers for our power now the next one the angle okay so this is where we get into even more mouths so the last one with ya okay it's somewhat difficult the angle we're gonna have to use a bit of trig so I already have a function I pre wrote here

Original Description

Pygame Tutorial Projectile Motion (Part 1). In this pygame tutorial I explain how to simulate projectile-motion in python. This involves basic physics in python. In this video I show you how to simulate projectile motion in python using the module pygame. This involves the use of physics and math. I use trigonometry to calculate angles and determine velocities and distances. This is an advanced tutorial and if you have any questions i encourage you to leave them down below and I will try my best to get back to you! Please LIKE and SUBSCRIBE for more content!! KEYWORDS: Python Pygame Pygame tutorial tutorial pygame physics in python projectile motion pygame projectile motion python
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 42 of 60

1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video tutorial teaches how to simulate projectile motion using Pygame and applies basic physics and math concepts to create a 2D animation of a golf ball shot. The tutorial covers how to create a basic Pygame screen, draw a circle on the screen, and use trigonometry to calculate power and angle.

Key Takeaways
  1. Open a new file and import Pygame and math
  2. Create a basic Pygame screen with a width of 1200 by 500
  3. Define a Ball class with attributes x, y, radius, and color
  4. Create a draw function to draw a circle on the screen
  5. Paste the draw function to create an outline for the circle
  6. Redraw the window and fill it with a gray background
  7. Create a main loop with a global variable run and an event loop to check for the quit event
  8. Create a ball object with x and y coordinates and draw it on the screen
  9. Propel the golf ball using a line between the mouse and the golf ball to indicate the angle and power
💡 The tutorial demonstrates how to use Pygame to simulate projectile motion and applies basic physics and math concepts to create a 2D animation of a golf ball shot.

Related Reads

Up next
Emergent Ai Tutorial || How To Use Emergent Ai Website Builder (Full Guide)
Chris Tech Guide
Watch →