Understanding Pygame masks

A tutorial on how to use masks in pygame. It can be used for more advanced collisions or filling surfaces/giving them outlines.
If you want to support me: / clearcode
(You also get lots of perks)
Social stuff:
Twitter - / clear_coder
Discord - / discord
Timestamps:
0:00:00 - What are masks?
0:03:05 - Masks with sprite collisions
0:14:53 - Mask collisions without sprites
0:28:15 - Filling a surface / creating an outline
0:50:39 - Coloring overlapping areas with masks
Project files:
github.com/clear-code-project...

Пікірлер: 54

  • @raydin9485
    @raydin94852 жыл бұрын

    I think that a more performance friendly way of filling the surfaces with color would be to use the "special_flags" argument of Surface.fill() as it will only fill non-transparent pixels with the color that you want. Iterating pixel by pixel might be slow in the context of a bigger game. Great video on masks!

  • @ClearCode

    @ClearCode

    2 жыл бұрын

    oh damn, I totally forgot that fill also has special flags... that would have been very useful :D

  • @StarFury2

    @StarFury2

    Жыл бұрын

    If some are confused what guys are talking about: new_obstacle_surf.set_colorkey((0, 0, 0)) new_obstacle_surf.fill('orange', special_flags=pygame.BLEND_RGBA_MULT) does the same thing as that 'for' loop with 'get_at' and 'set_at' methods, but much more efficiently. Though latter gives you clue about useful get_at / set_at methods.

  • @wouterbosch4571

    @wouterbosch4571

    Жыл бұрын

    I dont understand what exactly you would put down for this as the special_flags argument to achieve this. Could you explain?

  • @alwaysalegend63
    @alwaysalegend632 жыл бұрын

    We want more of this kind of game Dev people

  • @theadventurousprogrammer6449
    @theadventurousprogrammer64492 жыл бұрын

    How do you not have millions of views... This course was amazing! If you did more content, I'd for sure buy your course.

  • @EpicGamerX
    @EpicGamerX2 жыл бұрын

    Hey you haven't uploaded in a month but I just wanted to say your tutorials are perfect! Don't give up I am sure your content will be noticed, because you actually explain the code rather than have us remembering and your explanations are excellent!

  • @subarunatsuki1902

    @subarunatsuki1902

    2 жыл бұрын

    Yeah, he is awesome!

  • @mikechavannes5357
    @mikechavannes5357 Жыл бұрын

    This is one of the best videos I have seen on masks in pygame. Thank you very much for putting this together. You explained the concepts extremely well!

  • @umanglunia2194
    @umanglunia21942 жыл бұрын

    47:40 - we could also enlarge the mask surface/shrink the original surface and blit with the same center, and that should work I think Great video Clear Code... You are one of my favorite youtubers :)

  • @ClearCode

    @ClearCode

    2 жыл бұрын

    good idea to enlarge the surface although that would also remove holes inside the surface. Super glad you like it :)

  • @marcusberge8378
    @marcusberge83782 жыл бұрын

    Its actually scary how much bettert than every other youtuber, you are at explaining pygame problems!

  • @kyawmying224
    @kyawmying2245 ай бұрын

    last two min is really difficult part.Thank you so much.Your video is really great

  • @iammaxpetrov
    @iammaxpetrov Жыл бұрын

    Great guide, an interesting topic very well explained. Thank you very much for this, you did an amazing job!

  • @jimmyslaughter6262
    @jimmyslaughter62622 жыл бұрын

    Your tutorials are amazing!! Can you do a tutorial on dialog boxes for text based cutscenes? Like in RPG/visual novel games?

  • @Kokoz40
    @Kokoz402 жыл бұрын

    Quick question: How did you do that at 20:16 where you wrote "offset_" on both sides?? Btw awesome videos, I really enjoy them. Very very helpful too. Especially for a self-taught like me. Keep up the great work and wish you all the best :)

  • @ZimmoYT
    @ZimmoYT2 жыл бұрын

    Great video

  • @param4722
    @param47222 жыл бұрын

    Finally a new vid

  • @SkyFly19853
    @SkyFly198532 жыл бұрын

    Very useful.

  • @elainemisteria8309
    @elainemisteria83092 жыл бұрын

    THANK YOU

  • @sylvanfranklin6904
    @sylvanfranklin69042 жыл бұрын

    I really wish you got more attention, this content is not 30k- it’s 1mill at least

  • @doiman6591
    @doiman65912 жыл бұрын

    О, спасибо тебе за видео! Смотрел у тебя The ultimate introduction to Pygame и это очень крутой видос, спасибо тебе за него И да, я знаю, что ты не говоришь по-русски, мне просто хочется написать этот комментарий на родном языке =)

  • @ZgavY

    @ZgavY

    2 жыл бұрын

    Privet

  • @Foxtro

    @Foxtro

    2 жыл бұрын

    lol thanks to yt translation I can read your comment

  • @big123lak
    @big123lak4 ай бұрын

    So if you take the difference of the top left points of the images before the mask , then if any coordinates of one mask added by the offset equals any point on the other mask it’s overlap after 3 hours this is only logical conclusion I can come too but if the original image has a lot of transparency and one is much smaller it still feels like it doesn’t make sense if it already knows the masks coordinates all the offset is the difference of top left which always equals so how do you calculate which coordinates are within overlap idk all I know is add the masks in overlap and the difference in coordinates

  • @michaelhall2265
    @michaelhall226510 ай бұрын

    In part 1 when you add if "pygame.sprite.spritecollide(player.sprite,obstacle,False): (rect detection)" it stops working on the left side of the a.

  • @dakshdubey3852
    @dakshdubey38522 жыл бұрын

    Hi Clear Code I just wanted to ask so is the whole "shape" of the mask a rectangle with 1s and 0s? By the way your vid is awesome!

  • @ClearCode

    @ClearCode

    2 жыл бұрын

    yes, the whole shape is still a rectangle, and thank you :)

  • @dakshdubey3852

    @dakshdubey3852

    2 жыл бұрын

    @@ClearCode Thank you so much, you helped me with my code!!!!

  • @TasteDaRDX
    @TasteDaRDX2 жыл бұрын

    Hello Christian, thanks for another excellent video. I combined the masks with your Grand Theft game to keep the car inside the track, which got buggy when the player rotates. How do I fix it? Thanks again

  • @Choco794

    @Choco794

    5 ай бұрын

    You would have to create a new mask whenever the player rotates which could be quite performance-intensive. Hope this helps

  • @Cookie-mv2hg
    @Cookie-mv2hg2 жыл бұрын

    Hi, I was once making a game where I stopped at trying to identify if the screen is colored(other than white) per frame. I was thinking iterate through each pixels per frame but it's making game super heavy. Can this be done in pygame too?

  • @subarunatsuki1902

    @subarunatsuki1902

    2 жыл бұрын

    Did you find the answer?

  • @pascalbouillon1713
    @pascalbouillon17132 жыл бұрын

    How can I have Collisions between a mask and a spritegroup ( Its An Enemy and Bullets)

  • @muhammadreyad1663
    @muhammadreyad16632 ай бұрын

    I tried to make player mask collide with masks in a list problem is that the player mask only collides with first mask in the mask list

  • @mohammadkamran9727
    @mohammadkamran97272 жыл бұрын

    hi just wanted to know how do u get/make those neat sprites that you use in ur vids can u make a vid for it thanks! (btw love ur vids ♥)

  • @ClearCode

    @ClearCode

    2 жыл бұрын

    thank you :) Most of the artwork I get from opengameart.org which has a ton of free material (there is an artist called Kenney there that makes really good stuff) and I occasionally also use Adobe Photoshop and Illustrator to make some things myself

  • @mohammadkamran9727

    @mohammadkamran9727

    2 жыл бұрын

    @@ClearCode tysm

  • @mrdc3
    @mrdc32 жыл бұрын

    Clear Code, In your previous videos, I asked some additional questions. Could you answer them?

  • @mrdc3

    @mrdc3

    2 жыл бұрын

    @@TuIipe ok cool! Question 1: how would I add scrolling to the overworld? How would I write save states? How do I make the images and where are the font for the “Level 1, Level 2, etc. and how would I assemble them?

  • @kelsierii4747

    @kelsierii4747

    2 жыл бұрын

    @@mrdc3 Im not the best person to answer these but since no one else has yet Ill try to give you some where to start. 1 I dont know if theres a better way but for scrolling I usually create a variable called scrollX, then, everytime the character is about to leave the screen, I make the character stop walking and add scrollX -speed of the character, at last, for every obejct in the game (enemies, background (it has to be long), partciles) I draw them at their true X +scrollX. 2. I havent done this one in a while but the easiest way I know is to create an empty folder somewhere and save some object with the save information to it, using the pickle library. 3. pygame has font rendering in itself, with fonts that come with pygame, but you can use others you install, Id look for a tutorial but it is rather a simple process to render text on a pygame display. Hope this helped.

  • @mrdc3

    @mrdc3

    2 жыл бұрын

    @@kelsierii4747 But I still dont know which scroll_x variables to put, I already have it defined in level. I could easily copy it, but for the overworld I would need to replace some variables, that is what I am confused about. I am referring to Clear Code’s mario-style platformer

  • @vivat-

    @vivat-

    2 жыл бұрын

    Bloxy, you should join the Clear Code Discord Server! It has people who can help, when you need it. Try it out: (It is in the description of this video)

  • @mrdc3

    @mrdc3

    2 жыл бұрын

    @@vivat- That would be a good option!Will do!

  • @maipaksana1667
    @maipaksana16672 жыл бұрын

    Please make a tutorials of ghost game 🙏

  • @Wall_E-Lego_Edition
    @Wall_E-Lego_Edition8 ай бұрын

    Quick question: I tried implenting the sprite collision into my game, but it is really laggy. Any tips?

  • @ClearCode

    @ClearCode

    8 ай бұрын

    do you mean it got slow after a mask collision? Usually a good idea is to do a sprite collision and then a mask one but if you struggle with sprite collisions something else went wrong entirely.

  • @Wall_E-Lego_Edition

    @Wall_E-Lego_Edition

    8 ай бұрын

    @@ClearCode I think I meant mask collision. But don’t we have to always check for collisions when the player is walking on the tiles?

  • @ClearCode

    @ClearCode

    7 ай бұрын

    oh you want to create a platformer? For the floor you would need a constant collision check but a rect based one should be fine

  • @mohiuddinsyed1684
    @mohiuddinsyed16842 жыл бұрын

    Why not use the walrus operator instead of calling the mask function twice to print it.

  • @CoderCotCo
    @CoderCotCo3 ай бұрын

    28:15

  • @mrdc3
    @mrdc32 жыл бұрын

    Hi

  • @eek9313
    @eek93132 жыл бұрын

    Excuse me☝️, can't we just make a simple application like Facebook with Pygame😅! Please Suggest me...🙏

  • @big123lak
    @big123lak4 ай бұрын

    Jesus Christ the offset is hard to understand the offset makes literally no sense is it just easy to remember to use the difference of the top lefts ?? Then it can calculate the overlap using the masks and difference of the toplefts