如何转换图像的背景颜色以匹配 Pygame 窗口的颜色? [英] How to convert the background color of image to match the color of Pygame window?

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

问题描述

我需要做的是将图像背景的颜色与 Pygame 窗口的颜色相匹配.但是图像和pygame窗口的背景不匹配.看起来像这样

ship.py

导入pygame船级:"管理船舶的班级."def __init__(self, ai_game):"初始化船和起始位置."self.screen = ai_game.screenself.screen_rect = ai_game.screen.get_rect()# 加载船舶图像并获取其矩形.self.image = pygame.image.load('images/ship.bmp')self.rect = self.image.get_rect()# 在屏幕底部中央启动每艘新船.self.rect.midbottom = self.screen_rect.midbottomdef blitme(自我):"在其当前位置绘制船舶."self.screen.blit(self.image, self.rect)

外星人入侵.py

导入系统导入pygame从船舶进口船舶外星人入侵类:"管理游戏资产和行为的整体类.""def __init__(self):"游戏初始化,创建游戏资源."pygame.init()self.screen = pygame.display.set_mode((1200, 800))pygame.display.set_caption(外星人入侵")# 设置背景颜色self.bg_color = (0, 0, 255)self.ship = Ship(self)def run_game(self):"开始游戏的主循环.""为真:# 注意键盘和鼠标事件.对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:系统退出()# 在每次循环时重绘屏幕.self.screen.fill(self.bg_color)self.ship.blitme()# 使最近绘制的屏幕可见.pygame.display.flip()如果 __name__ == '__main__':# 创建一个游戏实例,并运行游戏.ai = AlienInvasion()ai.run_game()

self.image = pygame.image.load('images/ship.png').convert_alpha()

What I need to do is match the color of the image background with the color of Pygame windows. But the background of image and pygame window didn't match. It looks something like this

ship.py

import pygame

class Ship:
    """ A class to manage the ship. """
    
def __init__(self, ai_game):
    """ Initialize the ship and the starting position. """
    self.screen = ai_game.screen
    self.screen_rect = ai_game.screen.get_rect()

    # Load the ship image and get its rect.
    self.image = pygame.image.load('images/ship.bmp')
    self.rect = self.image.get_rect()

    # Start each new ship at the bottom center of the screen.
    self.rect.midbottom = self.screen_rect.midbottom

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

alieninvasion.py

import sys
import pygame
from ship import Ship


class AlienInvasion:
"""Overall class to manage game assets and behavior."""

def __init__(self):
    """Initialize the game, and create game resources."""
    pygame.init()
    self.screen = pygame.display.set_mode((1200, 800))
    pygame.display.set_caption("Alien Invasion")

    # Set background colour
    self.bg_color = (0, 0, 255)
    self.ship = Ship(self)
    
def run_game(self):
"""Start the main loop for the game."""
while True:
    # Watch for keyboard and mouse events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            
    # Redraw the screen during each pass through the loop.
    self.screen.fill(self.bg_color)
    self.ship.blitme()

    # Make the most recently drawn screen visible.
    pygame.display.flip()


if __name__ == '__main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()

I tried the answers from this discussion but I couldn't fix it.

I couldn't understand how to use image.convert_alpha() and image.set_colorkey() and using them in ship.py did not show any changes for me.

Note: ship.py is the class to make changes on ship while alieninvasion.py is the main file.

解决方案

You don't need to change the background color of the image to the background color of the window, but make the background of the image transparent.


Set the transparent colorkey by pygame.Surface.set_colorkey:

Set the current color key for the Surface. When blitting this Surface onto a destination, any pixels that have the same color as the colorkey will be transparent.

Note, all the background has to have exactly the same color. In your case the background color seems to be gray (230, 230, 230):

self.image = pygame.image.load('images/ship.bmp').convert()
self.image.set_colorkey((230, 230, 230))


Another option wound be to create a new image (you need to draw it in a painting application) with per pixel alpha (e.g. PNG) and a transparent background:

self.image = pygame.image.load('images/ship.png').convert_alpha()

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

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