0

I'd like to know if there's a way to blit an image above my grid tiles and keep a gray border (i.e. I'd like to remove the parts of the image that are on top of the gray/blank areas of my grid). I'm trying to avoid having individual images for each grid tile, thanks!

top: with image blit... bottom: without image blit

import pygame

TILESIZE = 22
margin = 1
BOARD_POS = (10, 10)


def create_board_surf():
    board_surf = pygame.Surface((TILESIZE * 50 + margin * 51, TILESIZE * 26 + margin * 27))
    board_surf.set_colorkey((0, 0, 0))
    board_surf.set_alpha(250)
    for row in range(26):
        for column in range(0, 25):
            color = (170, 70, 70)
            rect = pygame.draw.rect(screen, color, [(margin + TILESIZE) * column + margin, (margin + TILESIZE) * row + margin, TILESIZE, TILESIZE])
            pygame.draw.rect(board_surf, pygame.Color(color), rect)
        for column in range(25, 50):
            color = (70, 95, 195)
            rect = pygame.draw.rect(screen, color, [(margin + TILESIZE) * column + margin, (margin + TILESIZE) * row + margin, TILESIZE, TILESIZE])
            pygame.draw.rect(board_surf, pygame.Color(color), rect)

    water = pygame.image.load("water_6.png")
    water = pygame.transform.smoothscale(water, (TILESIZE * 60 + margin * 52, TILESIZE * 29 + margin * 27))
    water.set_alpha(40)
    board_surf.blit(water, (-80, -30))

    return board_surf



def create_board():
    board = []
    for row in range(26):
        board.append([])
        for column in range(50):
            board[row].append(None)
    return board


def main():
    global screen
    pygame.init()
    screen = pygame.display.set_mode((1200, 650))
    clock = pygame.time.Clock()

    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return

        create_board()
        board_surf = create_board_surf()
        screen.fill(pygame.Color((200, 200, 200)))
        screen.blit(board_surf, BOARD_POS)
        pygame.display.flip()
        clock.tick(60)


main()

1 Answers1

0

So cropped_region is kind of like drawing rectangles in pygame.

cropped_region = (x, y, width, height)
traget.blit(source_surf, (posx, posy), cropped_region)

source: https://discuss.dizzycoding.com/how-can-i-crop-an-image-with-pygame/

AzlanCoding
  • 209
  • 2
  • 11