pygame与类有关

时间:2017-12-03 15:32:32

标签: python pygame

创建平台游戏。当玩家到达地图外部时,当前使用的级别列表会附加到下一级别。 我的问题是我一直收到这个错误:' End_game'对象没有属性' level_limit' 这是我的最终游戏类:

class End_game(Level):
    def _init_(self, player):
            Level.__init__(self, player)
            self.level_limit = -1111 
            screen.fill(WHITE)
            text = s_font.render("Game Complete. Well Done",True, BLACK)
            text = s_font.render("Jumps taken to complete level:"+str(jump_score),True, BLACK)
            screen.blit(text, [100,100])

希望在第2级完成时显示结束游戏类。 这里是一些代码,一旦它们到达屏幕的末尾就会将玩家移动到下一个级别:

current_position = player.rect.x + current_level.world_shift
    if current_position < current_level.level_limit:
        player.rect.x = 120
        if current_level_no < len(level_list)-1:
            current_level_no += 1
            current_level = level_list[current_level_no]
            player.level = current_level

所需的结果是在最后有一个带有文字的白色屏幕。

贝洛是我的游戏代码,所以你拥有一切。

import pygame
import random
import mixer

from settings import *


size = [WIDTH, HEIGHT]
screen= pygame.display.set_mode(size)

pygame.font.init()
s_font = pygame.font.SysFont("comicsansms", 25)

jump_score =(0)
is_blue = False

def start_screen():
        pygame.init
        pygame.font.init()

        backgroundimage = pygame.image.load("start.PNG").convert()
        screen.blit(backgroundimage,(0,0))
        pygame.display.update()

        pygame.font.init()

        start = True
        while start:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        quit()
                    if event.type == pygame.KEYDOWN:
                            if event.key == pygame.K_SPACE:
                                    start_screen = False
                                    main()
                            if event.key == pygame.K_q:
                                    pygame.quit()
                                    quit()

def score():
        text = s_font.render("Jumps:"+str(jump_score),True, BLACK)
        screen.blit(text, [0,0])

class Player(pygame.sprite.Sprite):
    def __init__(self):

        super().__init__()
        if is_blue:
                self.image = pygame.image.load("blue_mushroom.PNG").convert_alpha()
                pygame.display.update()
        else:
                self.image = pygame.image.load("red_mushroom.PNG").convert_alpha()
                pygame.display.update()

        self.rect = self.image.get_rect()

        self.change_x = 0
        self.change_y = 0


        self.level = None

    def update(self):

        self.calc_grav()
        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

            self.change_y = 0

    def calc_grav(self):

        if self.change_y == 0:
            self.change_y = 1
        else:
            self.change_y += .35

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

    def jump(self):

        self.rect.y += 2
        platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        self.rect.y -= 2


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


    def go_left(self):
        self.change_x = -5

    def go_right(self):
        self.change_x = 5

    def stop(self):
        self.change_x = 0



class Platform(pygame.sprite.Sprite):
    def __init__(self, width, height):

        super().__init__()

        self.image = pygame.Surface([width, height])
        self.image.fill(BROWN)
        self.rect = self.image.get_rect()

class Level():

    def __init__(self, player):
        self.platform_list = pygame.sprite.Group()
        self.enemy_list = pygame.sprite.Group()
        self.player = player

        self.world_shift = 0

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

    def draw(self, screen):
        backgroundimage = pygame.image.load("forest.png").convert()
        backgroundimage
        screen.blit(backgroundimage,(0,0))

        self.platform_list.draw(screen)
        self.enemy_list.draw(screen)


    def shift_world(self, shift_x):

        self.world_shift += shift_x

        for platform in self.platform_list:
            platform.rect.x += shift_x

        for enemy in self.enemy_list:
            enemy.rect.x += shift_x



class Level_one(Level):
    def __init__(self, player):

        Level.__init__(self, player)

        self.level_limit = -1111

        level = [[210, 70, 500, 500],
                 [210, 70, 500, 100],
                 [70, 210, 450, 100],
                 [40, 40, 800, 300],
                 [40, 40, 800, 150],
                 [40, 80, 750, 200],
                 [210, 70, 800, 400],
                 [210, 70, 1000, 500],
                 [210, 70, 1220, 280],
                 [70, 210, 1000, 100],
                 [70, 210, 1000, 300],
                 [70, 310, 1220, 0],
                 [40, 40, 1070, 300],
                 [40, 40, 1070, 100],
                 [40, 40, 1180, 200],
                 [40, 40, 1180, 340]
                 ]

        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)



class Level_two(Level):
    def __init__(self, player):

        Level.__init__(self, player)

        self.level_limit = -1111

        level = [[210, 30, 450, 570],
                 [210, 30, 850, 420],
                 [210, 30, 850, 600],
                 [210, 30, 600, 280],
                 ]


        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)

class End_game(Level):
        def _init_(self, player):
                Level.__init__(self, player)
                self.level_limit = -1111 
                screen.fill(WHITE)
                text = s_font.render("Game Complete. Well Done",True, BLACK)
                text = s_font.render("Jumps taken to complete level:"+str(jump_score),True, BLACK)
                screen.blit(text, [100,100])


def main():

    pygame.init()

    sound = pygame.mixer.Sound("Mario_Jumping-Mike_Koenig-989896458.WAV")
    back_music = pygame.mixer.music.load("Sneaky_Snitch_Kevin_MacLeod_-_Gaming_Background_Mu.WAV")
    pygame.mixer.music.play(-1)

    pygame.display.set_caption(TITLE)

    player = Player()

    level_list = []
    level_list.append(Level_one(player))
    level_list.append(Level_two(player))
    level_list.append(End_game(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 = HEIGHT - player.rect.height
    active_sprite_list.add(player)


    done = False

    clock = pygame.time.Clock()

#main programme

    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_LEFT:
                    player.go_left()
                if event.key == pygame.K_RIGHT:
                    player.go_right()
                if event.key == pygame.K_UP:
                    player.jump()
                    sound.play()
                    global jump_score
                    jump_score = jump_score + 1
                if event.key == pygame.K_m:
                    pygame.mixer.music.pause()
                if event.key == pygame.K_n:
                    pygame.mixer.music.unpause()
                if event.type == pygame.KEYUP: 
                        if event.key == pygame.K_LEFT and player.change_x < 0:
                            player.stop()
                        if event.key == pygame.K_RIGHT and player.change_x > 0:
                            player.stop()


        active_sprite_list.update()
        current_level.update()


        if player.rect.right >= 500:
            diff = player.rect.right - 500
            player.rect.right = 500
            current_level.shift_world(-diff)

        if player.rect.left <= 120:
            diff = 120 - player.rect.left
            player.rect.left = 120
            current_level.shift_world(diff)

        # If the player gets to the end of the level, go to the next level
        current_position = player.rect.x + current_level.world_shift
        if current_position < current_level.level_limit:
            player.rect.x = 120
            if current_level_no < len(level_list)-1:
                current_level_no += 1
                current_level = level_list[current_level_no]
                player.level = current_level

        current_level.draw(screen)

        active_sprite_list.draw(screen)


        clock.tick(60)

        score() 
        pygame.display.flip()

    pygame.quit()

if __name__ == "__main__":
    start_screen()    
    main()

1 个答案:

答案 0 :(得分:0)

您遇到的唯一问题是End_game的{​​{1}}输入错误为__init__

这将导致它永远不会被运行,然后_init_属性实际上从未被设置。只需解决这个问题,你就可以摆脱这个错误。

相关问题