Pygame Car Racing Tutorial #2 - Pixel Perfect Collision
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
Your tutorials are awesome! They always help me keeping my python in mind and i always learn something new from your videos!
Thanks for the tutorial! Love your videos and stay safe and happy!
Tim,
Fine work. Well done and thank you!
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!🙏
My favorite Python teacher
Thanks for the cool python videos 🥰🥰
It was an awesome game sir Thank you so much for giving the wonderful project that helps to learn new things about technology...😊😊😊🎉🎉
perfect. I like this tutorial)
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!
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
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.
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
Nice editing skills 👍🏻
Everyone: What's the next video title?
Tim: PIXEL PERFECTION GOES BRRRRRRRRRRRRRRRRR
AMAZING CONTENT AS USUAL....
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
Very nice!
@SARA HOTGIRL VLOGS Very Nice!
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)
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.
He is very easy to listen to.
Yea true, I like how he explains stuff
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()
Thank you for this! I found changing the value to /3 gave a more realistic outcome.
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.
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.
Python is clean, I like it
I just wonder, how do we load multiple images for the shortest way?
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
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
python gang lets go 😎
I reset the velocity of the car when it bounces off the barriers.
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? 🤔
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.
What font are you / is he using in this video?
Can some one tell me how can i mask only the front part of the car
What if we don't have a track border, just the track, in that case how will we handle collision?
make the background around the track as your collision mask
WHT is the role of _int_(self)
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
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)
@@mikyjet065 Thank you so much. I was overthinking it way too much and the answer is pretty simple😅
what is the colour theme you're using ?
vs code monokai
he really loves python.. wish he can make more flutter tutorials
You're getting spam too
@@lionlight9514 Like everyone is getting
@@lionlight9514 *You're
@@ivanmironov599 my mistake
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
🙂
The logic for turning when reversing is wrong, it should be the opposite to what you said
Edit: Apart from that, great tutorial
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
Wassup mate
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
Do u need an ambulance? Cause it looks like youre having a stroke right now
Discord....
What’s the question?