首页 > 解决方案 > 如何修复随机无效语法

问题描述

无缘无故弹出无效语法,我不确定下一步该做什么。导致问题的代码非常重要,我不想把它搞砸。

我尝试删除该行,运行模块,然后尝试写回该行,弹出相同的错误。

与顶部问题相关的代码:

pygame.init()
game_width = 1000
game_height = 650
screen = pygame.display.set_mode((game_width, game_height))
clock = pygame.time.Clock()
running = True

与底部问题相关的代码:

pygame.display.flip()
clock.tick(50)
pygame.display.set_caption("MY GAME fps: " + str(clock.get_fps()))

第一个pygame上面的代码:

import pygame

# Set up the Enemy's brain
class Enemy():
#Enemy constructor function
def __init__(self):
    # Make the Enemy's varaible
    self.x = 30
    self.y = 30
    self.pic = pygame.image.load("../assets/Fish04_A.png")

    # Enemy update function (stuff to happen over and over again)
    def update(self, screen):
    screen.blit(self.pic, (self.x, self.y)()

我希望游戏可以正常运行,但这个错误是第一次出现。错误显示“无效语法并在第一个 pygame 中突出显示“p”。我使用的是 Python IDLE 3.8 版

标签: pythonpygame

解决方案


正如@hpaulj 和@Austin 提到的,您忘记为函数添加缩进,__init__并且updatescreen.blit(self.pic, (self.x, self.y)()行应更改为screen.blit(self.pic, (self.x, self.y))

import pygame

# Set up the Enemy's brain
class Enemy():
    #Enemy constructor function
    def __init__(self):
        # Make the Enemy's varaible
        self.x = 30
        self.y = 30
        self.pic = pygame.image.load("../assets/Fish04_A.png")

        # Enemy update function (stuff to happen over and over again)
    def update(self, screen):
        screen.blit(self.pic, (self.x, self.y))

pygame.init()
game_width = 1000
game_height = 650
screen = pygame.display.set_mode((game_width, game_height))
clock = pygame.time.Clock()
running = True

推荐阅读