我正在构建一个游戏,我试图在屏幕的中间底部显示一个小图片。我不明白为什么我根本看不到图像?
这是我的图片类代码,在一个名为 devil.py 的文件中:
import pygame
class Devil():
def __init__(self, screen):
"""Initialize the devil and set its starting position"""
self.screen=screen
#Load the devil image and get its rect
self.image=pygame.image.load('images/devil.bmp')
self.rect=self.image.get_rect()
self.screen_rect=screen.get_rect()
#Start each new devil at the bottom center of the screen
self.rect.centerx=self.screen_rect.centerx
self.rect.bottom=self.screen_rect.bottom
def blitme(self):
"""Draw the devil at its current location"""
self.screen.blit(self.image,self.rect)
这是我写在另一个文件中的主要代码:
import sys
import pygame
from settings import Settings
from devil import Devil
def run_game():
#Initialize pygame, settings and create a screen object.
pygame.init()
dvs_settings=Settings()
screen=pygame.display.set_mode(
(dvs_settings.screen_width, dvs_settings.screen_height))
pygame.display.set_caption("Devil vs Shitty")
#Make a devil
devil=Devil(screen)
#Start the main loop 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.
screen.fill(dvs_settings.bg_color)
devil.blitme()
#Make the most recently drawn screen visible
pygame.display.flip()
run_game()
这是我在一个名为 settings.py 的文件中的设置类:
class Settings():
"""A Class to store all settings for Alien Invasion."""
def __init__(self):
"""Initialize the game's settings"""
#Screen settings
self.screen_width = 1000
self.screen_height = 600
self.bg_color=(230,230,230)
我在这里找不到我做错了什么。
我发现了问题 - 这是一个非常奇怪的问题:当我编辑图像时,我将其保存为 32 位 bmp 文件(默认选项是 24 位,我心想"我使用的是 32 位 python,我认为它会匹配得更好)。但是当我试图在pygame中显示我的图像时 - 它没有出现。我什么都试过。最后,我尝试再次编辑图像。
现在,我将其保存为 24 位 bmp,效果很好!!