当我在显示窗口上模糊一个图像后,我遇到了语法错误。我创建了一个单独的模块,在其中,我创建了一个类来管理图像的所有方面(位置、行为)。我加载了图像并获取了它的rect,最后我将图像绘制到了它想要的位置。该文件没有错误,所以我转到管理游戏资产和行为的主文件。在主文件中,我导入了管理映像的类。然后,我打了一个电话(在填写背景后)绘制图像,使其出现在背景之上。它给了我错误
第46行self.ship.blitme() ^ SyntaxError:无效语法
下面是图像类的代码片段
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/ship.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)以下是管理游戏资产和行为的主要类
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")
# Set the background color.
self.bg_color = (230, 230, 230)
self.ship = Ship(self)
def run_game(self):
"""Start the main loop for the game."""
while True:
self._check_events()
self._update_events()
# Redraw the screen during each pass through the loop.
def _check_events(self):
# Respond for keyboard and mouse events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def _update_events(self):
"""Update images on the screen, and flip to the new screen."""
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()发布于 2020-05-07 09:19:04
你忘了第45行的一个结束括号,main.py
self.screen.fill((self.settings.bg_color) ) # <-- this one不幸的是,Python常常会在错误的一行下面标记一条线。
https://stackoverflow.com/questions/61654025
复制相似问题