Actually, Pygame has a tricky way to calculate 'delta_t', here's how: clock = pygame.time.Clock() FPS = 50 while True: delta_t = clock.tick(FPS) #tick() function will calculate the delta time since it was called in last frame
@@modernmage I don't know what setup you have but for me, outside the while loop i put: pre_time = time.perf_counter() then in the loop i put: now_time = time.perf_counter() dt = now_time - pre_time pre_time = time.perf_counter() so basically i use the function to find the dt so i can multiply it with the velocity values.
While this tutorial is helpful, dt seemed to vary wildly from frame to frame in my game when I tried implementing this, so what I did was create an ideal framerate variable (def_frame) and define dt like: clock = pygame.time.Clock() while run: clock.tick(def_frame) if not (clock.get_fps() == 0): #To avoid division by zero error in first few frames dt = def_frame/clock.get_fps() This gives much more stable and workable values for dt which doesn't vary hugely between frames, and it works perfectly with my game's custom animation and physics system. Thank you for the tutorial though, this is without doubt the best channel ever to learn pygame from!
I do that, but I noticed it could lead to some bugs like hitbox collision detection failing due to objects skipping through long distances etc, at this point the game is unplayable though
@@blessingipigansi5142 yeah, it will. Collision detection is already a difficult issue, but it becomes even more tricky if you care about doing it efficiently and without error. It's not impossible though, there are loads of videos that talk about different methods of collision detection. javidx9 is also a handy resource, this dude has so many helpful videos. He does everything in c++, but the concepts still apply to any other language, including python
Thanks for the video. but what if I have a movement based on tiles like each step is 32 pix, I have to keep my FPS at 10! if I change it to 60 the player moves super fast, and when FPS is on 10 I don't have enough fast keyboard response! how to have fast keyboard input at 60FPS! and game speed at 10 FPS? Not sure even it's possible or do miss something entirely? Thanks again.
You just don't use clock.tick(10) since you don't need that now, the game is framerate independent. Now the input is as fast as possible. And change your fps constant to 10. This way the game will act as if it's running at 10 fps, but it's not and you get the best input latency possible
The easiest solution is to force delta time to be above a certain value. That means that a little lag will have normal time progression, but a massive lag spike will slow down time so the physics or other features don't break.
@@DaFluffyPotato Sorry, my question was not clear. In case the game offers uncapped fps, in that case more than 60fps, wouldn't be the movement out of proportion, since the dt scaled too much? And how would I fix that? Also could you do a video about framerate independent animation for sprites? :)
@@SuperQuwertz dt is a measure of how much time has passed. By multiplying everything relating to time by how much time has passed each frame, it updates everything at the appropriate rate no matter what the FPS is. Dt doesn't cause that problem, it fixes it. Without it, you'll be doing everything by x amount every frame, which results in the game speeding up. Framerate independent animation just requires that you apply dt to the change in the timer for the animation.
@@leobozkir5425 No. FPS is how many frames are rendered per second. The easiest implementation of games on a framework level is to move things x amount every frame since you update everything on a per-frame basis. Dt is how you get everything to occur at the same speed no matter what your FPS is. Most engines automatically apply it to various aspects of the game. For the rest of the scripting, people are normally taught early on to multiply everything by dt.
Those are too similar to things I’ve done in other videos for the time being. If you’re having a hard time with those, check out the “How to Code (almost) Any Feature” video I’ll be making some time in the future.
Actually, Pygame has a tricky way to calculate 'delta_t', here's how:
clock = pygame.time.Clock()
FPS = 50
while True:
delta_t = clock.tick(FPS) #tick() function will calculate the delta time since it was called in last frame
@friedcrack4787 I heard time.perf_counter() is better. I'm using it and it works great.
@@bthub3 how? I tried replacing time.time() with time.perf_counter() and it broke everything
@@modernmage I don't know what setup you have but for me, outside the while loop i put:
pre_time = time.perf_counter()
then in the loop i put:
now_time = time.perf_counter()
dt = now_time - pre_time
pre_time = time.perf_counter()
so basically i use the function to find the dt so i can multiply it with the velocity values.
@@bthub3 ahhh I got it to work now, thx
@@modernmage 👍great!
While this tutorial is helpful, dt seemed to vary wildly from frame to frame in my game when I tried implementing this, so what I did was create an ideal framerate variable (def_frame) and define dt like:
clock = pygame.time.Clock()
while run:
clock.tick(def_frame)
if not (clock.get_fps() == 0): #To avoid division by zero error in first few frames
dt = def_frame/clock.get_fps()
This gives much more stable and workable values for dt which doesn't vary hugely between frames, and it works perfectly with my game's custom animation and physics system.
Thank you for the tutorial though, this is without doubt the best channel ever to learn pygame from!
Really cool of you to make these; thanks for sharing your experience!
You can also do it with multithreading if you dont want delta time boilerplate, like thread 1 has 60 ticks; and thread 2 has 20000 ticks.
Thank you for your tutorials!
I do that, but I noticed it could lead to some bugs like hitbox collision detection failing due to objects skipping through long distances etc, at this point the game is unplayable though
for collisions, you do multiple iterations of collision checks to ensure that entities don't pass through objects.
Won't that significantly slow down the performance of the game considering how expensive checking for collisions are in the first place?
@@blessingipigansi5142 yeah, it will. Collision detection is already a difficult issue, but it becomes even more tricky if you care about doing it efficiently and without error. It's not impossible though, there are loads of videos that talk about different methods of collision detection. javidx9 is also a handy resource, this dude has so many helpful videos. He does everything in c++, but the concepts still apply to any other language, including python
@@mr0o thank you, I already watch javidx9 as well.
You are awesome, please don't stop making videos
glad to found this channel, thanks man
dt = time.time() - last_time
UnboundLocalError: local variable 'last_time' referenced before assignment
you're supposed to define last_time before the loop as well
Extremly helpful!
should I apply dt to any incremeted variable? Or just to moving objects with speed
can someone explain why he defines time.time() globally?
You can't make varibles in loops, so he makes it before and just modifies it.
You are amazing keep doing it!
Thanks for the video. but what if I have a movement based on tiles like each step is 32 pix, I have to keep my FPS at 10! if I change it to 60 the player moves super fast, and when FPS is on 10 I don't have enough fast keyboard response! how to have fast keyboard input at 60FPS! and game speed at 10 FPS? Not sure even it's possible or do miss something entirely? Thanks again.
You just don't use clock.tick(10) since you don't need that now, the game is framerate independent. Now the input is as fast as possible. And change your fps constant to 10. This way the game will act as if it's running at 10 fps, but it's not and you get the best input latency possible
I imagine it would be a more helpful constant to use 1/dt - i.e. take min(1/dt, fps) as your frequency constant when calculating speed, no?
how do you make sure the player properly collides with things at lower framerates using this?
The easiest solution is to force delta time to be above a certain value. That means that a little lag will have normal time progression, but a massive lag spike will slow down time so the physics or other features don't break.
@@DaFluffyPotato that makes sense, thanks for the quick reply :D
i get an error.. TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'float'
ok no i'm stupid
Can you help me please? I don't know how to install pygame from the official website
Did you watch my video on installing Pygame?
DaFluffyPotato oh no
thenks.......but there is a problem. At velocity based games when jumpuing at low framerate you fly much hugher than normal. Is there a solve?
You also apply delta time to the velocity, not just the y coordinates
@@ZgavY ill try
@@vulnoryx well that's what I did and it works fine imo. It's not 100%, but then again, perfection is not achievable
bless you!
Why is the player not moving double the speed when he has 120 FPS with this implementation?
That’s the entire point of doing this
FPS isnt speed; it is how smoothly we see things on the screen
@@DaFluffyPotato Sorry, my question was not clear. In case the game offers uncapped fps, in that case more than 60fps, wouldn't be the movement out of proportion, since the dt scaled too much?
And how would I fix that?
Also could you do a video about framerate independent animation for sprites? :)
@@SuperQuwertz dt is a measure of how much time has passed. By multiplying everything relating to time by how much time has passed each frame, it updates everything at the appropriate rate no matter what the FPS is. Dt doesn't cause that problem, it fixes it. Without it, you'll be doing everything by x amount every frame, which results in the game speeding up.
Framerate independent animation just requires that you apply dt to the change in the timer for the animation.
@@leobozkir5425 No. FPS is how many frames are rendered per second. The easiest implementation of games on a framework level is to move things x amount every frame since you update everything on a per-frame basis. Dt is how you get everything to occur at the same speed no matter what your FPS is. Most engines automatically apply it to various aspects of the game. For the rest of the scripting, people are normally taught early on to multiply everything by dt.
Would it make more sense to multiply dt by your Framerate variable (constant) in case you decided to change that later?
nevermind no the point is that your game is running based on that fps so the whole point is not to change it
yoink
can you make video on how to make Boss
and
Level Selection Page
please do reply
Those are too similar to things I’ve done in other videos for the time being. If you’re having a hard time with those, check out the “How to Code (almost) Any Feature” video I’ll be making some time in the future.
@@DaFluffyPotato thanks
I just onto creating the boss
@@DaFluffyPotato tell best site for enemies sprite
How to save high score data like other games