Interested in what its like to be a computer science student? Check out my Podcast: anchor.fm/tech-with-tim Follow me on Twitter: twitter.com/TechWithTimm Join my Discord to Ask Questions and get help with pygame! discord.gg/pr2k55t SOURCE CODE: techwithtim.net/tutorials/game-development-with-python/pygame-tutorial/jumping/
Hey!! How can we make it bounce after colliding with a boundary on any of the four edges depending on the angle with which it is incident on any surface? I tweaked the code to make it like a SNAKElike set up wherein if you take the box to the top right-hand corner, it would pop up out of the bottom left hand corner. in fact that was what I thought when you said about this in the last tutorial. PLEASE REPLY.
Can you please tell me why when i code a pygame code in IDLE then it shows an error that pygame has no attribute called init. I can make it run by writing the code in ATOM and then i double click on the saved .py file then it creates a folder with a compiled python file which can run just like normal scripts. It is very difficult as if i make a mistake in the code the program does not tell me where the error is and it just closes. I have to go through the entire code to check what is wrong(usually capitalization mistakes) it would be nice if i could just do it from IDLE. Please help because after watching your videos I am really excited about pygame and can't make big projects without this problem being fixed.
@Respact King ... I was having the same issue, until I re-watched and video and retyped the code out. I mispelled the word jumpCount. But here's the working code: import pygame pygame.init() win = pygame.display.set_mode((500,500)) pygame.display.set_caption("First Game") x = 50 y = 425 width = 40 height = 60 vel = 5 isJump = False jumpCount = 10 run = True while run: pygame.time.delay(50) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False
keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and x > vel: x -=vel if keys[pygame.K_RIGHT] and x < 500 - width - vel: x += vel if not(isJump): if keys[pygame.K_UP] and y > vel: y -= vel if keys[pygame.K_DOWN] and y < 500 - height - vel: y += vel if keys[pygame.K_SPACE]: isJump = True else: if jumpCount >= -10: neg = 1 if jumpCount < 0: neg = -1 y -= (jumpCount ** 2) * 0.5 * neg jumpCount -=1 else: isJump = False jumpCount = 10 win.fill((0,0,0)) pygame.draw.rect(win, (255,0,0), (x, y, width, height)) pygame.display.update() pygame.quit()
hey, i got a question every time i run your games in this step, it says: "DeprecationWarning: an integer is required (got type float). Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python. pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))" what is this? and how can a avoid this?
For the boundaries, I just created "_WIDTH" and "_HEIGHT" for the main windows dimensions (both are still 500), then I just did the following: K_LEFT: if x >= 1 x -= vel K_RIGHT: if x < _WIDTH - width x += vel K_UP: if y >= 1 y -= vel K_DOWN: if y < _HEIGHT - height y += vel This allows you to get right up to the edge of the window, without the 5px gap
I believe that’s going to cut off the right side of your rectangle by 1-4 pixels. It will almost look identical, but slightly go over. Same thing with down except cuts off the bottom by 1-4 pixels.
I found that jumping equations work better with linear equations. This is because the derivative of a quadratic equation is a linear equation, so while you want the shape of your jump to be quadratic, the rate at which you change the y value per frame is linear. An example would be y -= 30 - (2 * jumpCount). Great video!
I tried to use both, but the quadratic equation is much better at simulating a real jump. Because the values rise faster in the beginning and slower near the end. This is what you would see during a normal jump. Your equation wouldn't do that. There no "gravity" is implemented where you start slowing down near the end of the jump. I like not having a uniform speed during a jump animation.
0:33 I freaked out with that "Beep-beep" 1:33 I freaked out with that "Beep-beep", again. Wait! That was a one minute 0 second difference! 2:31 Why is this saying "Powering off"?!
11:23 The correct formula should be y -= a * jumpCount. This is because the *change* in y at each step should be the *derivative* of the parabolic formula
I think it's simpler to just store the initial y position and then add height to that by means of a parabola. No negative checks required. I also added something to the jumpCount variable to prevent the block from jumping off the top of the screen. Now it does a little jump, bumps the boundary and comes down at the appropriate speed. Check it out: else: if jumpCount == -10: ytemp = y a = jumpHeight/-100 if jumpCount
If anyone doesn't get the jump thing, I'll explain it: First remember that if jumpCount >= 10 then neg (which is like gravity) is equal to 1. This means that your character will go up as jumpCount squared * 0.5 is going to be * by 1. But if jumpCount is < 0 (or if jumpCount is a negative number) then neg is equal to 1, so jumpCount squared * 0.5 will be * by *negative* 1, so y will be minused by a negative number, so y will add it because a positive minus a negative number (p - -d) be the same as adding the number (p - -d is the same as p + d). Therefore, your y value will go down, because the lower on the window you go, the higher your 'y' value will be. Then, jumpCount is being minused by 1 everytime the loop will loop around, so your character goes up but at a decreasing rate. Then, when jumpCount reaches 0, like the peak of a graph, neg becomes -1 and you start going down. Then, when jumpCount becomes -11, it is < 10, so the else statement plays, isjump is false, and the jumping stops. If this explanation was crap for you, tell me
For anyone having issues with jumping/choppiness/cube still going off screen, I switched my code over to Mu(an interpreter) from pycharm and it's working as intended. Not sure why, but maybe try that because a lot of you are having the same issue as I was.
For anyone having the problems in 2023. If you write this line before the loop: clock = pygame.time.Clock() and in the main loop write this instead of the pygame.time.delay(100): while run: clock.time(60) you set the framerate to 60 fps instead of the fixed pygame.time.delay(100). The Clock controls the frame rate and ensures consistent frame rate over the game loop for you.
Very nice tutorial. I'm currently remaking my pygame framework handler and needed a brushup on how pygame worked. It basicly makes this relativly low end programming integrated so you can just create an object and the framework will give you easy event handling like key presses, mouse movement, point and click buttons, movement to objects, simple physics and not to mention the main loop is already set up. All you gotta do is running the FRAMEWORK.tick() each frame and the rest is easy! Very handy for doing simple testing and fun projects that require a quick UI. I'll definently be watching on!
Why when you make borders of the screen you use vel variable? Why not make screen pixel positioning instead of vel: if keys[pygame.K_LEFT] and x > 0: x -= vel if keys[pygame.K_RIGHT] and x < (screen_x - width): x += vel if keys[pygame.K_UP] and y > 0: y -= vel if keys[pygame.K_DOWN] and y < (screen_y - height): y += vel
if you make a screen_width var and a screen_height var and use that when you get to subtracting height and width in K_RIGHT and K_DOWN, you'll be able to update your code easier by only changing the value of those vars once and still have the capability to not get off the screen. Sorry if that was complicated, I just wanted to h a l p
If the fact that the character is not touching the border because of your velocity is bothering you (like it bothered me), here: # height and width is for the character, screenHeight and screenWidth is for the window if keys[pygame.K_LEFT] and x - vel >= 0: x -= vel elif keys[pygame.K_LEFT]: x = 0 if keys[pygame.K_RIGHT] and x + width + vel = 0: y -= vel elif keys[pygame.K_UP]: y = 0 if keys[pygame.K_DOWN] and y + height + vel
First of all: Good tutorial that definitely convinced me to subscribe to this channel. Second of all: 1. Can you tell me what was that "Powering Off" sound? It *almost* scared the h#ll outta me. 2. Also tell me why would ANYONE bother jumping if he can move freely up and down? Like wtf And third of all: There is no third of all but making a list of only 2 notes makes me feel like it's incomplete so I added this. Thanks for the gr8 tutorial!
1. Headset sitting on my desk 2. It’s just for the purpose of learning because in most games you have one or the other and I wanted people that aren’t following the tutorial completely to be able to create their own games with the knowledge of how to jump.
You can jump with a mathematical function is what I believe I do in this series or you can jump with a list of preset values to move each frame. Have to watch my side-scroller tutorial to understand that more in depth.
Ahmed Mokhles also you can use jumping for example if you decide to make a “floor” then you remove y axis controls so you can only go left or right like an adventure game which will allow creating obstacles for example rectangles on the “floor” that will trigger end_event aka you lose like the dinosaur game on google with no connection although not useful for his game very useful for others
8:35 just a correction to the physics During a standard jump, the acceleration is constant. It is always 9.8 m/s2 in the downwards direction. It is the velocity that changes and becomes zero at the top of the jump. I hope it helps ;)
If everything in the universe depends on everything else in a fundamental way, it might be impossible to get close to a full solution by investigating parts of the problem in isolation ! Tim> Hope that make sense
There's a much simpler way to do boundaries. Just check if x respectively y is > 0 or < 500 before changing its position. Although you have to take into consideration the rectangles size so it doesn't move halfway outside or something similiar
I got the same kind of Boundary bevore i watched this vid :D.btw with this tutorial my guy keeps jumping do you know what i could have done wrong?it never gets to the else statement and instantly rejumps for some reason
@@fazefijiii6932maybe you have put the else in the wrong indentation or messed up the indentation somehow, I need to see the code to know what maybe going wrong, if u can, pls share the code, there r some sites which allow sharing of code
Great tutorial! one note tho, no need for "if keys[pygame.K_LEFT] and x > vel" , just do x > 0 , the vel is speed related and the greater you'll make the rectangle move the bigger the gap will be from it to the screen.
Thank you very much for this tutorial, its great. But I have a problem while doing this script. when I press SPACE my rect doesn't keep jumping! I mean I have to hold the SPACE to see its complete jump. I exactly wrote the same codes as you did but I don't know where is the problem. please help me to fix it. thanks a lot.
I had the same! Fortunaly i found a way to fix it. Replace the "else:" from if not(isJump), with "if isJump:" and after that remove 1 tab from every line!
thanks so much i was looking up so many tutorials of how to make a palyer and they were too complicated for me to add more onto them until i found this
if you want to increase the speed without breaking the boundaries, you need to create another variable and use it instead of vel. Like this: screenvel = 5 if keys[pygame.K_a] and x > screenvel:
you dont need to subtract the velocity when you are stopping player right at the movement but you need it when you are checking the player position and repositioning it to its expected position
another way to do the boundaries check, is by setting X and Y to a certain coodinate if they reachs a certain point in the screen. for Example, i did: if x > 450: x = 450 if y > 430: y = 430 if x < 10: x = 10 if y < 10: y = 10
My earlier comment was the solution, check the indents because it might be in the wrong loop,in other words just check the number of spaces there is ment to be behind each sentence of code. For example: While True: Print(number) Is different from While True: Print(number) So just remember becareful of the spaces
Ah yes! Classic arrow key controls are one of the first things I ever implement on a game. I always have a habit of getting a tad fancier with it. Suppose the space between the player x and the edge of the screen are less than the velocity but still greater than 0. We have a remainder in there so it is good to use a modulus function and abs to compare the distances. if keys[pygame.K_LEFT]: if x > 0: x -= vel elif abs(x - screen_width) % vel != 0: x -= (abs(x - screen_width) % vel) if keys[pygame.K_RIGHT]: if x < screen_width - width: x += vel elif x % vel != 0: x += (x % vel) if keys[pygame.K_UP]: if y > 0: y -= vel elif abs(y - screen_height) % vel != 0: y -= abs(y - screen_height) % vel if keys[pygame.K_DOWN]: if y < screen_height - height: y += vel elif y % vel != 0: y += (y % vel) I'm sure it can be refactored but that is how I did it. ^_^
Is this 100% correct? Because suppose that x = 1 and vel = 5, now if I press LEFT, the reactangle will go off-screen because you only checked "if x > 0" and not "if x > vel". Similar with the other directions.
Quick small tip which is probaly obvious but if you change the width and height to 1920 by 1080 and tweak some other lines of codes you will be able to "play" your "game" in full screen.
Yep. Was getting very mad at his explanation of a parabola. "you get faster as you jump up higher" - no you don't "you have, like, a hang time" - mmm, I mean it's instantaneous "[at the top] a position with zero acceleration" - nope, not that either, it has zero (y-component of) velocity *sigh*
Thanks again Tim for this video, wouldn't it be right to not use "- vel" when preventing the rectangle to move out of the screen (down and right) Refer to 6:30
that space is due to velocity....... while ending at the screen boundary....... can be corrected as: if keys[pygame.K_LEFT] and x > 0: x = x-velocity if keys[pygame.K_RIGHT] and x < (300 - rect_width): x = x + velocity if keys[pygame.K_UP] and y > 0: y -= velocity if keys[pygame.K_DOWN] and y < (200 - rect_height): y += velocity
Could someone clarify to me why do we use the neg variable at 13:30. If i am not wrong, y will go up and then go back because of the negative jumpCount? I even did the math with a calculator and was correct but the program shows otherwise.
oh shiiit you made writting boundaries *so simple* ! I remember when i first wrote my boundaries and it was like 20 (or more) lines of code, and i was reaaally happy that i diud that, because i found it pretty difficult .
instead of the quadratic you could have used an odd exponent to produce the same effect in the jump say like 3 and then multiply it by 0.1 or something like that
pygame works very slowly on mac presumably due to resolution conflics, if anyone is having issues when you set the display options do the following: pygame.display.set_mode((0, 0), pygame.FULLSCREEN) note: you won't be able to hit the x icon to exit the program, hold shift + ctr + opt + esc for 1-3 seconds put a key command along with the player controls to exit easily
i wrote this code but my character is jumping on the first click but it is not working if i click spacebar for the second time or more. how can i fix this problem ?
I'm Sorry that this is such a late comment, I just found this tutorial a while ago, so: Do you have any videos on the formula you used for jumping? If so, Please leave the link down below. Thanks!
Hey, I noticed that the rectangle moves smooth in the video, but the rectangle in my code moves in noticable increments... and not very smooth... Did anyone else have this problem??
If anybody is still wondering how to solve this, setting " pygame.time.delay( ) " to 34 will make the program MUCH smoother(you might still have it at 100). Apparently setting it to 34 will give you 30 fps, but you can just delete the line and it doent make a difference.
Hello Tim! Love your tutorial. Btw I want to ask about what if I want to give time to 'recharge' the jump? Like regeneration time. What is the function? Thank you very much. Have a nice day! Edit: Words
Rather than using neg as a positive or negative variable, why not use abs(). Absolute function will return the absolute value of the answer that is always positive
Thanks for this pygame code Tim. It is quite complicated especially the else and indent part got to get it correct or the window will not show. But at last get the jump part working after examining how the code works.
A simpler way to do it is by having a "SpeedY" variable and suybtracting it by 1 every frame, and changing the Y position by SpeedY. Jumping would just be setting the SpeedY variable to something positive, and speed y can be set to 0 on vertical collisions to not move any further. However, I never tried it with python, so I'm not sure if it works well here
There's a bug. Set the velocity really high, see what happens? The problem is that if the game calculates you are close to the border it just stops moving, the way to fix this is instead of stating it like this if keys[pygame.K_RIGHT] and x < screenWidth - width - vel: x += vel state it like this if keys[pygame.K_RIGHT]: x += vel if x > screenWidth - width: x = screenWidth - width because now, instead of detecting that an extra full velocity move would go over and just stop moving, you just clamp the amount it moves I am not here to mock you or anything, I am just kind of a code geek, I know 4 other languages and have Unity experience, so I have a good eye for bugs. I really love these videos because they give me knowledge about how the pygame namespace works in programming a game. PS I haven't watched the rest of the series yet so if you fixed this issue already I don't know and I'm sorry if you did and I commented this.
I wanted to ask if we could do a simple if statement where we check if the x and y positions are greater than or less than the screen dimensions. For example: if bird_x > 1920 or bird_x < 0 or bird_y > 1000 or bird_y < 0: bird_x = 100 bird_y = 500 This basically just resets the sprite back to its original position
5 років тому+5
Wasn't really doing much youtube at the time 0:32 youtube yells at you after you said that.
i solved the black bar problem by subtracting the rectangle's width from the screen width and did the same thing with height, here is that code: keys = pygame.key.get_pressed() if keys [pygame.K_LEFT] and x > vel-5: x -= vel if keys[pygame.K_RIGHT] and x < screen_width - char_width: x += vel if keys[pygame.K_UP] and y > vel-5: y -= vel if keys[pygame.K_DOWN] and y < screen_height - char_height: y += vel
Interested in what its like to be a computer science student? Check out my Podcast: anchor.fm/tech-with-tim
Follow me on Twitter: twitter.com/TechWithTimm
Join my Discord to Ask Questions and get help with pygame! discord.gg/pr2k55t
SOURCE CODE: techwithtim.net/tutorials/game-development-with-python/pygame-tutorial/jumping/
Hey!!
How can we make it bounce after colliding with a boundary on any of the four edges depending on the angle with which it is incident on any surface?
I tweaked the code to make it like a SNAKElike set up wherein if you take the box to the top right-hand corner, it would pop up out of the bottom left hand corner. in fact that was what I thought when you said about this in the last tutorial.
PLEASE REPLY.
Can you please tell me why when i code a pygame code in IDLE then it shows an error that pygame has no attribute called init.
I can make it run by writing the code in ATOM and then i double click on the saved .py file then it creates a folder with a compiled python file which can run just like normal scripts.
It is very difficult as if i make a mistake in the code the program does not tell me where the error is and it just closes. I have to go through the entire code to check what is wrong(usually capitalization mistakes) it would be nice if i could just do it from IDLE.
Please help because after watching your videos I am really excited about pygame and can't make big projects without this problem being fixed.
@Respact King ... I was having the same issue, until I re-watched and video and retyped the code out. I mispelled the word jumpCount. But here's the working code:
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")
x = 50
y = 425
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
run = True
while run:
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -=vel
if keys[pygame.K_RIGHT] and x < 500 - width - vel:
x += vel
if not(isJump):
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN] and y < 500 - height - vel:
y += vel
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -=1
else:
isJump = False
jumpCount = 10
win.fill((0,0,0))
pygame.draw.rect(win, (255,0,0), (x, y, width, height))
pygame.display.update()
pygame.quit()
Bro my code requires that i press and hold space to jump or else it jumps half way
hey, i got a question
every time i run your games in this step, it says: "DeprecationWarning: an integer is required (got type float). Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))"
what is this? and how can a avoid this?
Bro I thought I was being hacked when it said powering off
same I was gonna check my processes make sure I'm not being hacked
same, i taped my cam and mic
same
Same
it scared the heck out of me
Tim: I’m sorry it took so long for me to upload the second video
Me: Laughs in 2023
same hahaha
me too lmao
lol same
Not me. I am outraged or something
Yo did u come into any problems with the jump mechanic? When I jump i fall through the floor
2:30 i had to go back a few seconds in the video to make sure it was your device thats getting powered off and not mine lol
yo saaaaame
i was like " i knew i shouldnt have installed that program"
@@waterfallbw damn son that also happened to me
i kind of almost crapped my self
ahlie that seriously spooked the living daylight out of me
Same bro
We have now successfully constrained ourselves inside this box.
Most relatable thing here.
it's corona time
Stutch corona time
Damn.. Tim went deep there..
POWERING OFF!!! Good, so I'm not the only one. I've also been hearing random beeps. IDK. It's weird, but good tutorial!!!
That freaked me out I thought it was my phone lol
It scared crap outta me dude
Yes
@@colwarsstudiobrickfilmandm8580 same
I thought it was my computer lol
Comments at the bottom: "Wow nice video." "What if XXXX happens?" "Can you make a tutorial about YY?"
Comments at the top: "pOWeRinG OFf>"
Hehe
did anyone notice the "Powering off" sound effect?
Lol i think that was headset in the background... oops
That scared the shit out of me!
holy f wtf was that
yea XD
How the fuck can you not notice that?
7:01 There's an easier way to do that :
if x_cor >= 750:
#screen width - Character width
x_cor = 750
#screen width - Character width
elif x_cor
what are the hashtags for
@@donk7001 notes they dont affect the code
You are the most genuine youtuber ever. Here we can get pure raw knowledge without any bs
Huge respect for you bro.
if jumpCount >= -10 :
y -= (jumpCount * abs(jumpCount)) * 0.5
jumpCount -= 1
Much quicker using absolute value function
This is a video to learn how to code. Sure it's more compact, but not everybody will understand it.
Nah it’s as easy to understand as what he did in the video if not easier
Much quicker is to use hyperbolic function (x ** 3)
sorry it isn't called hyperbolic
Round of applause...
bro I thought my computer was haunted from those background noises from your computer.
I got so scared when it screamed "POWERING OFF" 🤣
For the boundaries, I just created "_WIDTH" and "_HEIGHT" for the main windows dimensions (both are still 500), then I just did the following:
K_LEFT:
if x >= 1
x -= vel
K_RIGHT:
if x < _WIDTH - width
x += vel
K_UP:
if y >= 1
y -= vel
K_DOWN:
if y < _HEIGHT - height
y += vel
This allows you to get right up to the edge of the window, without the 5px gap
I believe that’s going to cut off the right side of your rectangle by 1-4 pixels. It will almost look identical, but slightly go over. Same thing with down except cuts off the bottom by 1-4 pixels.
1337 H4xXoR
2:31 wtf
This scared the heck out of me
yeah why is is there
IanHackz died
I thought for a second that it was the end of the world and my computer is talking to me
it must be his sex doll turning off due to inactivity for 5 minutes explains the youtube break
I found that jumping equations work better with linear equations. This is because the derivative of a quadratic equation is a linear equation, so while you want the shape of your jump to be quadratic, the rate at which you change the y value per frame is linear. An example would be y -= 30 - (2 * jumpCount).
Great video!
I tried to use both, but the quadratic equation is much better at simulating a real jump. Because the values rise faster in the beginning and slower near the end. This is what you would see during a normal jump. Your equation wouldn't do that. There no "gravity" is implemented where you start slowing down near the end of the jump. I like not having a uniform speed during a jump animation.
That's what I wanna say. Linear equation makes sense physically.
0:33 I freaked out with that "Beep-beep"
1:33 I freaked out with that "Beep-beep", again. Wait! That was a one minute 0 second difference!
2:31 Why is this saying "Powering off"?!
11:23
The correct formula should be y -= a * jumpCount. This is because the *change* in y at each step should be the *derivative* of the parabolic formula
U r doing tutorial at 1:20 Am. Omg plz sleep
wtf, I just watched my clock, and it's 1:19 LOL
I'm watching this at 3am
Legend says programmers don't sleep
That's not even late lol.
@@wsujrdfginoh1116 its not for entitled parents
I've been following your tutorials from the 1st and this for 2 hours and im so happy that im actually did a Mario character (beginner) hahaha
Great!Just Great, finnaly a good mentor for pygame, you just got a biiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiig fan
I think it's simpler to just store the initial y position and then add height to that by means of a parabola. No negative checks required. I also added something to the jumpCount variable to prevent the block from jumping off the top of the screen. Now it does a little jump, bumps the boundary and comes down at the appropriate speed. Check it out:
else:
if jumpCount == -10: ytemp = y
a = jumpHeight/-100
if jumpCount
If anyone doesn't get the jump thing, I'll explain it:
First remember that if jumpCount >= 10 then neg (which is like gravity) is equal to 1. This means that your character will go up as jumpCount squared * 0.5 is going to be * by 1. But if jumpCount is < 0 (or if jumpCount is a negative number) then neg is equal to 1, so jumpCount squared * 0.5 will be * by *negative* 1, so y will be minused by a negative number, so y will add it because a positive minus a negative number (p - -d) be the same as adding the number (p - -d is the same as p + d). Therefore, your y value will go down, because the lower on the window you go, the higher your 'y' value will be. Then, jumpCount is being minused by 1 everytime the loop will loop around, so your character goes up but at a decreasing rate. Then, when jumpCount reaches 0, like the peak of a graph, neg becomes -1 and you start going down. Then, when jumpCount becomes -11, it is < 10, so the else statement plays, isjump is false, and the jumping stops.
If this explanation was crap for you, tell me
THANKYOUU
Thanks for that one sir! The only thing that remains is learning about parabolas and its equations, for it's definitely interesting!
Session 2 completed, managed to get this tutorial done and loved doing it. thank you for these videos
For anyone having issues with jumping/choppiness/cube still going off screen, I switched my code over to Mu(an interpreter) from pycharm and it's working as intended. Not sure why, but maybe try that because a lot of you are having the same issue as I was.
For anyone having the problems in 2023. If you write this line before the loop:
clock = pygame.time.Clock()
and in the main loop write this instead of the pygame.time.delay(100):
while run:
clock.time(60)
you set the framerate to 60 fps instead of the fixed pygame.time.delay(100). The Clock controls the frame rate and ensures consistent frame rate over the game loop for you.
thx for the comment
@@ySkipery
Very nice tutorial.
I'm currently remaking my pygame framework handler and needed a brushup on how pygame worked.
It basicly makes this relativly low end programming integrated so you can just create an object and the framework will give you easy event handling like key presses, mouse movement, point and click buttons, movement to objects, simple physics and not to mention the main loop is already set up. All you gotta do is running the FRAMEWORK.tick() each frame and the rest is easy! Very handy for doing simple testing and fun projects that require a quick UI.
I'll definently be watching on!
7:30 auto english subtitle
Kek
LMAO the things you miss without auto generated subtitles. xD
gay
I came looking for this comment 🤣
please explain the quadratic part. I want a clear idea. Great Tutorial btw! GOOD JOB
Pretty hard for a newbie like me, but keep it up!
Same with me
Why when you make borders of the screen you use vel variable?
Why not make screen pixel positioning instead of vel:
if keys[pygame.K_LEFT] and x > 0:
x -= vel
if keys[pygame.K_RIGHT] and x < (screen_x - width):
x += vel
if keys[pygame.K_UP] and y > 0:
y -= vel
if keys[pygame.K_DOWN] and y < (screen_y - height):
y += vel
Yea me too
thanks it fixed mine
if you make a screen_width var and a screen_height var and use that when you get to subtracting height and width in K_RIGHT and K_DOWN, you'll be able to update your code easier by only changing the value of those vars once and still have the capability to not get off the screen. Sorry if that was complicated, I just wanted to h a l p
Wow it fix that :D
woah thankx it works
bro your so smart to make that kind of idea for jumping..
a great respect bro.....
If the fact that the character is not touching the border because of your velocity is bothering you (like it bothered me), here:
# height and width is for the character, screenHeight and screenWidth is for the window
if keys[pygame.K_LEFT] and x - vel >= 0:
x -= vel
elif keys[pygame.K_LEFT]:
x = 0
if keys[pygame.K_RIGHT] and x + width + vel = 0:
y -= vel
elif keys[pygame.K_UP]:
y = 0
if keys[pygame.K_DOWN] and y + height + vel
LMAO I was listening to epic music and the POWER OFF sound came just at the right time so I thought it was part of the music
LOL
This videos are a double goal for me because with your videos I`m improving my English and my coding skills... Thank you
y -= abs(jumpCount)*jumpCount/2
here is a more efficient implementation of the jump part, no need for all the if statements and multiplying by neg
Nice math right here : )
Might be able to keep object in the center of the screen and have the background move instead....possible video #7?
First of all:
Good tutorial that definitely convinced me to subscribe to this channel.
Second of all:
1. Can you tell me what was that "Powering Off" sound? It *almost* scared the h#ll outta me.
2. Also tell me why would ANYONE bother jumping if he can move freely up and down? Like wtf
And third of all:
There is no third of all but making a list of only 2 notes makes me feel like it's incomplete so I added this.
Thanks for the gr8 tutorial!
1. Headset sitting on my desk
2. It’s just for the purpose of learning because in most games you have one or the other and I wanted people that aren’t following the tutorial completely to be able to create their own games with the knowledge of how to jump.
Wow gr8 thank you so much
Also you replied instantly I love youtubers that do this
You can jump with a mathematical function is what I believe I do in this series or you can jump with a list of preset values to move each frame. Have to watch my side-scroller tutorial to understand that more in depth.
Ahmed Mokhles also you can use jumping for example if you decide to make a “floor” then you remove y axis controls so you can only go left or right like an adventure game which will allow creating obstacles for example rectangles on the “floor” that will trigger end_event aka you lose like the dinosaur game on google with no connection although not useful for his game very useful for others
at 10:24, the else statment which carries the isJump = False and Jumpcount = 10.
The else statment says "invalid syntax"
Thanks a lot! Nextime when I know more about pygame I would start making my own games! 😀
8:35 just a correction to the physics
During a standard jump, the acceleration is constant. It is always 9.8 m/s2 in the downwards direction. It is the velocity that changes and becomes zero at the top of the jump. I hope it helps ;)
10:55 Another correction, it is actually the opposite of it. You are faster the closer you are to the ground.
just came across this video while i was surfing and i am really surprised how cool this tutorial is
If everything in the universe depends on everything else in a fundamental way, it might be impossible to get close to a full solution by investigating parts of the problem in isolation !
Tim> Hope that make sense
2:31 i literally checked my laptop's battery lmao
There's a much simpler way to do boundaries. Just check if x respectively y is > 0 or < 500 before changing its position. Although you have to take into consideration the rectangles size so it doesn't move halfway outside or something similiar
I tried it before but your character can escape in the corners
That's pretty sweet!! I am so impressed with my jumping rectangle!!
your tutorials are so helpful with my programming project, thanks so much!
The jump mechanic is genius.
I think we also do this for restricting the player inside the boundaries:
For left, x>0
For right, x0
For down, y
I got the same kind of Boundary bevore i watched this vid :D.btw with this tutorial my guy keeps jumping do you know what i could have done wrong?it never gets to the else statement and instantly rejumps for some reason
@@fazefijiii6932maybe you have put the else in the wrong indentation or messed up the indentation somehow, I need to see the code to know what maybe going wrong, if u can, pls share the code, there r some sites which allow sharing of code
Great tutorial! one note tho, no need for "if keys[pygame.K_LEFT] and x > vel" , just do x > 0 , the vel is speed related and the greater you'll make the rectangle move the bigger the gap will be from it to the screen.
i thought the same
Thank you very much for this tutorial, its great. But I have a problem while doing this script. when I press SPACE my rect doesn't keep jumping! I mean I have to hold the SPACE to see its complete jump. I exactly wrote the same codes as you did but I don't know where is the problem. please help me to fix it. thanks a lot.
I've got the same problem, IDK why it's happening either!
I had the same! Fortunaly i found a way to fix it. Replace the "else:" from if not(isJump), with "if isJump:" and after that remove 1 tab from every line!
thanks so much i was looking up so many tutorials of how to make a palyer and they were too complicated for me to add more onto them until i found this
if you want to increase the speed without breaking the boundaries, you need to create another variable and use it instead of vel. Like this:
screenvel = 5
if keys[pygame.K_a] and x > screenvel:
Does this increase the speed of the jump?
you dont need to subtract the velocity when you are stopping player right at the movement but you need it when you are checking the player position and repositioning it to its expected position
Loving your tutorials, pace, coding style and clarifications. +1 sub
you should make a video on quadratic function I don't understand anything after jumpcount
same here
Hi Tim! Thank you so much for making this content! Very informative and helpful.
No problem !
Love your videos bro. Keep on doing the good work. I am 13 year old and I am doing python courses, and your videos help me alot.
Probably already commented on but @5:50 is a little confusing. I can't get my rectangle to go all the way to the left or top edge
another way to do the boundaries check, is by setting X and Y to a certain coodinate if they reachs a certain point in the screen.
for Example, i did:
if x > 450:
x = 450
if y > 430:
y = 430
if x < 10:
x = 10
if y < 10:
y = 10
I copy the same code on my computer and when I jump one time, the rect never stop jumping ???
The same happened for me . I intended the else statement for the jump twice instead of once .
May be you did something wrong bcoz it's working for me perfectly..
I think it is because it is in the if (event.type == pygame.KEYDOWN) loop
dude did you resolved the problem ???? if so tell me too .... i'm stuck with this
My earlier comment was the solution, check the indents because it might be in the wrong loop,in other words just check the number of spaces there is ment to be behind each sentence of code.
For example:
While True:
Print(number)
Is different from
While True:
Print(number)
So just remember becareful of the spaces
Tip: you can get rid of the black bars on the collisions by putting = instead of just < or >
I'm having this as a school project, and it's going to be a great project 😊
Ah yes! Classic arrow key controls are one of the first things I ever implement on a game.
I always have a habit of getting a tad fancier with it.
Suppose the space between the player x and the edge of the screen are less than the velocity but still greater than 0.
We have a remainder in there so it is good to use a modulus function and abs to compare the distances.
if keys[pygame.K_LEFT]:
if x > 0:
x -= vel
elif abs(x - screen_width) % vel != 0:
x -= (abs(x - screen_width) % vel)
if keys[pygame.K_RIGHT]:
if x < screen_width - width:
x += vel
elif x % vel != 0:
x += (x % vel)
if keys[pygame.K_UP]:
if y > 0:
y -= vel
elif abs(y - screen_height) % vel != 0:
y -= abs(y - screen_height) % vel
if keys[pygame.K_DOWN]:
if y < screen_height - height:
y += vel
elif y % vel != 0:
y += (y % vel)
I'm sure it can be refactored but that is how I did it. ^_^
Is this 100% correct? Because suppose that x = 1 and vel = 5, now if I press LEFT, the reactangle will go off-screen because you only checked "if x > 0" and not "if x > vel". Similar with the other directions.
I was on my way to uninstall my sound drivers after hearing random noises.Comment section saved me.Awesome video though
Quick small tip which is probaly obvious but if you change the width and height to 1920 by 1080 and tweak some other lines of codes you will be able to "play" your "game" in full screen.
8:21 I died a little inside with this physics explanation of a jump
Yep. Was getting very mad at his explanation of a parabola.
"you get faster as you jump up higher" - no you don't
"you have, like, a hang time" - mmm, I mean it's instantaneous
"[at the top] a position with zero acceleration" - nope, not that either, it has zero (y-component of) velocity
*sigh*
"hope that makes sense" - bish I lost brain cells
Is there a way to make that rectangle "double-jump"? Thank you in advance if u noticed this...
Thanks again Tim for this video, wouldn't it be right to not use "- vel" when preventing the rectangle to move out of the screen (down and right)
Refer to 6:30
that space is due to velocity....... while ending at the screen boundary....... can be corrected as:
if keys[pygame.K_LEFT] and x > 0:
x = x-velocity
if keys[pygame.K_RIGHT] and x < (300 - rect_width):
x = x + velocity
if keys[pygame.K_UP] and y > 0:
y -= velocity
if keys[pygame.K_DOWN] and y < (200 - rect_height):
y += velocity
Could someone clarify to me why do we use the neg variable at 13:30. If i am not wrong, y will go up and then go back because of the negative jumpCount? I even did the math with a calculator and was correct but the program shows otherwise.
width 800 on the x < 760 < 0 height 600 on the y > 0 < 540
Nice jump ! :) I already love that game !
hi Tim, I stuck in Jump, could you please do some math in there. hope you receive
Jumping part is a bit hard... But trying to understand. Nice tutorial btw 😇
oh shiiit you made writting boundaries *so simple* ! I remember when i first wrote my boundaries and it was like 20 (or more) lines of code, and i was reaaally happy that i diud that, because i found it pretty difficult .
Your light mode burned my eyes and heart both T-T
Thanks anyways 🙏
instead of the quadratic you could have used an odd exponent to produce the same effect in the jump say like 3 and then multiply it by 0.1 or something like that
pygame works very slowly on mac presumably due to resolution conflics, if anyone is having issues when you set the display options do the following:
pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
note:
you won't be able to hit the x icon to exit the program, hold shift + ctr + opt + esc for 1-3 seconds
put a key command along with the player controls to exit easily
i wrote this code but my character is jumping on the first click but it is not working if i click spacebar for the second time or more. how can i fix this problem ?
I'm Sorry that this is such a late comment, I just found this tutorial a while ago, so:
Do you have any videos on the formula you used for jumping?
If so, Please leave the link down below. Thanks!
is there another tutorial series explaining this
At 2:30 i almost died thought my phone was being hacked or siri turned evil when i heard that 😂
For anyone from ep1 wondering why their jump/game is laggy, it's cos pygame.time.delay is still 100. Tim uses delay 50 in this.
Thank you, I changed it to 50 and it's a lot smoother now!
Hey, I noticed that the rectangle moves smooth in the video, but the rectangle in my code moves in noticable increments... and not very smooth...
Did anyone else have this problem??
yes I have that too.
No, I don't.... It's working for me pretty good
If anybody is still wondering how to solve this, setting " pygame.time.delay( ) " to 34 will make the program MUCH smoother(you might still have it at 100). Apparently setting it to 34 will give you 30 fps, but you can just delete the line and it doent make a difference.
Hello Tim! Love your tutorial.
Btw I want to ask about what if I want to give time to 'recharge' the jump?
Like regeneration time. What is the function?
Thank you very much. Have a nice day!
Edit: Words
Rather than using neg as a positive or negative variable, why not use abs(). Absolute function will return the absolute value of the answer that is always positive
Thanks for this pygame code Tim. It is quite complicated especially the else and indent part got to get it correct or the window will not show. But at last get the jump part working after examining how the code works.
how did you do it. It didn't work on my laptop.
Thank u so much it really helped a lot ,it's a best pygame turtorial.
11:52 what was that windows notif beep?
A simpler way to do it is by having a "SpeedY" variable and suybtracting it by 1 every frame, and changing the Y position by SpeedY. Jumping would just be setting the SpeedY variable to something positive, and speed y can be set to 0 on vertical collisions to not move any further. However, I never tried it with python, so I'm not sure if it works well here
I like this video. you explain your game in a compact and interesting way! well done! thank you
u were recording at midnight?!
Nobody:
Not a soul:
His Computer: POWERING OFF
Your videos are amazing now I can make my games
what was the sound that says powering off 02:36
There's a bug. Set the velocity really high, see what happens? The problem is that if the game calculates you are close to the border it just stops moving, the way to fix this is instead of stating it like this
if keys[pygame.K_RIGHT] and x < screenWidth - width - vel:
x += vel
state it like this
if keys[pygame.K_RIGHT]:
x += vel
if x > screenWidth - width:
x = screenWidth - width
because now, instead of detecting that an extra full velocity move would go over and just stop moving, you just clamp the amount it moves
I am not here to mock you or anything, I am just kind of a code geek, I know 4 other languages and have Unity experience, so I have a good eye for bugs. I really love these videos because they give me knowledge about how the pygame namespace works in programming a game.
PS I haven't watched the rest of the series yet so if you fixed this issue already I don't know and I'm sorry if you did and I commented this.
I'm still not able to hold down the arrow keys; I have to press every time. Any help?
Loved the video and was also tricked by Powering off sound 🤣 had to check my airpods 😂
I wanted to ask if we could do a simple if statement where we check if the x and y positions are greater than or less than the screen dimensions. For example:
if bird_x > 1920 or bird_x < 0 or bird_y > 1000 or bird_y < 0:
bird_x = 100
bird_y = 500
This basically just resets the sprite back to its original position
Wasn't really doing much youtube at the time 0:32
youtube yells at you after you said that.
i solved the black bar problem by subtracting the rectangle's width from the screen width and did the same thing with height, here is that code:
keys = pygame.key.get_pressed()
if keys [pygame.K_LEFT] and x > vel-5:
x -= vel
if keys[pygame.K_RIGHT] and x < screen_width - char_width:
x += vel
if keys[pygame.K_UP] and y > vel-5:
y -= vel
if keys[pygame.K_DOWN] and y < screen_height - char_height:
y += vel