3

In Pygame, I have wrote a Minesweeper clone. However, when I blit the final image stating YOU LOSE or YOU WIN, I get this result:

enter image description here

I'm sure you notice the thick black line surrounding the text. Here is the function in which the image is blitted onto the window:

def play():
    SIZE = (WIDTH, HEIGHT) = (16, 16)
    MINES = 40
    PIXELS_PER_CELL = 30
    pygame.init()
    screen = pygame.display.set_mode((WIDTH * PIXELS_PER_CELL,
                                      HEIGHT * PIXELS_PER_CELL))
    pygame.display.set_caption("PyMines")
    board = create_board(SIZE, MINES)
    board.draw(screen)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif (event.type == pygame.MOUSEBUTTONDOWN and board.is_playing and
                  not board.is_solved):
                board.mouse_handler(event, screen)

        message = None
        if not board.is_playing:
            board.show_mines(screen)
            message = pygame.image.load("images/lose.png").convert_alpha()
        elif board.is_solved:
            message = pygame.image.load("images/win.png").convert_alpha()

        if message:
            message = pygame.transform.scale(message, (screen.get_width(),
                                                       screen.get_height() //
                                                       5))
            screen.blit(message, (0, 0))
        pygame.display.update()

As I am not sure which part of the code you should be looking at, here is the full code.

Another reason why I think this behaviour is so bizarre, is that when I first created PyMines, the image blitted perfectly like so (as you can see, there is a very slight shadow to the text):

enter image description here

This however, is not a optimized version, as after each cycle, the whole board is redrawn (so it takes a very long time on a 16x16 board as shown in the first image, so I used a 9x9 - but the results are the same). Here is the play() function of the original version:

def play():
    SIZE = (WIDTH, HEIGHT) = (9, 9)
    MINES = 10
    PIXELS_PER_CELL = 30
    pygame.init()
    screen = pygame.display.set_mode((WIDTH * PIXELS_PER_CELL,
                                      HEIGHT * PIXELS_PER_CELL))
    pygame.display.set_caption("PyMines")
    board = create_board(SIZE, MINES)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif (event.type == pygame.MOUSEBUTTONDOWN and board.is_playing and
                  not board.is_solved):
                board.mouse_handler(event, screen)

        message = None
        if not board.is_playing:
            board.show_mines()
            message = pygame.image.load("lose.png").convert_alpha()
        elif board.is_solved:
            message = pygame.image.load("win.png").convert_alpha()

        board.draw(screen)
        if message:
            message = pygame.transform.scale(message, (screen.get_width(),
                                                       screen.get_height() //
                                                       5))
            screen.blit(message, (0, 0))
        pygame.display.update()

I would attach a link to the full code, but pastebin is down, so here is the full code for the original game without the strange black line.

EDIT: I have already tried dropping the convert_alpha() and adding convert() or even nothing at all.

.convert():

enter image description here

NOTHING:

enter image description here

Why are all these black lines there, how do I get rid of them and which version (convert/convert_alpha/NOTHING) should I use (and how to decide which one to use).

4

1 回答 1

2

文本有一个带有 Alpha 通道的黑色阴影。在您的原始版本中,您渲染板,然后渲染文本,阴影与板混合。

在修订版中,您先渲染板,然后在其上重复渲染文本。在第一次通过时,它正确渲染,阴影与板混合。在第二遍中,阴影与您已经渲染的阴影混合,形成了一个稍暗的阴影。在下一次通过时,阴影会稍微变暗,依此类推。

如果不严格控制要混合的内容,就无法使用 Alpha 混合。每次渲染文本时,您至少需要渲染文本后面的板子部分,如果不是整个板子的话。

于 2013-11-02T23:25:47.207 回答