首页 > 解决方案 > IndentationError 但它没有混淆制表符和空格

问题描述

当我尝试运行我的脚本时,它坚持认为第 10 行有一个 IndentationError,即 def init (self): 行。查找它我尝试了一些我见过的常见修复,检查以确保选项卡和空格没有混淆,确保循环结构正确,但没有任何工作!

import sys
import pygame

from settings import Settings
from ship import Ship

class AlienInvasion:
  """Overall game assets and behavior"""
    
    def __init__(self):
        """Initialize the game, and create game resources"""
        pygame.init()
        
        self.settings = Settings()pi
        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))

        pygame.display.set_caption("Alien Invasion")

        self.ship = Ship(self)

        # Set the background
        self.bg_color = (230, 230, 230)

    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 pass
                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()

如果有人能告诉我发生了什么,那将不胜感激。在我的编码生涯中这么晚才尝试 Python 实在是太棒了。

标签: python

解决方案


这个:

class AlienInvasion:
  """Overall game assets and behavior"""

应该是这样的:

class AlienInvasion:
    """Overall game assets and behavior"""

使其匹配下一个定义:

    def __init__(self):

推荐阅读