我正在学习《Python 速成课程》一书,但在尝试使用 Pygame 时遇到了困难。这是我的代码:
外星人入侵.py
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
""" Overall class to manage game assets and behavior"""
def __init__(self):
""" Initialize the game and create game resources"""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
def run_game(self):
"""Start the main loop for 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
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# Make the most recently drawn screen visible
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance and run the game
ai = AlienInvasion()
ai.run_game()
ship.py
import pygame
class Ship:
""" A class to manage the ship"""
def __init__(self, ai_game):
"""Initialize the ship and set its starting position"""
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
# Load the ship image and get its rect
self.image = pygame.image.load('images/rocket.bmp')
self.rect = self.image.get_rect()
#Start each new ship at the bottom center of the screen
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
""" Draw the ship at its current location"""
self.screen.blit(self.image, self.rect)
设置.py
class Settings:
""" Store all the settings for Alien Invasions"""
def __init__(self):
""" Initialize the game's settings"""
# Screen Settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230,230,230)
根据这本书,打开alien_invasion.py应该会弹出一个灰色背景的窗口,图像rocket.bmp位于底部居中。相反,pygame 窗口会打开一瞬间,然后立即关闭。
每个单独的模块编译时都没有错误。在阅读了其他具有类似问题的堆栈交换帖子后,我尝试两次和三次检查 while 循环是否存在缩进错误,并且我相当确定缩进是正确的。问题似乎与 Ship 类有关,因为如果我从 Alien_invasion.py 中删除第 19 行和第 31 行
self.ship = Ship(self)
...
self.ship.blitme()
然后程序按预期工作:它打开一个带有灰色背景的 pygame 窗口,没有火箭飞船图像(并且不会关闭)。
我已经多次检查 Ship 类是否有错误,但似乎找不到任何错误。我已经从书中逐个字符复制了所有内容(除了图像名称),因为我必须下载自己的火箭图像。我用过这个。该文件肯定位于正确的目录中,因为 ship.py 编译时没有错误。起初我使用原始图像(PNG)并简单地调用了convert()方法,即ship.py的第12行曾经是
self.image = pygame.image.load('images/rocket.png').convert()
由于这是与教科书的唯一区别,我尝试手动将图像转换为 BMP,但这并没有解决问题。我不知道还有什么可以尝试改变。我将不胜感激任何帮助!我对编程非常陌生,所以可能有一个我忽略的明显问题。
下面是修改后的飞船代码,以显示红色矩形,火箭飞船图像通常会出现在您的书中:
import pygame
class Ship:
""" A class to manage the ship"""
def __init__(self, ai_game):
"""Initialize the ship and set its starting position"""
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
# Load the ship image and get its rect
#self.image = pygame.image.load('images/rocket.bmp') #without a rocket.bmp file and a folder labled "images", this will not work!
#the following two lines are temporary, they make a red rectangle instead of a rocket
self.image = pygame.surface.Surface((200, 200)) #so you can see what this looks like, here is an image im place of the rocket
self.image.fill((255, 20, 20)) #make the image red so it shows up on screen
self.rect = self.image.get_rect()
#Start each new ship at the bottom center of the screen
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
""" Draw the ship at its current location"""
self.screen.blit(self.image, self.rect).image, self.rect)
旁注,您使用什么代码编辑器来运行它?如果您通过窗口顶部的“运行”选项卡运行它,它应该能够告诉您错误是什么。这使得调试变得更加容易,因为回溯将准确地显示错误发生在哪一行以及导致错误的原因。
希望这能解决您的问题,如果您还有其他问题,请随时询问我!祝您的编程之旅一切顺利!