2

I prepared a few hitboxes around certain enemies. These hitboxes are white. When they come into contact with a certain color, for example, red, it would trigger an if statement. I have heard pygame.mask is useful in this situation.

The Code:

# Setting a display width and height and then creating it
display_width = 700
display_height = 500
display_size = [display_width, display_height]
game_display = pygame.display.set_mode(display_size)
intro_display = pygame.display.set_mode(display_size)

spaceship = pygame.image.load("spaceship2.png")
blue_enemy = pygame.image.load("blue_enemy.png")
green_enemy = pygame.image.load("green_enemy.png")
orange_enemy = pygame.image.load("orange_enemy.png")
pink_enemy = pygame.image.load("pink_enemy.png")
yellow_enemy = pygame.image.load("yellow_enemy.png")

# Creating a way to add text to the screen
def message(sentence, color, x, y, font_type, display):
    sentence = font_type.render(str.encode(sentence), True, color)
    display.blit(sentence, [x, y])

# Creating a loop to keep program running
    while True:
        # --- Event Processing and controls
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    spaceship_x_change = 10
                elif event.key == pygame.K_LEFT:
                    spaceship_x_change = -10
            elif event.type == pygame.KEYUP:
                spaceship_x_change = 0

message(str(blue_enemy_health), white, 65, 10, font, game_display)
        game_display.blit(blue_enemy, (20, 25))
        blue_hit_box = pygame.draw.rect(game_display, white, [30, 35, 80, 70], 1)

        message(str(green_enemy_health), white, 203, 10, font, game_display)
        game_display.blit(green_enemy, (160, 25))
        green_hit_box = pygame.draw.rect(game_display, white, [180, 35, 60, 70], 1)

        message(str(orange_enemy_health), white, 341, 10, font, game_display)
        game_display.blit(orange_enemy, (300, 25))
        orange_hit_box = pygame.draw.rect(game_display, white, [315, 43, 65, 70], 1)

        message(str(pink_enemy_health), white, 496, 10, font, game_display)
        game_display.blit(pink_enemy, (440, 25))
        pink_hit_box = pygame.draw.rect(game_display, white, [460, 35, 90, 70], 1)

        message(str(yellow_enemy_health), white, 623, 10, font, game_display)
        game_display.blit(yellow_enemy, (580, 25))
        yellow_hit_box = pygame.draw.rect(game_display, white, [590, 40, 85, 70], 1)

        # Creating a spaceship, lasers, and enemies
        laser = pygame.draw.rect(game_display, red, [spaceship_x + 69, 100, 4, 300])
        game_display.blit(spaceship, (spaceship_x, spaceship_y))

I want it that when the white hitboxes around the enemies to trigger an if event when they come into contact with red. I would like this to be done specifically with pygame.mask. Thanks

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
hmood
  • 603
  • 7
  • 25
  • 1
    *"[...] if event when they come into contact with red"* - What do you mean by "red"? RGB (255, 0, 0)? Or do you mean any red tint? – Rabbid76 Jan 31 '21 at 17:01
  • 1
    For "mask" collision see [Collision between masks in PyGame](https://stackoverflow.com/questions/55817422/collision-between-masks-in-pygame/55818093#55818093) or [Pygame mask collision](https://stackoverflow.com/questions/60077813/pygame-mask-collision/60078039#60078039). – Rabbid76 Jan 31 '21 at 17:04
  • 1
    @Rabbid76 RGB (255, 0, 0) – hmood Jan 31 '21 at 17:25

1 Answers1

0

The mask collision in the Pygame is answered several times. See Mask and the answers to the following questions:

However, you have a special case where you want to create a mask from the red color (255, 0, 0).


To create a mask from a single color, create an image with a format without per pixel alpha with pygame.Surface.convert and set the transparent color key based on the mask color with pygame.Surface.set_colorkey:

mask_image = image.convert()
mask_image.set_colorkey(mask_color)

Create a mask form the image with pygame.mask.from_surface and invert the mask with pygame.mask.Mask.invert:

mask = pygame.mask.from_surface(mask_image)
mask.invert()

Minimal example:

import pygame

def ColorMask(image, mask_color):
    mask_image = image.convert()
    mask_image.set_colorkey(mask_color)
    mask = pygame.mask.from_surface(mask_image)
    mask.invert()
    return mask

pygame.init()
window = pygame.display.set_mode((450, 250))

test_image = pygame.Surface((200, 200), pygame.SRCALPHA)
pygame.draw.circle(test_image, (255, 0, 0), (70, 70), 70)
pygame.draw.circle(test_image, (0, 255, 0), (130, 70), 70)
pygame.draw.circle(test_image, (0, 0, 255), (70, 130), 70)
pygame.draw.circle(test_image, (255, 255, 255), (130, 130), 70)

mask = ColorMask(test_image, (255, 0, 0))

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.fill(0)
    window.blit(test_image, (25, 25))
    window.blit(mask.to_surface(), (250, 25))
    pygame.display.flip()

pygame.quit()
exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thanks! This looks like something i can use to finish my program! – hmood Jan 31 '21 at 17:45
  • Can you help "fit" this in my program? For example when RGB(255, 0, 0) comes into contact with `blue_enemy_hit_box`, which is RGB(255, 255, 255), `blue_enemy_health` lowers by one. Thanks – hmood Jan 31 '21 at 18:26
  • @snoopstick Why do you still stick to idea of "color" collision? You don't need this at all. You know that one image is read and the other one is blue. A normal collision will do the job. Don't reinvent the wheel. – Rabbid76 Jan 31 '21 at 18:30
  • I still dont understand how collision works at all. Please help. I am stuck on this program for the past 3 months unable to understand how collision works. Any amount of help is needed. All I want is when the "laser" is shot by the spaceship, and it collides with the hitboxes of an enemy, I want the enemy health to lower by one point. – hmood Jan 31 '21 at 18:40
  • @snoopstick I can't help you if you don't read the answers to questions about collision detection. Should I write the application for you? I don't want that and you wouldn't learn anything. Start reading. e.g. [How do I detect collision in pygame?](https://stackoverflow.com/questions/29640685/how-do-i-detect-collision-in-pygame/65064907#65064907) – Rabbid76 Jan 31 '21 at 18:56
  • Sorry if i seemed to bother you. Thanks again – hmood Jan 31 '21 at 19:06