Framerate Independence - Pygame Tutorial

Поділитися
Вставка
  • Опубліковано 18 лис 2024

КОМЕНТАРІ • 60

  • @neotodsoltani5902
    @neotodsoltani5902 4 роки тому +54

    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

    • @bthub3
      @bthub3 Рік тому +1

      @friedcrack4787 I heard time.perf_counter() is better. I'm using it and it works great.

    • @modernmage
      @modernmage 5 місяців тому

      @@bthub3 how? I tried replacing time.time() with time.perf_counter() and it broke everything

    • @bthub3
      @bthub3 5 місяців тому

      @@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.

    • @modernmage
      @modernmage 5 місяців тому +1

      @@bthub3 ahhh I got it to work now, thx

    • @bthub3
      @bthub3 5 місяців тому

      @@modernmage 👍great!

  • @ishaanpandey9174
    @ishaanpandey9174 Рік тому +2

    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!

  • @caseycu
    @caseycu 4 роки тому +7

    Really cool of you to make these; thanks for sharing your experience!

  • @apekz3592
    @apekz3592 7 місяців тому +1

    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.

  • @jojofan2017
    @jojofan2017 3 роки тому +3

    Thank you for your tutorials!

  • @liam8398
    @liam8398 3 роки тому +5

    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

    • @DaFluffyPotato
      @DaFluffyPotato  3 роки тому +2

      for collisions, you do multiple iterations of collision checks to ensure that entities don't pass through objects.

    • @blessingipigansi5142
      @blessingipigansi5142 3 роки тому

      Won't that significantly slow down the performance of the game considering how expensive checking for collisions are in the first place?

    • @mr0o
      @mr0o 2 роки тому +2

      @@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

    • @blessingipigansi5142
      @blessingipigansi5142 2 роки тому

      @@mr0o thank you, I already watch javidx9 as well.

  • @serufim
    @serufim 4 роки тому

    You are awesome, please don't stop making videos

  • @davidjijo6657
    @davidjijo6657 4 роки тому +1

    glad to found this channel, thanks man

  • @axhraf7712
    @axhraf7712 3 роки тому +1

    dt = time.time() - last_time
    UnboundLocalError: local variable 'last_time' referenced before assignment

    • @aqua8584
      @aqua8584 2 роки тому

      you're supposed to define last_time before the loop as well

  • @thequicklyid5699
    @thequicklyid5699 Рік тому +1

    Extremly helpful!

  • @MathRythmatic
    @MathRythmatic 5 місяців тому

    should I apply dt to any incremeted variable? Or just to moving objects with speed

  • @mannb1023
    @mannb1023 3 роки тому +2

    can someone explain why he defines time.time() globally?

    • @mkac0036
      @mkac0036 3 роки тому

      You can't make varibles in loops, so he makes it before and just modifies it.

  • @vialomur__vialomur5682
    @vialomur__vialomur5682 3 роки тому

    You are amazing keep doing it!

  • @simplepycodes
    @simplepycodes 4 роки тому +1

    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.

    • @ZgavY
      @ZgavY 2 роки тому +1

      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

  • @greg.sym.4115
    @greg.sym.4115 3 роки тому

    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?

  • @shinydewott
    @shinydewott 6 місяців тому

    how do you make sure the player properly collides with things at lower framerates using this?

    • @DaFluffyPotato
      @DaFluffyPotato  6 місяців тому

      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.

    • @shinydewott
      @shinydewott 6 місяців тому

      @@DaFluffyPotato that makes sense, thanks for the quick reply :D

  • @zerokun2655
    @zerokun2655 3 роки тому

    i get an error.. TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'float'

  • @Gamer-nm1uh
    @Gamer-nm1uh 4 роки тому +1

    Can you help me please? I don't know how to install pygame from the official website

  • @vulnoryx
    @vulnoryx 3 роки тому

    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?

    • @ZgavY
      @ZgavY 2 роки тому

      You also apply delta time to the velocity, not just the y coordinates

    • @vulnoryx
      @vulnoryx 2 роки тому

      @@ZgavY ill try

    • @ZgavY
      @ZgavY 2 роки тому

      @@vulnoryx well that's what I did and it works fine imo. It's not 100%, but then again, perfection is not achievable

  • @abramhansen
    @abramhansen 4 роки тому

    bless you!

  • @SuperQuwertz
    @SuperQuwertz 4 роки тому

    Why is the player not moving double the speed when he has 120 FPS with this implementation?

    • @DaFluffyPotato
      @DaFluffyPotato  4 роки тому +9

      That’s the entire point of doing this

    • @leobozkir5425
      @leobozkir5425 4 роки тому

      FPS isnt speed; it is how smoothly we see things on the screen

    • @SuperQuwertz
      @SuperQuwertz 4 роки тому

      @@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? :)

    • @DaFluffyPotato
      @DaFluffyPotato  4 роки тому +4

      @@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.

    • @DaFluffyPotato
      @DaFluffyPotato  4 роки тому +3

      @@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.

  • @erik-martin
    @erik-martin 4 роки тому +1

    Would it make more sense to multiply dt by your Framerate variable (constant) in case you decided to change that later?

    • @leobozkir5425
      @leobozkir5425 3 роки тому +1

      nevermind no the point is that your game is running based on that fps so the whole point is not to change it

  • @eboatwright_
    @eboatwright_ 3 роки тому

    yoink

  • @surajsalian583
    @surajsalian583 4 роки тому +1

    can you make video on how to make Boss
    and
    Level Selection Page
    please do reply

    • @DaFluffyPotato
      @DaFluffyPotato  4 роки тому +4

      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.

    • @surajsalian583
      @surajsalian583 4 роки тому

      @@DaFluffyPotato thanks

    • @surajsalian583
      @surajsalian583 4 роки тому

      I just onto creating the boss

    • @surajsalian583
      @surajsalian583 4 роки тому

      @@DaFluffyPotato tell best site for enemies sprite

    • @surajsalian583
      @surajsalian583 4 роки тому

      How to save high score data like other games