V
V
Voxel252020-06-17 10:31:06
Python
Voxel25, 2020-06-17 10:31:06

When I write a function, an invalid syntax error occurs, what should I do?

When I write a function, an invalid syntax error occurs, please help!
Sublime highlights like this:
5ee9c5cac12f5126965563.jpeg

import pygame

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

bg = pygame.image.load('bg.jpg')


class Player(pygame.sprite.Sprite):
    idle = True
    left = False
    right = False

    def __init__(self):
        super.__init__()

        self.image = pygame.image.load('idle.png')
        self.rect = self.image.get_rect()

        self.change_x = 0
        self.change_y = 0

    def update(self):
        self.calc_gravity()

        self.rect.x += self.change_x

        block_hit_list = pygame.sprite.spritecollide(
            self, self.level.platform_list, False)
        for block in block_hit_list:
            if self.change_x > 0:
                self.rect.right = block.rect.left
            elif self.change_x < 0:
                self.rect.left = block.rect.right

        self.rect.y += self.change_y

        block_hit_list = pygame.sprite.spritecollide(
            self, self.level.platform_list, False)
        for block in block_hit_list:
            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            elif self.change_y < 0:
                self.rect.top = block.rect.bottom

    def calc_grav(self):
        if self.change_y == 0:
            self.change_y = 1
        else:
            self.change_y += .95

        if self.rect.y >= SCREEN_HEIGHT - self.rect.height and self.change_y >= 0:
            self.change_y = 0
            self.rect.y = SCREEN_HEIGHT - self.rect.height

    def jump(self):
        self.rect.y += 10
        platform_hit_list = pygame.sprite.spritecollide(
            self, self.level.platform_list, False)
        self.rect.y -= 10

        if len(platform_hit_list) > 0 or self.rect.bottom >= SCREEN_HEIGHT:
            self.change_y = -16

    def go_left(self):
        self.change_x = -9
        if (not self.left):
            self.image = pygame.image.load('left.png')
            self.idle = False
            self.left = True
            self.right = False

    def go_right(self):
        self.change_x = 9
        if (not self.right):
            self.image = pygame.image.load('left.png')
            self.idle = False
            self.left = False
            self.right = True

    def stop(self):
        self.change_x = 0
        self.image = pygame.image.load('idle.png')
        self.idle = True
        self.left = False
        self.right = False


class Block(pygame.sprite.Sprite):
    def __init__(self, width, height):
        super().__init__()
        self.image = pygame.image.load('block.png')

        self.rect = self.image.get_rect


class Level(object):
    def __init__(self, player):
        self.patforms = pygame.sprite.Group()
        self.player = player

    def update(self):
        self.platform_list.update()

    def draw(self, screen):
        screen.blit(bg, (0, 0))

        self.platform_list.draw(screen)


class Level_01(Level):
        def __init__(self, player):
            Level.__init__(self, player)

            level = [
                [25, 24, 500, 500],
                [25, 24, 200, 400],
                [25, 24, 600, 300],
            ]

            for platform in level:
                block = Platform(platform[0], platform[1])
                block.rect.x = platform[2]
                block.rect.y = platform[3]
                block.player = self.player
                self.platform_list.add(block


def main():
    pygame.init()

    size=[SCREEN_WIDTH, SCREEN_HEIGHT]
    screen=pygame.display.set_mode(size)

    pygame.display.set_caption("Game")

    player=Player()

    level_list=[]
    level_list.append(Level_01(player))

    current_level_no=0
    current_level=level_list[current_level_no]

    active_sprite_list=pygame.sprite.Group()
    player.level=current_level

    player.rect.x=340
    player.rect.y=SCREEN_HEIGHT - player.rect.height
    active_sprite_list.add(player)


    clock=pygame.time.Clock()

    done=False

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done=True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    player.go_left()
                if event.key == pygame.K_d:
                    player.go_right()
                if event.key == pygame.K_SPACE:
                    player.jump()

        current_level.draw(screen)
        active_sprite_list.draw(screen)

        clock.tick(25)

        pygame.display.flip()

pygame.quit()

Mistake:
File "D:\Voxel\Programming\Projects\Game\Scripts\main.py", line 126
    def main():
    ^
SyntaxError: invalid syntax

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
# ., 2020-06-17
@Voxel25

Forgot to close the parenthesis!

import pygame

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

bg = pygame.image.load('bg.jpg')


class Player(pygame.sprite.Sprite):
    idle = True
    left = False
    right = False

    def __init__(self):
        super.__init__()

        self.image = pygame.image.load('idle.png')
        self.rect = self.image.get_rect()

        self.change_x = 0
        self.change_y = 0

    def update(self):
        self.calc_gravity()

        self.rect.x += self.change_x

        block_hit_list = pygame.sprite.spritecollide(
            self, self.level.platform_list, False)
        for block in block_hit_list:
            if self.change_x > 0:
                self.rect.right = block.rect.left
            elif self.change_x < 0:
                self.rect.left = block.rect.right

        self.rect.y += self.change_y

        block_hit_list = pygame.sprite.spritecollide(
            self, self.level.platform_list, False)
        for block in block_hit_list:
            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            elif self.change_y < 0:
                self.rect.top = block.rect.bottom

    def calc_grav(self):
        if self.change_y == 0:
            self.change_y = 1
        else:
            self.change_y += .95

        if self.rect.y >= SCREEN_HEIGHT - self.rect.height and self.change_y >= 0:
            self.change_y = 0
            self.rect.y = SCREEN_HEIGHT - self.rect.height

    def jump(self):
        self.rect.y += 10
        platform_hit_list = pygame.sprite.spritecollide(
            self, self.level.platform_list, False)
        self.rect.y -= 10

        if len(platform_hit_list) > 0 or self.rect.bottom >= SCREEN_HEIGHT:
            self.change_y = -16

    def go_left(self):
        self.change_x = -9
        if (not self.left):
            self.image = pygame.image.load('left.png')
            self.idle = False
            self.left = True
            self.right = False

    def go_right(self):
        self.change_x = 9
        if (not self.right):
            self.image = pygame.image.load('left.png')
            self.idle = False
            self.left = False
            self.right = True

    def stop(self):
        self.change_x = 0
        self.image = pygame.image.load('idle.png')
        self.idle = True
        self.left = False
        self.right = False


class Block(pygame.sprite.Sprite):
    def __init__(self, width, height):
        super().__init__()
        self.image = pygame.image.load('block.png')

        self.rect = self.image.get_rect


class Level(object):
    def __init__(self, player):
        self.patforms = pygame.sprite.Group()
        self.player = player

    def update(self):
        self.platform_list.update()

    def draw(self, screen):
        screen.blit(bg, (0, 0))

        self.platform_list.draw(screen)


class Level_01(Level):
        def __init__(self, player):
            Level.__init__(self, player)

            level = [
                [25, 24, 500, 500],
                [25, 24, 200, 400],
                [25, 24, 600, 300],
            ]

            for platform in level:
                block = Platform(platform[0], platform[1])
                block.rect.x = platform[2]
                block.rect.y = platform[3]
                block.player = self.player
                self.platform_list.add(block)   ЗДЕСЬ БЫЛА НЕЗАКРЫТА СКОБКА


def main():
    pygame.init()

    size=[SCREEN_WIDTH, SCREEN_HEIGHT]
    screen=pygame.display.set_mode(size)

    pygame.display.set_caption("Game")

    player=Player()

    level_list=[]
    level_list.append(Level_01(player))

    current_level_no=0
    current_level=level_list[current_level_no]

    active_sprite_list=pygame.sprite.Group()
    player.level=current_level

    player.rect.x=340
    player.rect.y=SCREEN_HEIGHT - player.rect.height
    active_sprite_list.add(player)


    clock=pygame.time.Clock()

    done=False

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done=True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    player.go_left()
                if event.key == pygame.K_d:
                    player.go_right()
                if event.key == pygame.K_SPACE:
                    player.jump()

        current_level.draw(screen)
        active_sprite_list.draw(screen)

        clock.tick(25)

        pygame.display.flip()

pygame.quit()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question