2017-12-03 9 views
0

プラットフォーム作成。プレイヤーがマップの外側に達すると次のレベルに追加されるレベルリストを現在使用しています。 「End_game」オブジェクトには属性「level_limit」 .thisを持っていない私の最後のゲームクラス: 私の問題は、私はこのエラーを取得しておくことであるクラスでパイゲームが発生する

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() 
+2

'_init_'は' __init__'と同じものではありません... – jasonharper

+0

私は気づいていませんでした。お手数ですが –

答えて

0

あなたが持っている唯一の問題は、あなたのEnd_game__init__ではなく_init_として誤って入力されていることがあります。

これは決して実行されず、level_limit属性は実際には設定されません。ちょうどそれを修正し、あなたはこのエラーを取り除くでしょう。

+0

ありがとうございます。今はうまくいく。このような小さなエラーは決して私によって拾われていないでしょう –

関連する問題