2016-09-03 6 views
2

PyGameのチュートリアルに従っていますが、何らかの理由でループが表示されない間にgameOverです。私はいくつかの構文エラーなどを調べようとしましたが、すべてうまく見えます。PyGame - Whileループが期待どおりに実行されていない - テキストが画面に印刷されず、ループがまだ実行中です

ここにコードがありますが、while gameOver == True:ループが問題です。

# Imports the pygame module. 
import pygame 

# Initialises all the pygame methods. 
pygame.init() 

# This will be the width and height of our window. 
display_width = 800 
display_height = 600 

# Sets the caption, or title bar to 'Slither'. 
pygame.display.set_caption("Slither") 
# Sets the pygame window too whatever display_width and display_height is, so we can change the variable easily - a touple because it only accepts one argument. 
gameDisplay = pygame.display.set_mode((display_width,display_height)) 

# Sets some basic colours using rgb (red,blue,green). 
white = (255,255,255) 
red = (255,0,0) 
green = (0,255,0) 
blue = (0,0,255) 
black = (0,0,0) 

# This variable will decide how many pixels the object will be moving. 
block_size = 25 

# Returns a font object to the variable font, with the font of Sys, with a size of 25. 
font = pygame.font.SysFont(None, 25) 

# Sets the frames per second of the game. 
FPS = 30 

# Returns a clock object to the clock variable. 
clock = pygame.time.Clock() 

# Defines a function called message_to_screen that takes in 2 arguments, the message itself and it's colour. 
def message_to_screen(msg,colour): 
    # Sets the variable screen_text by using the font method. 
    screen_text = font.render(msg,True,colour) 
    # Actually prints the text to the screen. 
    gameDisplay.blit(screen_text,[display_width/2, display_height/2]) 

# Creates a function so we can call it again later. 
def gameLoop(): 
    # Sets the gameExit variable to False, so we can set it to true later to get out of the loop below -- for QUITTING THE GAME. 
    gameExit = False 
    # Game over will be true when the user fails the objective. 
    gameOver = False 

    # These will be the coordinates of the head of the snake - we will change them in the while loop. 
    lead_x = display_width/2 # Divides the resolution of the display by 2, so whatever the resolution is, it'll be in the middle. 
    lead_y = display_height/2 
    lead_x_change = 0 
    lead_y_change = 0 


    # If the user does fail the objective, then this while loop will run. 
    while gameOver == True: 
     # Sets the background colour to black. 
     gameDisplay.fill(black) 
     # It sends a message to the screen using the functon we defined -- asks the user for input. 
     message_to_screen("You lose! Press R to try again and Q to quit.", red) 
     # Updates the display. 
     pygame.display.update() 

     # A loop that checks for events. 
     for event.type in pygame.event.get(): 
      # If the the user presses a key down, then this will run. 
      if event.type == pygame.KEYDOWN: 
       # If that key that is being pressed down is Q, then this if statement will run. 
       if event.key == pygame.K_q: 
        # Sets the appropirate variables to be able to exit the gameOver while loop, and then sets gameExit to true to exit the while loop below. 
        gameExit = True 
        gameOver = False 
       if event.key == pygame.K_r: 
        gameLoop() 


    # This while loop runs when gameExit is equal to false. 
    while gameExit == False: 
     # A for loop -- all the events that are happening on the screen are being sent to pygame.event.get(), so we're going through those events and then doing different things according to those events. 
     for event in pygame.event.get(): 
      # If the event in pygame.event.get() is QUIT, then this if statement runs. 
      if event.type == pygame.QUIT: 
       # Afterwards, it sets gameExit to true, therefore exiting the loop and running the exit functions outside the loop. 
       gameExit = True 

      # If the event type is pressing a key down, then this statement will run. 
      if event.type == pygame.KEYDOWN: 
       # If the key that is pressed down is the left key, then lead_x will change, depending on the size of the rectangle. 
       if event.key == pygame.K_LEFT: 
        # The lead_x variable changing by -25, this will be the coordinate that the rectangle will follow. 
        lead_x_change = -block_size 
        # Sets lead_y_change back to zero, so the rectangle doesn't move diagonally. 
        lead_y_change = 0 
       # If the key that is pressed down is the left key, then lead_x will change, depending on the size of the rectangle. 
       elif event.key == pygame.K_RIGHT: 
        # The lead_x_change variable changing by +25, this will be the coordinate that the rectangle will follow. 
        lead_x_change = block_size 
        # Sets lead_y_change back to zero, so the rectangle doesn't move diagonally. 
        lead_y_change = 0 

       # If the event is pressing the up key, then this will run. 
       elif event.key == pygame.K_UP: 
        # And therefore change the lead_y_change variable by -25. 
        lead_y_change = -block_size 
        # Sets lead_x_change back to zero, so the rectangle doesn't move diagonally. 
        lead_x_change = 0 
       # If the event is pressing the down key, then this will run. 
       elif event.key == pygame.K_DOWN: 
        # And therefore change the lead_y_change variable by -25. 
        lead_y_change = block_size 
        # Sets lead_x_change back to zero, so the rectangle doesn't move diagonally. 
        lead_x_change = 0 

     # Because it's in a loop, lead_x_change will constantly keep summing lead_x, and that controlls the coordinates of the snake. Same with lead_y_change. 
     lead_x += lead_x_change 
     lead_y += lead_y_change 

     # If the rectangle, which coordinates are outside the bounderies of the window, which is 800x600, then gameExit will equal to true and therefore escape from the loop. 
     if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0: 
      gameOver = True 

     # Sets the background colour. 
     gameDisplay.fill(black) 

     # Draws a rectangle - the arguments go as follows: where, colour, co-ordinates+width_height. 
     pygame.draw.rect(gameDisplay,green,[lead_x,lead_y,block_size,block_size]) 

     # A way to create a rectangle - colour and then co-ordinates+width_height. 
     # gameDisplay.fill(blue, rect=[200,150,25,25]) 

     # Updates the display. 
     pygame.display.update() 

     # Uses the clock object and sets the fps to the variable FPS. 
     clock.tick(FPS) 

# Calling the function so the game runs. 
gameLoop() 

# De-initliaises the pygame methods. 
pygame.quit() 
# Quits out of the window. 
quit() 

助けていただければ幸いです。

答えて

2

問題はあなたのループです。ここにあなたのゲームの基本的な構造です:

def gameLoop(): 
    while gameOver == True: 
    // Show game over screen 

    while gameExit == False: 
    // Runs the game 
    // Can set gameOver variable to true 

gameExitループが期待どおりに動作し、画面を終了するときにtrueにgameOverを設定します。しかし、ループはgameExitの内部にとどまります。コードは決してgameLoop関数の先頭に到達しないため、画面上のゲームは決して表示されません。

一つの解決策は、次のような構造になると思われる、あなたのgameExitループ内while gameOver == Trueを移動することができます

def gameLoop(): 
    while gameExit == False: 
    while gameOver == True: 
     // Show game over screen 

    // Runs the game 
    // Can set gameOver to true 

しかし、よりエレガントな解決策は、その別々の関数にループ上でゲーム全体を置くことであるかもしれませんそれを呼んでください。次に、gameOver変数を取り除くこともできます。ここに私の意味は次のとおりです:

def gameOverLoop(): 
    while True: 
    // Show game over screen until player makes selection. Then break the loop. 

def gameLoop(): 
    while gameExit == False: 
    // Run game 
    // ... 
    // If game ends, call game over screen 
    gameOverLoop() 

+0

私はこのYouTubeのビデオから「while gameOver == True」というコードをベースにしています:https://youtu.be/juRZLrUkDtU彼のコードがうまくいかない理由は何ですか? – naf

+0

ああ、私は問題を見る!あなたが言ったように、while while文を 'while gameExit == False'に移動するだけで恥ずかしいです! – naf

+0

うれしい私は仲間を助けることができる!基本的には、 'gameOver'のif文が動作しているかどうかを確認しました。私が見たとき、私は 'gameOver'ループが決して誘発されず、答えを見つけた理由を探しました:) – Cat

関連する問題