如何修复随机无效语法

时间:2019-11-12 03:02:01

标签: python pygame

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

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

与问题顶部相关的代码:

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

1 个答案:

答案 0 :(得分:1)

正如@hpaulj和@Austin所述,您忘记为函数__init__update添加缩进,并且screen.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