Pygame Tile Based Game Tutorial: Physics and Delta Time

Поделиться
HTML-код
  • Опубликовано: 5 окт 2024
  • In this video, we'll discuss player physics and framerate independence. We'll learn how we can create a player character that has fluid and fun movement.
    Code can be found on my github: github.com/Chr...
    Personal Channel - / @sunset_cd
    Download tiled: www.mapeditor....
    Previous video in the series: • Pygame Tile Based Game...
    My video on sprite sheets: • Pygame Sprite Sheet Tu...
    Music:
    Mother - Field Theme 1
    Slowmo Jump Video: • Huge Super Slow Motion...
    Article on Framerate Independence: / understanding-delta-time
    #pygame #physics #gamedev
    I stayed up until 5 am doing this, and forgot my outro lol

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

  • @CDcodes
    @CDcodes  3 года назад +4

    Hey Everyone, there's a slight bug in the code. In the limit_velocity() function, I mixed up the bounds and forgot to assign the value to our player (whoops)
    Here's the corrected function:
    def limit_velocity(self, max_vel):
    self.velocity.x = max(-max_vel, min(self.velocity.x, max_vel))
    if abs(self.velocity.x) < .01: self.velocity.x = 0
    Technically, bounding the velocity is optional. The friction value will increase with the player's velocity, eventually causing it to hit a ceiling. But in case you want more control over how fast your character goes, bounding the velocity is not a bad option.
    Special thanks to MattR0se on the Pygame reddit for pointing this out!
    Also for the position formula, you'll get better results if you subtract the acceleration term

  • @richardmartinez4496
    @richardmartinez4496 3 года назад +9

    This is EXACTLY what I was looking for thank you so much!

    • @CDcodes
      @CDcodes  3 года назад +1

      Happy to help!

  • @Olorin409
    @Olorin409 3 года назад +4

    Great tutorial,
    Looking forward to the next one!

    • @CDcodes
      @CDcodes  3 года назад +1

      Thank you! Shouldn't take as long to put out haha

  • @RoboMarchello
    @RoboMarchello 3 года назад +4

    Thank you!

    • @CDcodes
      @CDcodes  3 года назад +2

      You're welcome, thanks for watching : )

  • @JoseOrtiz-ds6rs
    @JoseOrtiz-ds6rs 3 года назад +2

    Thanks for sharing all this info, i was passing a hard time trying to code the Delta Time by my self but i messed up.

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

      Happy to help!

  • @FeedFall8
    @FeedFall8 3 года назад +2

    Amazing video

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

      Thank you :)

  • @fernandoalves3953
    @fernandoalves3953 3 года назад +2

    Nice!

    • @CDcodes
      @CDcodes  3 года назад +1

      Thank you! Cheers!

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

    Excellent ,keep the good work!

    • @CDcodes
      @CDcodes  3 года назад +2

      Thank you!

  • @denisobrezkov4354
    @denisobrezkov4354 3 года назад +1

    Pygame really needs an entrance tutorial like Godot's one.

  • @Jagjot_Singh
    @Jagjot_Singh 3 года назад +2

    bro im getting a error, BTW nice video man
    in vertical_movement
    self.position.y += self.velocity.y * dt + (self.acceleration * 0.5) * (dt * dt)
    TypeError: unsupported operand type(s) for +: 'float' and 'pygame.math.Vector2'

    • @CDcodes
      @CDcodes  3 года назад +2

      self.acceleration should be self.acceleration.y I believe

  • @mtbminibomber
    @mtbminibomber 3 года назад +3

    I will never be as smart as you

    • @CDcodes
      @CDcodes  3 года назад +2

      I'm not smart, I just have practice. It's all just about learning : )

  • @joewhitters800
    @joewhitters800 3 года назад +2

    Hi again, again your code is great I have a question though I've since made progress on my game but I've been refining my code to allow for my future updates (e.g. multiplayer) and increasing the size of my sprite sheets I've come to the bit in the tiles.py where you load the tiles and the issue is I no longer have like 20 assets I have in one sprite sheet 1759 and I need and would like a short method of doing this as the other method is writing a python script to write all of the elif statements I did do that when I had a more manageable amount of assets of a single sprite sheet, do you have any advise to get this working? as at the moment when i load the game there is no issue it just loads no tiles but my character loads fine and there is no game crashing issues.
    here is what i tried:
    def load_tiles(self, filename):
    tiles = []
    map = self.read_csv(filename)
    x, y = 0, 0
    pointless = 0
    for row in map:
    x = 0
    for tile in row:
    for A in range (1,2):
    if tile == A:
    print("i work")
    tiles.append(Tile(("tile000.png"), x * self.tile_size, y * self.tile_size, self.spritesheet))
    for B in range (2,10):
    if pointless == "1":
    print("O oh")
    elif tile == B:
    print("i work"+str(z))
    temp1= str("tile00"+str(z)+".png")
    tiles.append(Tile((temp1), x * self.tile_size, y * self.tile_size, self.spritesheet))
    z+=1
    for C in range (10,100):
    if pointless == "1":
    print("O oh")
    elif tile == C:
    print("i work"+str(z))
    temp1= str("tile0"+str(z)+".png")
    tiles.append(Tile((temp1), x * self.tile_size, y * self.tile_size, self.spritesheet))
    z+=1
    for D in range (100,1760): #self.Ss+1
    if pointless == "1":
    print("O oh")
    elif tile == D:
    print("i work"+str(z))
    temp1 = str("tile"+str(z)+".png")
    tiles.append(Tile(("+temp1+"), x * self.tile_size, y * self.tile_size, self.spritesheet))
    z+=1
    x+=1
    y+=1
    self.map_w, self.map_h = x * self.tile_size, y * self.tile_size
    return tiles

    • @CDcodes
      @CDcodes  3 года назад +1

      Hi Joe,
      When it comes to lots of assests, I like to group them by their functionality and load them. Something like:
      if tile falls in range 0-10:
      load enemy(tile)
      elif tile falls in range 11-99:
      load foreground tile(tile)
      elif tile falls in range 100-150
      load background tile(tile)
      I think this is what you're aiming to do. I plan on launching my discord sever soon. Perhaps me or someone else will be able to help you there. I'll keep you updated

  • @shh6545
    @shh6545 2 года назад

    Shouldn't in about 11:08 that vertical movement thing be:
    1. self.position.y += self.velocity.y * dt - (self.acceleration.y * .5) * (dt * dt)
    and not
    2. self.position.y += self.velocity.y * dt + (self.acceleration.y * .5) * (dt * dt) ?
    At least now in my program, the jumping stays the same if I change the fps. With that second the jumping arcs are not the same.
    If not, something has went terribly wrong in my program.

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

    Ok guys, I’m having trouble with understanding the concept.
    Say Mario can move 5px per frame and the jump is 300px wide and he can only be in the air for one second. He makes the jump when the game runs in 30FPS, but if it is any lower he can’t.
    My question is how is that happening. The function that adds gravity would also operate slower if the game run in lower fps, so he wouldn’t be in the air for only one second but for more.
    Thanks in advance.

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

      Hey there. You're exactly right, gravity would work differently if our game was not independent of the framerate. I just wanted to simplify things to x direction case in the example for the sake of keeping things simple.

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

      @@CDcodes thanks for the reply

    • @CDcodes
      @CDcodes  3 года назад +1

      @@bovfbovf I'd say it depends on your scope. If you're making a small little game for your friends/family, its not super necessary. If you plan on releasing a commercial game, I think it's important.

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

      @@CDcodes ill just go ahead and implement it,🙌🏼

    • @stylus_sheath
      @stylus_sheath 3 года назад +1

      ...cause the whole logic is not to make a fast game to run slower, but to avoid for a slow game to run faster, putting a cap on higher framerates. If not, your 60fps game running in 30fps mode will skip pixels (and the inverse is true the same). Practically, you emulate the slowest framerate. Mario SLOWEST devised time is 1 jump second at 30 fps. It should be 1 second at 60 fps too, and that's why you apply a delta. If your device run slower than 30fps, is not a game issue but a lag (and that's mean the device is wrong or I did a bad game since the start).

  • @JaJ-w3b
    @JaJ-w3b Год назад

    what if I just took the difference between the set fps and the current fps and then in the next line set the fps where you add the difference to the fps for that loop?

  • @CipherZIsMe
    @CipherZIsMe 2 года назад

    How exactly would I build up the velocity to a higher point, at a slower pace? Can't figure it out..

    • @CDcodes
      @CDcodes  2 года назад

      Lower the acceleration but raise the max value velocity can take

  • @duskmanepokemon4943
    @duskmanepokemon4943 3 года назад +1

    Sir can you tell me that what is spritesheet ?
    Can i import it similarly like pygame or i have to do something ?

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

      Spritesheets are a way to quickly access images for your game. The module I use is one I made in a previous video, so it can't be imported from pygame. But you can follow the video and make it yourself if you'd like to see how it works. ruclips.net/video/ePiMYe7JpJo/видео.html

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

      @@CDcodes thanks bro, I saw your video, but I cannot save the .json and the png file,
      Instead a .tps file is created.
      Can you suggest me something
      Edit : is the spritesheet.py is same for every project ?
      BTW tutorial was nice bro, I'm exited to make my game ☺