在 Python 3.8 中尝试 blit 图像时遇到语法错误
Encountering a Syntax Error while attempting to blit an image in Python 3.8
我在显示器上 blit 图像后遇到语法错误 window。我制作了一个单独的模块,在其中创建了一个 class 来管理图像的所有方面(位置、行为)。我加载图像并获取其矩形,最后我将图像绘制在所需位置。该文件没有错误,所以我转到管理游戏资产和行为的主文件。在主文件中,我导入了管理图像的 class。然后,我打电话(在填充背景后)绘制图像,使其出现在背景之上。它给了我错误
第 46 行
self.ship.blitme()
^
语法错误:语法无效
这是图片的代码片段 class
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)
这是管理游戏资产和行为的主要class
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()
您在第 45 行忘记了右括号,main.py
:
self.screen.fill((self.settings.bg_color) ) # <-- this one
不幸的是,Python 经常在错误的下方标记一行。
我在显示器上 blit 图像后遇到语法错误 window。我制作了一个单独的模块,在其中创建了一个 class 来管理图像的所有方面(位置、行为)。我加载图像并获取其矩形,最后我将图像绘制在所需位置。该文件没有错误,所以我转到管理游戏资产和行为的主文件。在主文件中,我导入了管理图像的 class。然后,我打电话(在填充背景后)绘制图像,使其出现在背景之上。它给了我错误
第 46 行 self.ship.blitme() ^ 语法错误:语法无效
这是图片的代码片段 class
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)
这是管理游戏资产和行为的主要class
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()
您在第 45 行忘记了右括号,main.py
:
self.screen.fill((self.settings.bg_color) ) # <-- this one
不幸的是,Python 经常在错误的下方标记一行。