How to Code Realistic Physics in Python Games! PyGame Tutorial (Gravity, Bounce, Throw, Friction)

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

КОМЕНТАРІ •

  • @physicsinveins2705
    @physicsinveins2705 Рік тому +12

    This is one of the UA-cam channel that needs attention guys please share as much as possible. This man is really incredible and awesome. I've seen many youtubers but I never felt attracted towards their teaching than I did with le master tech channel. There is something magical about this channel. So I hope people who see my comment would definitely hit the link share and subscribe button

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

      Thank you so much!! that's a super nice comment, I really appreciate the support!

  • @braybro7660
    @braybro7660 2 місяці тому +1

    Bro you cool as hell! I now feel confident using pygame to create physics simulations and games! Keep up the good work!

    • @lemastertech
      @lemastertech  2 місяці тому

      Thank you so much! Nice to have you here and I’m glad it was helpful!!

  • @mjrober33
    @mjrober33 Рік тому +3

    fun coding along on this project. One thing I noticed and not sure how this can be corrected, but when one ball is on top of another ball, when you select one, it selects both.

    • @lemastertech
      @lemastertech  Рік тому +3

      Hi this is an easy fix you can just go into the mouse button down code and change the code to a for loop that will check if each ball is selected, but only check If selected = false, that way once it finds one it will stop looking!

  • @bgsand56
    @bgsand56 7 місяців тому +2

    ive recently been wanting to make some physics stuff but i didnt know where to start, this is very helpful thanks

    • @lemastertech
      @lemastertech  7 місяців тому

      Thanks for watching glad you found the channel!!

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

    Smashing it, the hard work's paying off my man! Almost at 4K subs then you'll be aiming for 5K! Really enjoying your videos, even if I'm not commenting on all of them, this one was awesome!

    • @lemastertech
      @lemastertech  Рік тому

      Thanks so much Dale!! Can’t wait to hit the big milestones but enjoying the process too! I feel like I’m getting a lot more proficient at big python productions and I’d like to keep mixing in C++ and arduino but dang it python content always does so much better 😂 appreciate your support as always man! I’m glad you liked this one too! Cheers my friend!

  • @DrBFromEarth
    @DrBFromEarth 6 місяців тому +1

    You deserve a lot more views for this work my friend. Five stars. Thank you!

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

      Thanks so much Dr B from Earth!! I’m glad you found it useful, I don’t worry too much about views UA-cam is a long term game it seems :)

  • @MoonLee-y6q
    @MoonLee-y6q Рік тому +2

    amazing wow... realistic friction.

    • @lemastertech
      @lemastertech  Рік тому

      Thanks for watching!! Glad you liked it!

  • @MichaelGarcia-cg9on
    @MichaelGarcia-cg9on 7 місяців тому +1

    Good stuff, I really like how you broke everything down with the code and explained it.

  • @yigawaffle
    @yigawaffle 6 місяців тому +1

    Thank you! This helped me with a project I had wanted to make!

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

      Awesome so glad it helped you thanks for watching!

  • @dolodestinations7628
    @dolodestinations7628 3 місяці тому +1

    did you make the video on the advanced physics? great video!

    • @lemastertech
      @lemastertech  3 місяці тому +1

      I have not yet! I have such a long todo list of future videos haha! Thanks for watching :)

  • @joachimandresaldano-sle2302
    @joachimandresaldano-sle2302 10 місяців тому +2

    Hey, amazing and informative video, I've been searching for something this good for quite some time now. I appreciate u learning it to other people, it's really helpful. I've watched this video all the way thru and prob spendt about 2 hours doing, understanding and learning from u. this is not the first video I've seen of u, amazing work all the way thru!. I see ur almost at 10k subs, and I subbed, so now ur 1 step closer and I would love to be on this journey with u. I got a question, would u mind maybe showing how to make some type of rocket that starts on the ground level, where it takes of and flies up in the air with fuel, and when the fuel runs out it goes back to the ground with the gravity we made. like maybe make it realistic. maybe put in gravity, air-resistance, and so on. thank u so much for all ur help!

    • @lemastertech
      @lemastertech  10 місяців тому +1

      Hey thanks so much for that super nice comment!! I would love to do a rocket launch simulation game at some point with some realistic physics! I have thought about that a few times before and it would be a lot of fun, but definitely a ton of work to put together, I can start building it out and see if it’s feasible!! 😊

  • @nhatsanchau1613
    @nhatsanchau1613 Рік тому +4

    Hi, I have a small question, I don't know how to put picture on object ( like giving the balls in your video a skin ). Please help me if you know, thank you so much !

    • @quanhoang5539
      @quanhoang5539 Рік тому

      same

    • @lemastertech
      @lemastertech  Рік тому +3

      Hi - you would need to use screen.blit(ball_img, (Xcoord, ycoord)) and make your coords the same as the ball - you could define your circle as a surface then draw an image onto the ball surface! PyGame doesn’t have an amazing way to do this natively like a full game engine!

    • @nhatsanchau1613
      @nhatsanchau1613 Рік тому

      @@lemastertechThanks for your answer, but Im new to pygame so I couldn’t understand it clearly. I would like to have your discord so I can get your help directly ! Hope to receive your answer

    • @quanhoang5539
      @quanhoang5539 Рік тому

      ​@@lemastertech thanks for the reply, can i ask how to define the circle pls? i did the screen.blit(..(ball.x_pos,ball.y_pos)) but it draws at the center of the circle and behind
      its

  • @joachimandresaldano-sle2302
    @joachimandresaldano-sle2302 10 місяців тому +1

    hi, i was also wondering on how to make the balls collide with each other, obvi with physics in the collition between the balls aswell.
    and not to take to much of ur time, but I am quite new to programming, in school we had to chose a project to work with, and now I have chosen to make a "pool" game, but I do need real physics and I was wondering if u could try and make something similar to a pool game which can help and guide me and getting better at programming.

    • @lemastertech
      @lemastertech  10 місяців тому +3

      That’s a very cool idea but I will not lie to you I am super busy these days! I have a new baby and started a new job recently so I’m struggling to keep up with huge projects! You should check out the UA-cam channel coding with Russ - I think his channel is brilliant and he did a pool in Godot tutorial I believe!

    • @joachimandresaldano-sle2302
      @joachimandresaldano-sle2302 10 місяців тому

      wow thank u for that@@lemastertech, I wish u luck with the baby and work and thank u for taking ur time to tell me where I could find it! have a nice day

  • @izi989
    @izi989 2 місяці тому +1

    Hi, I know it might be a bit to late, but I'm having an issue. I'm stuck at the moment of drawing the circle, but instead I've made my own image of the circle and I want to load that image and make it appear on screen, but I'm not sure how to do that.
    Which I mean is that I know about the whole pygame.image.load but i have no clue how to make it work with the whole class situation

    • @lemastertech
      @lemastertech  2 місяці тому +1

      HI, youll want to pass the image of the ball in as another variable when you create new classes similar to size and coef. of friction and everything else, then to draw it on the screen just use screen.blit(img) - it will try to draw the image from the top right, but if you keep everything else about the code the same you can just shift the screen.blit(img) back and up by the radius of the ball and your collision and everything else should still work!

    • @izi989
      @izi989 2 місяці тому +1

      @@lemastertech Thanks a lot!

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

    bro would deifntley like to look at the cocnservetion if momentum and conservatuon of energy

    • @lemastertech
      @lemastertech  Рік тому

      That’s a great idea! Will get working on it

    • @pqb0
      @pqb0 Рік тому

      @@lemastertech dude thats awesome I actually made a model to simulate circular motion with centripetal force from a gravitational field, really fun doe I wanted to implement conservation of momentum but I only managed to simulate collision between planets/ balls by exchanging their velocities (in a perfect collision) but it lead to some weird combinations of bounces.

  • @Intellectualmind4
    @Intellectualmind4 Рік тому +3

    Good content

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

      Thank you, thanks for watching, glad you liked it!

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

    Great video! I'm curious what the id, in the Ball class, could be used for.

    • @lemastertech
      @lemastertech  Рік тому

      Thanks! In my version that I built a few more features into I used that in collision detection so I could update both ball’s directions only once. Since any time you do physics accurate collision detection, both balls would register a collision, you only want to do math once to each to update their new speed vectors so I used ID to check what ids of balls colliding and to not update them twice!

  • @elliottsmith2818
    @elliottsmith2818 9 місяців тому +1

    If I wanted to make a new ball on a button press, and eventually would end up with like 200 balls, do I need to write "button200.update() etc" for each one by hand? Thanks. Really helpful vid btw👍

    • @lemastertech
      @lemastertech  9 місяців тому +1

      Hi you could just add every ball you make to a list and just run a for loop for item in list item.update()!

  • @raging_demon3371
    @raging_demon3371 4 місяці тому +1

    ball has no attribute update_pos i am so confused ive checked everything and i cant figure out whats wrong

    • @lemastertech
      @lemastertech  4 місяці тому

      Maybe you just forgot the parentheses when you call update_pos() ?

  • @MowiWowi
    @MowiWowi 6 місяців тому +1

    Balls huh...nice!

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

      I’ve got the best balls in town!

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

      @@lemastertech yea i rule my towns were i come from..😎

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

    Can you make a tutorial on sprite class

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

      That’s a good idea! I’ll start putting one of those together and might do it for an upcoming tutorial!

    • @cleanmess1400
      @cleanmess1400 Рік тому

      @@lemastertech thank you

  • @sophzart
    @sophzart 4 місяці тому

    please use elif instead of else if in your check_gravity method

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

    Hey, thanks for the video! Very informative! I do have a question. I'm working on a platformer (using a coding style similar to @CodingwithRuss), and I want to add a ball for the player to push around. I, now, want the ball to roll if it gets too close to an edge of a ground tile. Is there a way to implement this (preferably without drastically changing too much code)?

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

      Hi! Thanks for watching and I love @codingwithruss ‘s channel and know the series you’re talking about! I think the best way to put a pushable ball into the game would be to add a ball class that has similar collision mechanics to the player in terms of ground and gravity, then just make it to where the ball has an x velocity of zero unless the player rect is colliding with the rect for the ball! I hope this helps - sounds like a fun project! Good luck :)

  • @Biokompott
    @Biokompott 2 місяці тому +1

    Nice, but why not use pymunk?

    • @lemastertech
      @lemastertech  2 місяці тому

      Mostly cuz I’m not that familiar with pymunk! But I also enjoy figuring modules out for myself unless it’s like ridiculous amounts of work haha

  • @ClassicCase
    @ClassicCase Місяць тому +1

    My balls list is a left/right boolean.

    • @lemastertech
      @lemastertech  29 днів тому

      How could the list be a Boolean?

    • @lol-be6kz
      @lol-be6kz 16 днів тому

      It means that 🏀 🚀 🏀

  • @sr.tomate3818
    @sr.tomate3818 Рік тому +2

    thanks

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

      You’re welcome, glad you liked it, thanks for watching!

  • @Natura325
    @Natura325 Рік тому

    Un bellissimo video, grazie

    • @lemastertech
      @lemastertech  Рік тому

      grazie mille per la visione e per il bel commento!

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

    wowwwwwwww!

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

      Hope you liked it! Thanks for watching!!

  • @tommasotettamanti500
    @tommasotettamanti500 3 місяці тому +1

    gravity = .5
    stop_ball = 1.5
    # GOAL
    # far cadere la pallina e, attraverso la gravità fermarla
    class Ball:
    def __init__(self, color, radius, x_pos, y_pos, x_speed, y_speed, brake):
    self.color = color
    self.radius = radius
    self.x_pos = x_pos
    self.y_pos = y_pos
    self.x_speed = x_speed
    self.y_speed = y_speed
    self.brake = brake
    self.in_air = True
    def draw(self):
    pygame.draw.circle(wn, self.color, (int(self.x_pos), int(self.y_pos)), self.radius)
    def manage_forces_ball(self):
    if self.y_pos < WN_SIZE[1] - self.radius:
    self.y_speed += gravity
    else:
    self.y_pos = WN_SIZE[1] - self.radius
    # Se la velocità verticale è sopra la soglia, applica il freno
    if self.y_speed > stop_ball:
    self.y_speed *= -self.brake
    else:
    # Anche la velocità orizzontale può essere fermata se molto bassa
    if abs(self.y_speed)

    • @lemastertech
      @lemastertech  3 місяці тому +1

      Hi I’m sorry I really don’t have the time to throw this code into an IDE and troubleshoot for you! You could always add some code where if the ball gets below a certain speed just stop it!

    • @tommasotettamanti500
      @tommasotettamanti500 3 місяці тому

      @@lemastertech fixed it, thank you anyway