Search code examples
pythonpython-3.xpygameopacitypygame-surface

Surface display able to properly represent opacity, but any other surface cannot


I am trying to make a tic-tac-toe game with pygame. An important thing I want is being able to make my images (eg. X and O) slightly translucent for when my user is only hovering over a grid tile. I also use opacity to visually show whose turn it is.

This is what I have tried:

x_tile = pygame.image.load('x_tile').convert()
x_tile.set_alpha(100)

This works fine when I'm blitting x_tile directly onto the display like this:

# This is for simplicity's sake. The actual blit process is all being done in an infinite loop
screen = pygame.display.set_mode((300, 300))
screen.blit(x_file, x_file.get_rect())

But my game is using another image that represents the grid, and that is what I'm blitting onto. So I'm blitting this board onto the display, then blitting the actual X and O tiles on the board.

screen = pygame.display.set_mode((300, 300))
screen.blit(board, board_rect)
board.blit(x_tile, x_tile.get_rect(center=grid[0].center))  # I have a list of Rects that make a grid on the board image. grid[0] is the top left

When I do it that way, x_tile.set_alpha(100) seems to have no effect and I don't know what to do.

Edit: I am using pygame 2.0.1. I'm on Windows 10.

Here is the entire code

import os
import pygame
from pygame.locals import *

# Game constants
WIN_SIZE = WIN_WIDTH, WIN_HEIGHT = 800, 600
BLACK = 0, 0, 0
WHITE = 255, 255, 255
RED = 255, 0, 0
BLUE = 0, 0, 255


# Game functions
class NoneSound:
    """dummy class for when pygame.mixer did not init 
    and there is no sound available"""
    def play(self): pass


def load_sound(file):
    """loads a sound file, prepares it for play"""
    if not pygame.mixer:
        return NoneSound()
    
    music_to_load = os.path.join('sounds', file)
    try:
        sound = pygame.mixer.Sound(music_to_load)
    except pygame.error as message:
        print('Cannot load following sound:', music_to_load)
        raise SystemExit(message)
    
    return sound


def load_image(file, colorkey=None, size=None):
    """loads image into game"""
    image_to_load = os.path.join('images', file)
    try:
        image = pygame.image.load(image_to_load).convert()
    except pygame.error as message:
        print('Cannot load following image:', image_to_load)
        raise SystemExit(message)
    
    if colorkey is not None:
        if colorkey == -1:
            colorkey = image.get_at((0, 0))
        image.set_colorkey(colorkey, RLEACCEL)

    if size is not None:
        image = pygame.transform.scale(image, size)
    
    return image


# Game class
class TTTVisual:
    """Controls game visuals"""

    def __init__(self, win: pygame.Surface):
        self.win = win
        # Load in game images
        self.board = load_image('board.png', size=(600, 450), colorkey=WHITE)
        self.x_tile = load_image('X_tile.png', size=(100, 100), colorkey=BLACK)
        self.o_tile = load_image('O_tile.png', size=(100, 100), colorkey=BLACK)
        # Translucent for disabled looking tile
        self.x_tile_trans = self.x_tile.copy()
        self.o_tile_trans = self.o_tile.copy()
        self.x_tile_trans.set_alpha(100)
        self.o_tile_trans.set_alpha(100)
        # Used to let user know whose turn it is
        self.x_turn = pygame.transform.scale(self.x_tile, (50, 50))
        self.o_turn = pygame.transform.scale(self.o_tile, (50, 50))
        self.x_turn_trans = pygame.transform.scale(self.x_tile_trans, (50, 50))
        self.o_turn_trans = pygame.transform.scale(self.o_tile_trans, (50, 50))
        
        self.get_rects()
        self.grid = self.setup_grid()

    def get_rects(self):
        """Creates coords for some visual game assets"""
        self.board_rect = self.board.get_rect(
            center=self.win.get_rect().center)
        self.x_turn_rect = self.x_turn.get_rect(top=10, left=10)
        self.o_turn_rect = self.o_turn.get_rect(top=10, left=WIN_WIDTH-60)

    def setup_grid(self):
        grid = []
        left = 0
        top = 150
        row = 0
        for i in range(9):
            if (i != 0) and (i % 3 == 0):
                row += 1
                left = 0
            grid.append(pygame.Rect(left, row*top, 200, 150))
            left += 200

        return grid

    def update_turn_status(self):
        """Updates the X and O tiles on the top left and right to 
        let user know whose turn it is"""
        self.win.blits((
            (self.x_turn_trans, self.x_turn_rect),
            (self.o_turn, self.o_turn_rect)
        ))

    def update_grid(self):
        """Updates board"""
        self.win.blit(self.board, self.board_rect)

        # Here is where you could change board to win and see that the tile changes in opacity
        self.board.blit(self.x_tile_trans, self.x_tile_trans.get_rect(center=self.grid[0].center))

    def update(self):
        self.win.fill(WHITE)
        self.update_turn_status()
        self.update_grid()
        pygame.display.flip()


def main():
    pygame.init()
    win = pygame.display.set_mode(WIN_SIZE)
    tttvisual = TTTVisual(win)
    tttfunc = TTTFunc(tttvisual)
    
    clock = pygame.time.Clock()
    running = True
    while running:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False

        tttvisual.update()
       
    pygame.quit()

if __name__ == "__main__":
    main()


Solution

  • The issue is caused by the line:

    self.board.blit(self.x_tile_trans, self.x_tile_trans.get_rect(center=self.grid[0].center))
    

    You don't blit the image on the display Surface, but on the self.board Surface. When a Surface is blit, it is blended with the target. When you draw on a Surface, it changes permanently. Since you do that over and over again, in every frame, the source Surface appears to by opaque. When you decrease the alpha value (e.g. self.x_tile_trans.set_alpha(5)), a fade in effect will appear.

    Never draw on an image Surface. Always draw on the display Surface. Cleat the display at begin of a frame. Draw the entire scene in each frame and update the display once at the end of the frame.

    class TTTVisual:
        # [...]
    
        def update_grid(self):
            """Updates board"""
            self.win.blit(self.board, self.board_rect)
    
            # Here is where you could change board to win and see that the tile changes in opacity
            x, y = self.grid[0].center
            x += self.board_rect.x
            y += self.board_rect.y
            self.win.blit(self.x_tile_trans, self.x_tile_trans.get_rect(center=(x, y)))
    

    The typical PyGame application loop has to: