How to Use Pygame Masks for Pixel Perfect Collision

  Рет қаралды 39,495

Coding With Russ

Coding With Russ

Күн бұрын

Пікірлер: 70
@the_foliot
@the_foliot Жыл бұрын
ok this is the best tutorial on masks i've found. so glad i woke to find this tutorial.
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Thanks, glad it was helpful!
@code--breaker
@code--breaker Жыл бұрын
Seriously. I am not speak english but I learned pygame masks and all it is beacuse you.Thanks(maybe I writed wrong sorry.)
@muhammedelzalabany
@muhammedelzalabany 10 ай бұрын
You are one of the best, other programmers explained this in an hour and you in 10 minutes thank you ❤
@Daniel-jv6bf
@Daniel-jv6bf Жыл бұрын
you deserve the oscar, mask collision is exactly what i tried to implement all the time in my games, but always failed, cause I didn't know what to use and how to do it; thanks for stopping my suffering !
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Haha nice, glad to hear it is making sense now.
@jermania7467
@jermania7467 Жыл бұрын
Thanks for tutorial on masks I hadn't really explored them before so this was quite helpful and including the spritecollide logic with the masks. Great content as always!
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Thanks, glad to hear it made sense!
@GundirQuid
@GundirQuid Жыл бұрын
Thank you, stumbled across this video on one of my late night programming sessions and wanted background audio to listen to. I was using the first method resulting in spaghetti code with many small rectangle checks like you explained before masks, as I was unaware of them actually being a pygame feature. So many wasted lines of code. With this updated knowledge, I have cut down my many lines to just a couple of lines, the code is a lot cleaner to read now as well! Thank you!
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Glad it helped! I've used it in a few of my games as well and it's really good when you need more accurate collision
@NandoDevlop
@NandoDevlop Жыл бұрын
I'm glad yt recomend me this, this video is awesome
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Thanks!
@TheSteelJackal
@TheSteelJackal Жыл бұрын
You’re fantastic Russ, thank you for these videos!
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Cheers, glad you like them!
@DineshVaidyaOfficial
@DineshVaidyaOfficial Жыл бұрын
Thank you so much. I didn't find pygame explanation like you do in any other pygame video tutorial. It was quite helpful. Please keep it on. And i hope you will bring more videos related to pygame with nice examples. Thanks once again.
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Thanks Dinesh, glad this one was useful. I have a few more pygame tutorials in progress :)
@DineshVaidyaOfficial
@DineshVaidyaOfficial Жыл бұрын
@@CodingWithRuss Wow! I will be waiting for them. thanks
@alboukhari20011
@alboukhari20011 Жыл бұрын
As always the best lessons for pygame engine , greate Thinks for all courses
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Thanks, glad you liked it.
@CodingWithRuss
@CodingWithRuss Жыл бұрын
For the source code used in this tutorial, head over to my website: www.codingwithruss.com/pygame/how-to-use-pygame-masks-for-pixel-perfect-collision/
@misterelrob
@misterelrob Жыл бұрын
Thank you! Exceptionally clear presentation. You have a new subscriber.
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Thanks! Glad it all made sense.
@lioninaboxgames
@lioninaboxgames 11 ай бұрын
This is an amazing tutorial! I wonder which of the two methods are faster and more efficient for getting collision?
@gfgdfgd39
@gfgdfgd39 6 ай бұрын
How do I check for collision within it's own sprite group? So one sprite in group A and another sprite also in the same group.
@lost_hope813
@lost_hope813 8 ай бұрын
thank you so much! this tutorial helps me to fix my game!😀
@SlowPursuit
@SlowPursuit Жыл бұрын
Excellent work. BTW, as a little "request", do you have any work on 'slopes' for ground? Ive gone through your (great) platformers & OOP'd these with seperate (file) classes, but was wondering about (ground) collision with ramps or slopes- this would make a nice scroller through say a forest (type of scene- following a pathway with up & down gradients), or think of a BMX style game with ramps & jumps (jumps a bit of physics tho!). Hope that makes sense!
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Hi, that's a really good idea for a game! I haven't played around with sloped terrain before but it would be interesting to try. I think it could be quite tricky to implement from scratch although a physics library like pymunk would make it much easier. Thanks for the suggestion, I've added it to my list, although I've already planned out the topics for the next few weeks so this would be something for later down the line.
@DaveScurlock
@DaveScurlock Жыл бұрын
Presumably the second method is better if you have a large number of sprites, so that you can take advantage of sprite groups?
@DaveScurlock
@DaveScurlock Жыл бұрын
Thanks for a great video btw!
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Yes exactly. Something that you create a lot of instances of and stick into a sprite group would be better suited to the second method.
@Qwertyuio010
@Qwertyuio010 4 ай бұрын
thanks you for the turorial, but I still have a question, how to use the masks in the full platformer game
@omkarjadhav6049
@omkarjadhav6049 Жыл бұрын
Really awesome tutorial Russ😍. I am confused 😕 about the optimization of mask collision at 8:55 onwards since both the statements will be executing though??
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Thanks! The optimisation means that the mask check is only executed when there is a rectangle collision, which is quicker and less demanding. So it means that each loop you check for a rectangle collision instead of a mask collision. Only if you get a rect collision, do you then go onto the more demanding check
@omkarjadhav6049
@omkarjadhav6049 Жыл бұрын
@@CodingWithRuss Got It 👍😊
@omkarjadhav6049
@omkarjadhav6049 Жыл бұрын
Thanks 👍
@SIJTTY_blueski
@SIJTTY_blueski 9 күн бұрын
​@@CodingWithRuss and I'd also note that you could create check_collision_function for your player class and use it to check collision with soldier or bullet rectangle / mask, instead of checking every collision with every object. Similar for the projectile class, it shouldn't check for player if its fired by player, and shouldn't check for soldier if its fired by soldier ( if u dont want to make that a feature). In a different video i saw someone add special class for bullets and then including that class in attack function of character, which basically means bullets are going to be personalised for each character class. Just giving a little info i saw. Also thanks for the video, gave me another perspective on that function, reducing checks would be really neat.
@Nitenix
@Nitenix Жыл бұрын
Hi, I love your videos but there’s a bit of code I’m stuck on implementing and I was wondering if there’s any other way I could contact you in order to send it to you
@jakeaustin901
@jakeaustin901 10 ай бұрын
Why don't most games (especially fighting games) use such a feature)?
@KeiS14
@KeiS14 9 ай бұрын
Skinny characters will get an advantage that’s difficult to account for during the game balancing process
@V0LTR0N
@V0LTR0N 10 ай бұрын
i wanted to ask a question. when i try to get a rectangle from my image its always too big and i cant seem to find a way to reduce its size without making errors. the big rectangle means that its difficult to detect collisions as it not accurate. Can you please help?
@CodingWithRuss
@CodingWithRuss 10 ай бұрын
It could be that your image has a lot of transparent space around it, which makes it big. You could edit the image to remove the transparent space. Alternatively you can manually define the rectangle instead of using get_rect(). You can define a rectangle using the Rect() class, which you can find here: www.pygame.org/docs/ref/rect.html
@ShreshthoSaha
@ShreshthoSaha Жыл бұрын
Hello sir! I've been watching your tutorials since September 2022. Your videos are really simple and easy to understand. I've learned a lot from those tutorials. I've a problem...when I animate a sprite and check collision between the sprite and an another sprite, the collision happens between the first image of the animating sprite not the other images(frames)! So what's the solution of it?
@CodingWithRuss
@CodingWithRuss Жыл бұрын
I haven't tried but I expect you would just need to update the mask each time you change frames in the animation so then the mask will match the image each time.
@ShreshthoSaha
@ShreshthoSaha Жыл бұрын
@@CodingWithRuss you mean animation for the masks too?
@digimonk-ym7br
@digimonk-ym7br Жыл бұрын
thanks, you earned a subscriber
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Cheers!
@michalel
@michalel Жыл бұрын
Thank you so much for this tutorial! Now my game isn't full of rectangles xd
@aarohimudholkar6898
@aarohimudholkar6898 8 ай бұрын
Hey! great video and very helpful series. I'm learning very efficiently. One question - what and how does get_rect work?
@metrowh4le480
@metrowh4le480 Жыл бұрын
Is it possible to check collision between two sprite groups using masks?
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Yes, you could use the groupcollide() method. I haven't used it but you can find details of it in the docs here: www.pygame.org/docs/ref/sprite.html#pygame.sprite.groupcollide
@droycesendari
@droycesendari Жыл бұрын
Great tutorial!
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Thanks!
@aronzavoronok8983
@aronzavoronok8983 Жыл бұрын
can i do it but inside a class?
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Yes I don't see why not.
@ARIADNARB
@ARIADNARB Жыл бұрын
how can I get pos if I have two oject mooving fandomly? Not specified with mouse
@CodingWithRuss
@CodingWithRuss Жыл бұрын
pos is the x and y coordinate so if you have objects on the screen then you can use their x and y coordinates instead
@ARIADNARB
@ARIADNARB Жыл бұрын
@Coding With Russ so I can get pos from player_rect and enemy_rect jf I had randomly moving object player and enemy on screen? Do I can het x and y from mask of this objects? Thank you much for answer, you are doing great job with this vodeo - I can create a own game with my 13 old brother! He is happy to do this!
@khargos7602
@khargos7602 7 ай бұрын
👍
@kydung5378
@kydung5378 7 ай бұрын
Amazing :3333
@omarsplaytimechannel
@omarsplaytimechannel Жыл бұрын
5:37 my mask is zoomed in.
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Are you sure you followed the code correctly? Check the link in the description to the full source code that you can check for reference.
@boxhead-zk7sn
@boxhead-zk7sn Жыл бұрын
sorry but i have pygame.surface(10, 10) ^^^^^^^^^^^^^^^^^^^^^^ TypeError: 'module' object is not callable
@moonlight-hb5df
@moonlight-hb5df 3 ай бұрын
Capitalize Surface
@zennihilator3724
@zennihilator3724 Жыл бұрын
first
@CodingWithRuss
@CodingWithRuss Жыл бұрын
:)
@АнтонНазарук-щ5с
@АнтонНазарук-щ5с Жыл бұрын
Hello! Thanks for another brilliant lesson! May I wounder, what is the line "pygame.sprite.Sprite.__init__(self)" do ? Never seen before __init__ inside another __init__
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Thanks Anton, glad you liked it! This is because the Soldier and Bullet classes are being created as child classes of the Sprite class (which already exists in Pygame). This allows me to "inherit" the existing functionality of the Sprite class into the two child classes, but to do this, I need to call the __init__ of the parent class inside them. Difficult to explain in a youtube comment :) So the pygame documentation may be more helpful: www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite
@АнтонНазарук-щ5с
@АнтонНазарук-щ5с Жыл бұрын
@@CodingWithRuss Thanks you for a detailed reply
@usmanamjad1409
@usmanamjad1409 Жыл бұрын
👍got a subscriber🥳
@CodingWithRuss
@CodingWithRuss Жыл бұрын
Thanks!
Getting Multi Line Text Input In Pygame
6:52
Coding With Russ
Рет қаралды 9 М.
Masks (and Pixel-Perfect Collisions) - Pygame Tutorial
17:35
DaFluffyPotato
Рет қаралды 21 М.
風船をキャッチしろ!🎈 Balloon catch Challenges
00:57
はじめしゃちょー(hajime)
Рет қаралды 60 МЛН
ЛУЧШИЙ ФОКУС + секрет! #shorts
00:12
Роман Magic
Рет қаралды 31 МЛН
Кто круче, как думаешь?
00:44
МЯТНАЯ ФАНТА
Рет қаралды 2,8 МЛН
小路飞还不知道他把路飞给擦没有了 #路飞#海贼王
00:32
路飞与唐舞桐
Рет қаралды 77 МЛН
Collisions in Pygame - Beginner Tutorial
10:59
Coding With Russ
Рет қаралды 51 М.
2 YEARS of PYTHON Game Development in 5 Minutes!
4:54
Coding With Russ
Рет қаралды 957 М.
Understanding Pygame masks
1:00:29
Clear Code
Рет қаралды 41 М.
Collisions in Pygame!
8:35
Max Rohowsky (Max on Tech)
Рет қаралды 37 М.
Python Pygame Tutorial - Pixel Perfect Collision Using Mask
9:45
How to Code (almost) Any Feature
9:48
DaFluffyPotato
Рет қаралды 696 М.
Get Started in Pygame in 10 minutes!
10:19
Coding With Russ
Рет қаралды 361 М.
Collisions between moving objects
52:35
Clear Code
Рет қаралды 29 М.
Shaders - Pygame & ModernGL Tutorial
46:10
DaFluffyPotato
Рет қаралды 34 М.
風船をキャッチしろ!🎈 Balloon catch Challenges
00:57
はじめしゃちょー(hajime)
Рет қаралды 60 МЛН