PyGame - while循环未按预期运行 - 文本不打印到屏幕,而其他while循环仍在运行

时间:2016-09-03 09:03:40

标签: python-3.x pygame

我一直在关注PyGame的教程,但出于某种原因我的gameOver 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()

感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

问题是你的循环。这是你游戏的基本结构:

gameExit

现在gameOver循环按预期工作,并在您离开屏幕时将gameExit设置为true。但是你仍然在gameLoop循环中。代码永远不会到达while gameOver == True函数的顶部,因此屏幕上的游戏永远不会显示。

一种解决方案可能是将gameExit移到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() 变量。这就是我的意思:

DateTime start()
    { 
        DateTime d = new DateTime();
        return d.AddMilliseconds(Environment.TickCount);
    }

希望这有帮助。

相关问题