微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python速成班外星人游戏-用背景.bmp图像替换背景色

如何解决Python速成班外星人游戏-用背景.bmp图像替换背景色

我有一个正在运行的Alien Invasion游戏,该游戏来自Python Crash Course书。但是,我正在应用修改。我试图替换bg_color的值来表示存储在项目文件夹中的背景图片,而不是颜色,即

self.bg_color = pygame.image.load('images/space.bmp')

当前,AlienInvasion类从存储在名为settings.py的Sep文件中的名为settings的类中导入背景色


class Settings:
    """A class to store all settings for Alien Invasion."""
    def __init__(self):
        """Initialize the game's static settings."""
        # Screen settings
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230,230,230)

AlienInvasion是设置游戏的主要游戏类。

class AlienInvasion:
    """Overall class to manage game assets and behavior."""
    def __init__(self):
        """Initialize the game,and create game resources."""
        pygame.init()
        """create a settings object using the imported settings class"""
        self.settings = Settings()
        """initialise the game display using the settings saved within the settings file"""
        self.screen = pygame.display.set_mode((self.settings.screen_width,self.settings.screen_height))
        """ assign the imported ship attributes to a new ship instance - "Self" in the call references to a call of AlienInvasion as a class """
        self.ship = Ship(self)
        """ run the game in full screen mode """
        pygame.display.set_caption("Alien Invasion")
        # Set the background color.
        self.bg_color = (230,230)

Update_screen随着游戏的进行不断更新屏幕并在运行游戏时处理背景色

def _update_screen(self):
            """Update the images on the screen and flip to the new screen"""  
            # Redraw the screen during each pass through the loop. Use the settings file here
            self.screen.fill(self.settings.bg_color)

我认为我的问题与.fill()仅适用于颜色值(230、230、230)有关。但我本质上希望屏幕使用背景图像而不是静态颜色进行更新。有人可以帮忙吗?我应该使用另一个函数代替.fill()吗?

解决方法

更改Settings中的背景:

class Settings:
    def __init__(self):
        # [...]
    
        self.bg_color = pygame.image.load('images/space.bmp')

Settings的构造函数中的AlienInvasion对象获取背景。如果背景图像的大小与显示器的大小不同,我建议使用pygame.transform.smoothscale()将背景缩放到正确的大小:

class AlienInvasion:
    """Overall class to manage game assets and behavior."""
    def __init__(self):
        # [...]
        self.settings = Settings()
        # [...]

        self.bg_color = pygame.transform.smoothscale(self.settings.bg_color,self.screen.get_size())
 

blit而不是fill背景:

class AlienInvasion:
    # [...]

    def _update_screen(self):
        self.screen.blit(self.settings.bg_color,(0,0))

blit将一个图像绘制到另一个图像上。在这种情况下,它将在与显示器关联的 Surface 上绘制背景图像( Surface )。


请注意,名称bg_color具有误导性。如果您使用的是背景图片,则应对其进行重命名(例如bg_imagebg_surface)。

,

使用:

      entities: [__dirname + '/../**/*.entity{.ts,.js}'],migrationsTableName: 'migration',migrations: [__dirname + '/../migration/*{.ts,cli: {
        migrationsDir: 'src/migration',},

代替:

self.screen.blit(your_image,0))

您的图片应该是屏幕的大小,元组应该是位置

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。