如何将图像的背景颜色与 Pygame 的背景颜色匹配? [英] How to match background color of an image with background color of Pygame?

查看:446
本文介绍了如何将图像的背景颜色与 Pygame 的背景颜色匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要制作一个在将图像的背景颜色与屏幕的背景颜色相匹配,反之亦然.我已经将 Pygame 屏幕的背景颜色值设置为蓝色,但是当我在 DrawCharacter 类中执行相同操作时,它只是使用图像的背景颜色(白色)打开屏幕.我可能只是将 bg_color 属性放在我班级的错误位置.

I need to Make a class that draws the character at the center of the screen and match the background color of the image to the background color of the screen, or vice versa. I have already set a value for the background color of Pygame screen to blue, but when I do the same in my DrawCharacter class, it just opens the screen with the background color of the image (white). I might just be placing the bg_color attribute at the wrong place in my class.

game_character.py

game_character.py

import sys
import pygame

class DrawCharacter():
    bg_color = ((0, 0, 255))
    def __init__(self, screen):
        """Initialize the superman and set starting position"""
        self.screen = screen

        # Load image and get rect
        self.image = pygame.image.load("Images/supermen.bmp")
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # Start each new supermen at the center of the screen
        self.rect.centerx = self.screen_rect.centerx
        self.rect.centery = self.screen_rect.centery

    def blitme(self):
        """Draw the superman at its current location"""
        self.screen.blit(self.image, self.rect)

blue_sky.py

blue_sky.py

import  sys
import pygame
from game_character import DrawCharacter

def run_game():
    # Initialize game and make screen object
    pygame.init()
    screen = pygame.display.set_mode((1200, 800))
    pygame.display.set_caption("Blue Sky")
    bg_color = (0, 0, 255)

    # Make superman
    superman = DrawCharacter(screen)

    # Start the main loop for the game
    while True:

        # Check for keyboard and mouse events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        # Redraw screen during each loop pass
        screen.fill(bg_color)
        superman.blitme()
        # make the most recently drawn screen visible
        pygame.display.flip()
    run_game()

我希望图像的背景颜色与 Pygame 屏幕的背景颜色相同,但事实并非如此.第一段代码用于我的类文件,第二段用于 pygame 文件

I expected the background color of the image to be the same as the background color of the Pygame screen, but it is not. The first block of code is for my class file, while the second is for the pygame file

推荐答案

如果图像具有透明背景,请尝试在 game_Character.py 中执行以下操作:

if the image has a transparent background try doing the following in game_Character.py:

# Load image and get rect
    self.image = pygame.image.load("Images/supermen.bmp").convert_alpha()
    self.rect = self.image.get_rect()
    self.screen_rect = screen.get_rect()

如果它有白色背景,你可能想像这样设置颜色键:

If it has a white background you probably want to set the colorkey like so:

    # Load image and get rect
        self.image = pygame.image.load("Images/supermen.bmp").convert()
        self.image.set_colorkey((255, 255, 255))
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

这篇关于如何将图像的背景颜色与 Pygame 的背景颜色匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆