How to Use Pygame Masks for Pixel Perfect Collision

Поделиться
HTML-код
  • Опубликовано: 14 окт 2024
  • In this video I will explain how to check for collisions in Pygame using masks.
    I'll go over two different methods which are using the .overlap() and .spritecollide() functions
    I'll also explain how to optimise the collision check to make it run more efficiently.
    For the full source code and step by step explanation head over to my website: www.codingwithr...

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

  • @the_foliot
    @the_foliot Год назад +25

    ok this is the best tutorial on masks i've found. so glad i woke to find this tutorial.

    • @CodingWithRuss
      @CodingWithRuss  Год назад +5

      Thanks, glad it was helpful!

    • @code--breaker
      @code--breaker Год назад +2

      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 9 месяцев назад +12

    You are one of the best, other programmers explained this in an hour and you in 10 minutes thank you ❤

  • @Daniel-jv6bf
    @Daniel-jv6bf Год назад +3

    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 Год назад +11

    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!

  • @GundirQuid
    @GundirQuid Год назад +2

    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

  • @lioninaboxgames
    @lioninaboxgames 10 месяцев назад +1

    This is an amazing tutorial!
    I wonder which of the two methods are faster and more efficient for getting collision?

  • @NandoDevlop
    @NandoDevlop Год назад +2

    I'm glad yt recomend me this, this video is awesome

  • @DineshVaidyaOfficial
    @DineshVaidyaOfficial Год назад +1

    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

  • @TheSteelJackal
    @TheSteelJackal Год назад +3

    You’re fantastic Russ, thank you for these videos!

  • @alboukhari20011
    @alboukhari20011 Год назад +1

    As always the best lessons for pygame engine , greate Thinks for all courses

  • @misterelrob
    @misterelrob Год назад +1

    Thank you! Exceptionally clear presentation. You have a new subscriber.

  • @SlowPersuit
    @SlowPersuit Год назад +3

    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  Год назад +2

      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.

  • @omkarjadhav6049
    @omkarjadhav6049 Год назад +1

    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 👍

  • @CodingWithRuss
    @CodingWithRuss  Год назад +2

    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/

  • @lost_hope813
    @lost_hope813 7 месяцев назад +1

    thank you so much! this tutorial helps me to fix my game!😀

  • @gfgdfgd39
    @gfgdfgd39 5 месяцев назад +1

    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.

  • @aarohimudholkar6898
    @aarohimudholkar6898 7 месяцев назад

    Hey! great video and very helpful series. I'm learning very efficiently. One question - what and how does get_rect work?

  • @Qwertyuio010
    @Qwertyuio010 3 месяца назад

    thanks you for the turorial, but I still have a question, how to use the masks in the full platformer game

  • @DaveScurlock
    @DaveScurlock Год назад +1

    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 Год назад +1

      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.

  • @ShreshthoSaha
    @ShreshthoSaha Год назад +1

    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 Год назад +1

    thanks, you earned a subscriber

  • @michalel
    @michalel 11 месяцев назад

    Thank you so much for this tutorial! Now my game isn't full of rectangles xd

  • @Nitenix
    @Nitenix Год назад +1

    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 9 месяцев назад +4

    Why don't most games (especially fighting games) use such a feature)?

    • @KeiS14
      @KeiS14 8 месяцев назад +2

      Skinny characters will get an advantage that’s difficult to account for during the game balancing process

  • @droycesendari
    @droycesendari Год назад +1

    Great tutorial!

  • @V0LTR0N
    @V0LTR0N 9 месяцев назад +1

    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  9 месяцев назад +1

      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

  • @metrowh4le480
    @metrowh4le480 Год назад +1

    Is it possible to check collision between two sprite groups using masks?

    • @CodingWithRuss
      @CodingWithRuss  Год назад +1

      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

  • @khargos7602
    @khargos7602 6 месяцев назад

    👍

  • @aronzavoronok8983
    @aronzavoronok8983 Год назад +1

    can i do it but inside a class?

  • @ARIADNARB
    @ARIADNARB Год назад

    how can I get pos if I have two oject mooving fandomly? Not specified with mouse

    • @CodingWithRuss
      @CodingWithRuss  Год назад +1

      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!

  • @kydung5378
    @kydung5378 6 месяцев назад

    Amazing :3333

  • @omarsplaytimechannel
    @omarsplaytimechannel Год назад +1

    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

  • @zennihilator3724
    @zennihilator3724 Год назад +1

    first

  • @АнтонНазарук-щ5с
    @АнтонНазарук-щ5с Год назад +1

    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  Год назад +1

      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 Год назад +1

    👍got a subscriber🥳