Understanding Pygame masks

Поделиться
HTML-код
  • Опубликовано: 15 ноя 2024

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

  • @raydin9485
    @raydin9485 2 года назад +30

    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 года назад +14

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

    • @StarFury2
      @StarFury2 2 года назад +10

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

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

  • @Persona-jt4qv
    @Persona-jt4qv Месяц назад +1

    58:30 In this case, since you want to color only the set bits of a mask, all with the same color, you can simply do it while creating the surface from the mask:
    new_surf = mew_mask.to_surface(setcolor = 'red')
    to obtain the same result without checking manually every pixel of the surface, and for those interested, the Mask.to_surface method has a few useful params:
    - surface: Surface to draw mask onto
    - setsurface: Use this surface color values to draw set bits
    - unsetsurface: Use this surface color values to draw unset bits
    - setcolor: Color to draw set bits
    - unsetcolor: Color to draw unset bits
    - dest: Surface destination of where to position the topleft corner of the mask being drawn
    Btw great video and you genuinely deserve more views

  • @alwaysalegend63
    @alwaysalegend63 2 года назад +9

    We want more of this kind of game Dev people

  • @theadventurousprogrammer6449
    @theadventurousprogrammer6449 2 года назад +5

    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
    @EpicGamerX 2 года назад +4

    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!

  • @kyawmying224
    @kyawmying224 10 месяцев назад

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

  • @marcusberge8378
    @marcusberge8378 2 года назад

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

  • @mikechavannes5357
    @mikechavannes5357 2 года назад

    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
    @umanglunia2194 2 года назад +2

    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 :)

  • @iammaxpetrov
    @iammaxpetrov 2 года назад

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

  • @jimmyslaughter6262
    @jimmyslaughter6262 2 года назад +1

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

  • @Kokoz40
    @Kokoz40 2 года назад +1

    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 :)

  • @sylvanfranklin6904
    @sylvanfranklin6904 2 года назад

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

  • @nialninyesiga-og2fq
    @nialninyesiga-og2fq 2 месяца назад +1

    The Cat in the begining looked like spider-mans pet

  • @doiman6591
    @doiman6591 2 года назад +2

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

    • @ZgavY
      @ZgavY 2 года назад +1

      Privet

    • @Foxtro
      @Foxtro 2 года назад

      lol thanks to yt translation I can read your comment

  • @param4722
    @param4722 2 года назад

    Finally a new vid

  • @big123lak
    @big123lak 10 месяцев назад

    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

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

    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

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

    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.

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

    Hi, I have a mask question. Can masks be used for player cosmetic choices and if they can, would this remove the processing burden associated with masks if the mask was not responsible for detecting collisions at all?

  • @elainemisteria8309
    @elainemisteria8309 2 года назад

    THANK YOU

  • @ZimmoYT
    @ZimmoYT 2 года назад

    Great video

  • @SkyFly19853
    @SkyFly19853 2 года назад

    Very useful.

  • @pascalbouillon1713
    @pascalbouillon1713 2 года назад

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

  • @dakshdubey3852
    @dakshdubey3852 2 года назад

    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
    @TasteDaRDX 2 года назад

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

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

  • @mohiuddinsyed1684
    @mohiuddinsyed1684 2 года назад

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

  • @mrdc3
    @mrdc3 2 года назад +1

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

      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
    @maipaksana1667 2 года назад

    Please make a tutorials of ghost game 🙏

  • @Wall_E-Lego_Edition
    @Wall_E-Lego_Edition Год назад

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

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

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

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

      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

  • @Cookie-mv2hg
    @Cookie-mv2hg 2 года назад

    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?

  • @mohammadkamran9727
    @mohammadkamran9727 2 года назад

    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

  • @eek9313
    @eek9313 2 года назад +1

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

  • @Mjngg02
    @Mjngg02 8 месяцев назад

    28:15

  • @mrdc3
    @mrdc3 2 года назад

    Hi

  • @big123lak
    @big123lak 10 месяцев назад

    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