我对编程相当陌生,所以我买了Eric Matthes的书《Python速成课程》。最近,我决定在Pygame中重新创建口袋妖怪战斗系统。到目前为止,我已经建立了一个足够好的框架来启动战斗系统。我选择口袋妖怪喵作为测试对象。我已经有了图片的透明背景,但pygame仍然显示灰色和白色的方块。这是我的主要代码文件:
from settings import Settings
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Pykemon Battle Simulator")
pokemon = Mew(screen)
mixer.music.load("battle_music.mp3")
mixer.music.play(-1)
while True:
screen.fill(ai_settings.bg_color)
pokemon.blitme()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game()
这是我的Mew设置文件:
import pygame
class Mew():
def __init__(self, screen):
"""Initialize the Pokémon Mew and it's location """
self.screen = screen
#Load the pokemon and get it's rect.
self.image = image = pygame.image.load("mew.jpg").convert_alpha()
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
#Start each new Pokemon at the bottom of the screen
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
def blitme(self):
"""Draw the pokemon's current location"""
self.screen.blit(self.image, self.rect)
结果就是这样。如何使图片背面透明
将PNG与convert_alpha()函数一起使用:
问题是图像格式JPEG图像没有alpha通道。您可以通过^{} 设置一个透明的颜色键,但是结果不会让您满意,因为JPEG不是无损的。这意味着由于压缩,颜色将略有变化,颜色键将无法正常工作。例如白色:
使用
set_colorkey()
和类似BMP的无损图像格式:或者使用
PNG
格式。例如:相关问题 更多 >
编程相关推荐