Python 3.x 在Python 3.8中尝试blit图像时遇到语法错误
在显示窗口上blit图像后,我遇到语法错误。我制作了一个单独的模块,在其中,我创建了一个类来管理图像的所有方面(位置、行为)。我加载了图像并获取了它的rect,最后我在它想要的位置绘制了图像。该文件没有错误,因此我转到管理游戏资产和行为的主文件。在主文件中,我导入了管理映像的类。然后,我打了一个电话(填充背景后)来绘制图像,使其显示在背景之上。它给了我错误 第46行 self.ship.blitme() ^ 语法错误:无效语法 下面是image类的代码片段Python 3.x 在Python 3.8中尝试blit图像时遇到语法错误,python-3.x,pygame,pygame-surface,blit,Python 3.x,Pygame,Pygame Surface,Blit,在显示窗口上blit图像后,我遇到语法错误。我制作了一个单独的模块,在其中,我创建了一个类来管理图像的所有方面(位置、行为)。我加载了图像并获取了它的rect,最后我在它想要的位置绘制了图像。该文件没有错误,因此我转到管理游戏资产和行为的主文件。在主文件中,我导入了管理映像的类。然后,我打了一个电话(填充背景后)来绘制图像,使其显示在背景之上。它给了我错误 第46行 self.ship.blitme() ^ 语法错误:无效语法 下面是image类的代码片段 import pygame clas
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()
您在第45行,
main.py
中忘记了一个右括号:
self.screen.fill((self.settings.bg_color) ) # <-- this one
self.screen.fill((self.settings.bg_color))哇!一直以来,这只是一个结束括号哈哈。非常感谢。