2017-05-19 17 views
0

理由は全く分かりませんが、speed変数を5から他の番号に変更しようとすると、1回の繰り返しの後にすべてがハングします。Pygame - speed変数は5から変更できません

私のゲームは、マウスによって制御される単なる矩形であり、ランダムに発生した高さを持つ2つの壁の間を移動する必要があります。ここに私の主な機能は次のとおりです。

def main(): 

    pygame.init() 

    # Game sounds 
    pygame.mixer.music.set_volume(1.0) 

    song = "Music/boss.WAV" 
    pygame.mixer.music.load(song) 
    pygame.mixer.music.set_endevent(pygame.constants.USEREVENT) 
    pygame.mixer.music.play() 

    passSound = pygame.mixer.Sound("Sound Effects/passPillar.wav") 
    passSound.set_volume(0.3) 
    levelUpSound = pygame.mixer.Sound("Sound Effects/levelUp.wav") 
    levelUpSound.set_volume(0.5) 

    # Set the width and height of the screen [width,height] 
    width = 1000 
    height = 800 
    size = [width, height] 
    screen = pygame.display.set_mode(size) 

    pygame.display.set_caption("boxes") 

    # Loop until the user clicks the close button. 
    done = False 

    # Used to manage how fast the screen updates 
    clock = pygame.time.Clock() 

    # Hide the mouse cursor 
    pygame.mouse.set_visible(False) 

    # the player and its variables 
    playerDimension = 50 

    playerX = width/4 

    playerScore = 0 

    playerLevel = 1 

    scoreBarWidth = 50 

    # the walls and their variables 
    extraWidth = 70 

    wallWidth = playerDimension/2 

    wallHeight_1 = randint((height/5) , (height*4/5)) 
    wallHeight_2 = height - wallHeight_1 - playerDimension+extraWidth 

    wall_Y_1 = 0 
    wall_Y_2 = wallHeight_1 + playerDimension+extraWidth 

    walls_X = width - wallWidth  

    speed = 5 

    # Game over and Intro screen 
    gameOver = False 

    introScreen = True 

    # Fonts 
    font = pygame.font.SysFont("Avenir Next", 26) 

    titleFont = pygame.font.SysFont("Avenir Next", 36) 

    # Game loop 
    while not done: 

     # ALL EVENT PROCESSING 
     for event in pygame.event.get(): 

      if event.type == pygame.QUIT: 

       done = True 

      elif event.type == pygame.constants.USEREVENT: 

       # When the song stops playing, start song again 
       pygame.mixer.music.load(song) 
       pygame.mixer.music.play() 

      elif event.type == pygame.KEYDOWN: 

       if event.key == pygame.K_r: 

        if gameOver: 

         gameOver = False 

         introScreen = True 

         playerLevel = 1 
         playerScore = 0 

         walls_X = width - wallWidth 

       if event.key == pygame.K_SPACE: 

        if introScreen: 

         introScreen = False 

         levelUpSound.play() 

     # ALL GAME LOGIC 

     # GET MOUSE POSITION 
     if not gameOver: 

      pos = pygame.mouse.get_pos() 
      y = pos[1] 

     if not gameOver and not introScreen: 

      # Format is x,y,w,h twice for both rects 
      touchingUpper = collisionDetect(playerX, y, playerDimension, playerDimension, walls_X, wall_Y_1, wallWidth, wallHeight_1) 

      touchingLower = collisionDetect(playerX, y, playerDimension, playerDimension, walls_X, wall_Y_2, wallWidth, wallHeight_2) 

      gameOver = touchingUpper or touchingLower 

      if walls_X == 0: # Move wall back to start and change heights 

       wallHeight_1 = randint((height/5) , (height*4/5)) 
       wallHeight_2 = height - wallHeight_1 - playerDimension+extraWidth 

       wall_Y_1 = 0 
       wall_Y_2 = wallHeight_1 + playerDimension+extraWidth 

       walls_X = width - wallWidth 

      else: 

       walls_X = walls_X - speed 

      if walls_X == playerX: 

       playerScore += 1 

       if playerScore < 5: 

        passSound.play() 

       elif playerScore == 5: 

        playerScore = 0 

        playerLevel += 1 

        levelUpSound.play() 

      scoreBarWidth = playerScore*50 



     # ALL CODE TO DRAW 
     screen.fill(screenColour) 

     # the player 
     pygame.draw.rect(screen, playerColour, [playerX, y, playerDimension, playerDimension], 0) 

     pygame.draw.rect(screen, wallColour, [walls_X, wall_Y_1, wallWidth, wallHeight_1])#the upper wall 
     pygame.draw.rect(screen, wallColour, [walls_X, wall_Y_2, wallWidth, wallHeight_2])#the lower wall 

     for i in range(0, playerLevel): 

      pygame.draw.rect(screen, levelsColour, [20 + (i*40), 20, 30, 30], 2) #levels 

     pygame.draw.rect(screen, scorebarColour, [20, 60, scoreBarWidth, 30], 0) #scorebar filler 
     pygame.draw.rect(screen, scorebarOutlineColour, [20, 60, 5*50, 30], 2) #scorebar outline 

     if gameOver: 

      text1 = titleFont.render("Game Over!", True, wallColour) 
      text1_rect = text1.get_rect() 
      text1_x = screen.get_width() * 1.7/3 - text1_rect.width/2 
      text1_y = screen.get_height() * 1.2/3 - text1_rect.height/2 
      screen.blit(text1, [text1_x, text1_y]) 

      text2 = font.render("You reached level " + str(playerLevel) + ".", True, wallColour) 
      text2_rect = text2.get_rect() 
      text2_x = screen.get_width() * 1.7/3 - text2_rect.width/2 
      text2_y = screen.get_height()/2 - text2_rect.height/2 
      screen.blit(text2, [text2_x, text2_y]) 

      text3 = font.render("Press R to restart.", True, wallColour) 
      text3_rect = text3.get_rect() 
      text3_x = screen.get_width() * 1.7/3 - text3_rect.width/2 
      text3_y = screen.get_height() * 1.8/3 - text3_rect.height/2 
      screen.blit(text3, [text3_x, text3_y]) 

     elif introScreen: 

      text1 = titleFont.render("Press Space to start!", True, wallColour) 
      text1_rect = text1.get_rect() 
      text1_x = screen.get_width()/2 - text1_rect.width/2 
      text1_y = screen.get_height() * 1/3 - text1_rect.height/2 
      screen.blit(text1, [text1_x, text1_y]) 

     # Update the screen 
     pygame.display.flip() 

     # Limit frames per second 
     clock.tick(200) 

    # quit. 
    pygame.quit() 

これはなぜ起こっているのですか?究極の目標は、速度をplayerLevelに比例させて、時間の経過とともに難易度を上げることです。

+1

あなたは何が起こっているかを見るためにプログラムをトレースしてみましたか? – Fletchius

答えて

0

の代わりに仕事を始め

if walls_X == 0: # Move wall back to start and change heights 

使用

if walls_X <= 0: # Move wall back to start and change heights 

なぜですか?

あなたが一箇所にのみ可変速度を使用します。walls_Xの初期値は975width - wallWidth)である

walls_X = walls_X - speed 

、その後、あなたが繰り返しspeedことによってそれを減少させる - speed == 5

970, 965, 960, 955...への問題のために文の中で walls_Xの値をテストすると発生します。

if walls_X == 0: # Move wall back to start and change heights 

speedでない場合、の除数975(i。 e。 1, 3, 5, 13, 15, ...とは異なります)walls_Xは決して0に届かず、あなたのテストwalls_X == 0は決して通過しません。

0

名前シャドーイングのように見えます。私は(私はすべての色を作成して、音をコメントアウトする必要がありましたが)自分のエラーを再現するために管理が、私はwall_speedするスピードと改名するとき、すべてが

関連する問題