Pygame Car Racing Tutorial #2 - Pixel Perfect Collision

Sdílet
Vložit
  • čas přidán 23. 07. 2024
  • Welcome back to this tutorial series! In this video, we'll be covering collision, specifically, pixel perfect collision. But first I'll go over some things I missed in the last video.
    💻 AlgoExpert is the coding interview prep platform that I used to ace my Microsoft and Shopify interviews. Check it out and get a discount on the platform using the code "techwithtim" algoexpert.io/techwithtim
    📄 Resources 📄
    Download Image Assets: dev-cms.us-east-1.linodeobjec...
    Code In This Video: github.com/techwithtim/Pygame...
    ⭐️ Timestamps ⭐️
    00:00 | Moving The Car Backwards
    04:03 | Understanding Masks
    08:50 | Collision With The Track
    17:20 | Adding The Finish Line
    19:38 | Collision With The Finish Line
    📚 Playlist: • Pygame Car Racing Tuto...
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    💰 Courses & Merch 💰
    💻 The Fundamentals of Programming w/ Python: tech-with-tim.teachable.com/p...
    👕 Merchandise: teespring.com/stores/tech-wit...
    🔗 Social Medias 🔗
    📸 Instagram: / tech_with_tim
    📱 Twitter: / techwithtimm
    ⭐ Discord: / discord
    📝 LinkedIn: / tim-ruscica-82631b179
    🌎 Website: techwithtim.net
    📂 GitHub: github.com/techwithtim
    🔊 Podcast: anchor.fm/tech-with-tim
    🎬 My CZcams Gear 🎬
    🎥 Main Camera (EOS Canon 90D): amzn.to/3cY23y9
    🎥 Secondary Camera (Panasonic Lumix G7): amzn.to/3fl2iEV
    📹 Main Lens (EFS 24mm f/2.8): amzn.to/2Yuol5r
    🕹 Tripod: amzn.to/3hpSprv
    🎤 Main Microphone (Rode NT1): amzn.to/2HrZxXc
    🎤 Secondary Microphone (Synco Wireless Lapel System): amzn.to/3e07Swl
    🎤 Third Microphone (Rode NTG4+): amzn.to/3oi0v8Z
    ☀️ Lights: amzn.to/2ApeiXr
    ⌨ Keyboard (Daskeyboard 4Q): amzn.to/2YpN5vm
    🖱 Mouse (Logitech MX Master): amzn.to/2HsmRDN
    📸 Webcam (Logitech 1080p Pro): amzn.to/2B2IXcQ
    📢 Speaker (Beats Pill): amzn.to/2XYc5ef
    🎧 Headphones (Bose Quiet Comfort 35): amzn.to/2MWbl3e
    🌞 Lamp (BenQ E-reading Lamp): amzn.to/3e0UCr8
    🌞 Secondary Lamp (BenQ Screenbar Plus): amzn.to/30Dtafi
    💻 Monitor (BenQ EX2780Q): amzn.to/2HsmUPZ
    💻 Monitor (LG Ultrawide 34WN750): amzn.to/3dSD7tS
    🎙 Mic Boom Arm (Rode PSA 1): amzn.to/30EZw9m
    🎚 Audio Interface (Focusrite Scarlet 4i4): amzn.to/2TjXsih
    💸 Donations 💸
    💵 One-Time Donations: www.paypal.com/donate?hosted_...
    💰 Patreon: / techwithtim
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    ⭐️ Tags ⭐️
    - Tech With Tim
    - Pygame
    - Masks
    - Collision
    - Pixel Perfect Collision
    - Algoexpert
    ⭐️ Hashtags ⭐️
    #TechWithTim #Collision

Komentáře • 59

  • @DjNOiz3
    @DjNOiz3 Před 2 lety +2

    Your tutorials are awesome! They always help me keeping my python in mind and i always learn something new from your videos!

  • @harshverma6115
    @harshverma6115 Před 2 lety +2

    Thanks for the tutorial! Love your videos and stay safe and happy!

  • @TheDavidlloydjones
    @TheDavidlloydjones Před 2 lety +1

    Tim,
    Fine work. Well done and thank you!

  • @DevMadeEasy
    @DevMadeEasy Před 2 lety +4

    Hey 👑Tech With Tim,
    Pygame Car Racing Tutorial #2 - Pixel Perfect Collision?
    🤩Wow, Great content, as always!!!
    I just 💜loved it.
    Thanks for sharing it!🙏

  • @abdultheseekerofknowledge4453

    My favorite Python teacher

  • @dilemaedits
    @dilemaedits Před 2 lety +2

    Thanks for the cool python videos 🥰🥰

  • @GoogleSoftwareEnginear
    @GoogleSoftwareEnginear Před 8 měsíci

    It was an awesome game sir Thank you so much for giving the wonderful project that helps to learn new things about technology...😊😊😊🎉🎉

  • @Lekar1996
    @Lekar1996 Před 2 lety +1

    perfect. I like this tutorial)

  • @joningram
    @joningram Před 2 lety +9

    I haven't watched the 3rd and 4th videos yet, but I believe the collision detection is glitchy because you are looking at the mask of the original car image, rather than the rotated car image that's actually moving. One way to fix this is to change your 'blit_rotate_center' function into a 'rotate_center' function that returns the rotated image, and then compare the mask of the track to the mask of the rotated image. Perhaps this is something you discuss in a later video. In the mean time I enjoyed fixing it myself!

    • @sidhaksingh7675
      @sidhaksingh7675 Před 2 lety +1

      I think the mask works on the rectangle holding the car image. Since we are providing the offset to the top left of the rectangle, it will automatically detect the pixels of the rotated car and hence the collison. I don't know if I'm right but it the collison works perfect for the original car image

    • @cuencaguy9799
      @cuencaguy9799 Před 2 lety

      I noticed this problem as well. This sounds easy enough to do, but I'm struggling with how to do this. The errors I'm getting are when I try to blit the rotated image in the draw function, so I'm not sure if I changed the rotate_center function correctly either.

    • @MoradSayed
      @MoradSayed Před rokem

      Can you pass the code if u still have it, as I have a problem while trying to use the rotated image as the mask

  • @revanthgsr
    @revanthgsr Před 2 lety

    Nice editing skills 👍🏻

  • @BeRaikwal
    @BeRaikwal Před 2 lety

    Everyone: What's the next video title?
    Tim: PIXEL PERFECTION GOES BRRRRRRRRRRRRRRRRR
    AMAZING CONTENT AS USUAL....

  • @Jeff--vq4zb
    @Jeff--vq4zb Před rokem

    I used Blitz Basic years ago and I thought masks were magical, but here you say that they are an array of values. Now I know the truth :D

  • @acivilian.797
    @acivilian.797 Před 2 lety +1

    Very nice!

  • @ivanmironov599
    @ivanmironov599 Před rokem +3

    It should be noted that the direction you turn when reversing is incorrect; when you turn right on the steering wheel, the car will move to the right and vice-versa. So far, what I have found that worked for me was creating a new method under the 'rotate' method that reversed it:
    def reverse_rotate(self, left=False, right=False):
    if left:
    self.angle -= self.rotation_vel
    elif right:
    self.angle += self.rotation_vel
    I then changed my "pressed keys" code block to this:
    if keys[pygame.K_w]:
    moved = True
    player_car.move_forward()
    if keys[pygame.K_a]:
    player_car.rotate(left = True)
    if keys[pygame.K_d]:
    player_car.rotate(right = True)
    if keys[pygame.K_s]:
    moved = True
    player_car.move_backward()
    if keys[pygame.K_a]:
    player_car.reverse_rotate(left = True)
    if keys[pygame.K_d]:
    player_car.reverse_rotate(right = True)
    if not moved:
    player_car.reduce_speed()
    if keys[pygame.K_a]:
    player_car.rotate(left = True)
    if keys[pygame.K_d]:
    player_car.rotate(right = True)

  • @asplelei2629
    @asplelei2629 Před 2 lety +1

    could I make the track scroll with the car using this method? i have tried but the collision is messed up when I do this and its at the wrong place.

  • @Myrslokstok
    @Myrslokstok Před 2 lety

    He is very easy to listen to.

    • @mikyjet065
      @mikyjet065 Před 2 lety

      Yea true, I like how he explains stuff

  • @koeiswit
    @koeiswit Před 2 lety +2

    In case any of you also wants to have the ' reduce_speed' effect when going backwards, change the 'reduce_speed' method to this (or anything similar):
    def reduce_speed(self):
    if self.vel >= 0:
    self.vel = max(self.vel - self.acceleration/2, 0)
    else:
    self.vel = min(self.vel + self.acceleration/2, 0)
    self.move()

    • @ivanmironov599
      @ivanmironov599 Před rokem

      Thank you for this! I found changing the value to /3 gave a more realistic outcome.

  • @mikyjet065
    @mikyjet065 Před 2 lety

    Hi, I was trying to figure out how to make the collision with the wall more realistic in the sense: the angle of incidence is equal to the angle of reflection. But I just can't figure it out, can somebody help me, thank you.

  • @satshah1492
    @satshah1492 Před rokem

    Great video. Could you show us how to make multiple screens in Pygame? For instance adding a main menu, then via a button click, direct us to a different screen. Hope that makes sense. Thanks.

  • @user-gd9iq7kr8y
    @user-gd9iq7kr8y Před 2 lety

    Python is clean, I like it

  • @petrex1505
    @petrex1505 Před 2 lety

    I just wonder, how do we load multiple images for the shortest way?

  • @ulfp8025
    @ulfp8025 Před 2 lety

    for some reason when i putted the def bounce in class PlayerCar it didn't work but tryed to put it in AbstractCar and it worked perfectly for some reason

  • @TheNuttyGamer
    @TheNuttyGamer Před 2 lety +1

    The car collides with the wall too early because the png isnt rotating properly so its detecting the original image as the hitbox for the walls. how do i fix this and whats the code

  • @NeetCode
    @NeetCode Před 2 lety +2

    python gang lets go 😎

  • @henryash413
    @henryash413 Před 2 lety

    I reset the velocity of the car when it bounces off the barriers.

  • @larared6243
    @larared6243 Před rokem

    yo man, when I put the reset() method, on running the program like just before the end of the video, it's like the car is constantly hitting the finish line and printing finish. my code looked exactly like yours by the end of the video. any idea how I can remedy this, maybe something I overlooked? 🤔

  • @AnheiShu
    @AnheiShu Před 9 měsíci

    Hi There, could you hep check if there is any setting which caused this clip does not available on my kid account? He can see #1,3,4 but just not be able to watch #2.

  • @killianward9127
    @killianward9127 Před 2 lety

    What font are you / is he using in this video?

  • @imtiazahmad7826
    @imtiazahmad7826 Před rokem

    Can some one tell me how can i mask only the front part of the car

  • @venkateshakhouri9541
    @venkateshakhouri9541 Před 2 lety

    What if we don't have a track border, just the track, in that case how will we handle collision?

    • @MoradSayed
      @MoradSayed Před rokem

      make the background around the track as your collision mask

  • @cauchy2012
    @cauchy2012 Před 2 lety

    WHT is the role of _int_(self)

  • @yussefhallak981
    @yussefhallak981 Před 2 lety

    Tim I would like to know how can I make it so that the car only rotates when the car is moving forward or backward and not when stopped since that can't happen in real life, I have been trying for more than 2 hours now 😅. PLEASE REPLY

    • @mikyjet065
      @mikyjet065 Před 2 lety

      You probably figured out by now, but in case you did not:
      if keys[pygame.K_a] and player_car.vel != 0:
      player_car.rotate(left=True)
      if keys[pygame.K_d] and player_car.vel != 0:
      player_car.rotate(right=True)

    • @yussefhallak981
      @yussefhallak981 Před 2 lety

      @@mikyjet065 Thank you so much. I was overthinking it way too much and the answer is pretty simple😅

  • @elite__6372
    @elite__6372 Před 2 lety

    what is the colour theme you're using ?

  • @trinib1
    @trinib1 Před 2 lety +10

    he really loves python.. wish he can make more flutter tutorials

  • @LaPatataAtomica
    @LaPatataAtomica Před rokem

    I'm not sure if it's only me, but I find this video extremely confusing when you talk about collision, mask, and rectangles. I think it's because you're not using the subclass Sprite, and because you're trying to check the collision with the mask of the original image

  • @PasaOp
    @PasaOp Před 2 lety

    🙂

  • @killianward9127
    @killianward9127 Před 2 lety

    The logic for turning when reversing is wrong, it should be the opposite to what you said
    Edit: Apart from that, great tutorial

  • @godwinsojan6205
    @godwinsojan6205 Před 2 lety

    Tim, i am Godwin in your video "what is new in python" in that video I have commented you but no replay but now
    can you make your own os with graphics pls pls

  • @redditforecast9442
    @redditforecast9442 Před 2 lety

    Wassup mate

  • @hichellam_iloveu1800
    @hichellam_iloveu1800 Před 2 lety

    Hi sir i have doubt sir about deta base I want to talk with you sir just 5 minutes sir message only sir you clear my doubt it's really helpful for my knowledge I ask many people but all are tell don't know 🙏 plzz sir just 5 mins I want to talk with you sir