python - 32 位 bmp 图像未显示在 pygame 屏幕上

标签 python python-3.x pygame

我正在构建一个游戏,我试图在屏幕的中间底部显示一张小图片。 我不明白为什么我根本看不到图像?

这是我的图片类代码,位于名为 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,效果很好!!!

关于python - 32 位 bmp 图像未显示在 pygame 屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46863143/

相关文章:

python - Python什么时候创建一个新对象

python-3.x - PyCharm 单元测试 : AttributeError: module 'enum' has no attribute 'IntFlag'

python - 如何使我的 pygame 游戏窗口可调整大小?

python - Pygame 碰撞错误

python - 使用正则表达式替换文本文件中的多个条目

Python:Ascii字符<->十进制表示转换

python - 在类中存储集合信息

python-3.x - Python 中 CPU 使用情况的拆分结果

python - 如何在 Pandas DataFrame 散点图中添加图例?

python-2.7 - Pygame 会在显示器外用一个矩形 blit Sprite 吗