Framerate Independence - Pygame Tutorial

Поделиться
HTML-код
  • Опубликовано: 24 дек 2024

Комментарии • 60

  • @ishaanpandey9174
    @ishaanpandey9174 Год назад +3

    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!

  • @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 6 месяцев назад

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

    • @bthub3
      @bthub3 6 месяцев назад +1

      @@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 6 месяцев назад +2

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

    • @bthub3
      @bthub3 6 месяцев назад +1

      @@modernmage 👍great!

  • @caseycu
    @caseycu 4 года назад +7

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

  • @apekz3592
    @apekz3592 8 месяцев назад +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!

  • @davidjijo6657
    @davidjijo6657 4 года назад +1

    glad to found this channel, thanks man

  • @serufim
    @serufim 4 года назад

    You are awesome, please don't stop making videos

  • @vialomur__vialomur5682
    @vialomur__vialomur5682 3 года назад

    You are amazing keep doing it!

  • @thequicklyid5699
    @thequicklyid5699 2 года назад +1

    Extremly helpful!

  • @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 года назад +3

      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 года назад +3

      @@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 года назад +1

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

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

  • @MathRythmatic
    @MathRythmatic 6 месяцев назад

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

  • @abramhansen
    @abramhansen 4 года назад

    bless you!

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

  • @shinydewott
    @shinydewott 7 месяцев назад

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

    • @DaFluffyPotato
      @DaFluffyPotato  7 месяцев назад

      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 7 месяцев назад

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

  • @greg.sym.4115
    @greg.sym.4115 4 года назад

    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?

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

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

  • @Gamer-nm1uh
    @Gamer-nm1uh 4 года назад +1

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

    • @DaFluffyPotato
      @DaFluffyPotato  4 года назад

      Did you watch my video on installing Pygame?

    • @Gamer-nm1uh
      @Gamer-nm1uh 4 года назад

      DaFluffyPotato oh no

  • @zerokun2655
    @zerokun2655 4 года назад

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

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

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

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