Python外星人速成班游戏-用background.bmp图像替换背景颜色
我有一个正在运行的外星人入侵游戏,它来自Python速成教程。但是,我正在应用修改。我试图替换Python外星人速成班游戏-用background.bmp图像替换背景颜色,python,python-3.x,pygame,pygame-surface,Python,Python 3.x,Pygame,Pygame Surface,我有一个正在运行的外星人入侵游戏,它来自Python速成教程。但是,我正在应用修改。我试图替换bg_color的值,以表示存储在项目文件夹中的背景图像,而不是颜色,即 self.bg_color = pygame.image.load('images/space.bmp') 当前AlienInvision类从名为settings.py的sep文件中存储的名为settings的类导入背景色 class Settings: """A class to sto
bg_color
的值,以表示存储在项目文件夹中的背景图像,而不是颜色,即
self.bg_color = pygame.image.load('images/space.bmp')
当前AlienInvision类从名为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, 230)
更新屏幕随着游戏的进行不断更新屏幕,并在游戏运行时处理背景颜色
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()?使用:
self.screen.blit(your_image, (0, 0))
而不是:
self.screen.fill((0, 0, 0))
您的图像应为屏幕大小,元组为位置使用:
self.screen.blit(your_image, (0, 0))
而不是:
self.screen.fill((0, 0, 0))
您的图像应该是屏幕大小,元组是位置在
设置中更改背景
:
类设置:
定义初始化(自):
# [...]
self.bg_color=pygame.image.load('images/space.bmp'))
从AlienInvision
的构造函数中的设置
对象获取背景。如果背景图像的大小与显示器的大小不同,我建议使用将背景缩放到正确的大小:
类AlienInvision:
“”“管理游戏资产和行为的总体类。”“”
定义初始化(自):
# [...]
self.settings=settings()
# [...]
self.bg_color=pygame.transform.smoothscale(self.settings.bg_color,
self.screen.get_size())
背景而非显示表面:
类AlienInvision:
# [...]
def_更新_屏幕(自身):
self.screen.blit(self.settings.bg_color,(0,0))
blit
将一个图像绘制到另一个图像上。在这种情况下,它会在与显示器相关联的表面上绘制背景图像(表面)
请注意,名称
bg_color
具有误导性。如果您使用的是背景图像,则应将其重命名(例如,bg_图像
或bg_表面
)。在设置中更改背景:
类设置:
定义初始化(自):
# [...]
self.bg_color=pygame.image.load('images/space.bmp'))
从AlienInvision
的构造函数中的设置
对象获取背景。如果背景图像的大小与显示器的大小不同,我建议使用将背景缩放到正确的大小:
类AlienInvision:
“”“管理游戏资产和行为的总体类。”“”
定义初始化(自):
# [...]
self.settings=settings()
# [...]
self.bg_color=pygame.transform.smoothscale(self.settings.bg_color,
self.screen.get_size())
背景而非显示表面:
类AlienInvision:
# [...]
def_更新_屏幕(自身):
self.screen.blit(self.settings.bg_color,(0,0))
blit
将一个图像绘制到另一个图像上。在这种情况下,它会在与显示器相关联的表面上绘制背景图像(表面)
请注意,名称bg_color
具有误导性。如果您使用的是背景图像,则应将其重命名(例如,bg\u图像
或bg\u曲面
)