启动应用程序后,Python退出

时间:2015-09-24 19:30:30

标签: python python-2.7

我对编程很陌生。我可能犯了一些愚蠢的错误,但我无法找到它。任何帮助表示赞赏。这是代码:

import pygame
import time
import random

pygame.init()

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width, display_height))

pygame.display.set_caption('Moving square')

#####################Set the display parameters and title#####################

gameExit = False
gameOver = False

lead_x = display_width/2
lead_y = display_height/2
lead_x_change = 0
lead_y_change = 0
FPS = 15
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 25)
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
block_size = 10
###########################Setting the variables############################
def message_2_screen(msg, color):#setting a variable for the text
    themessage = font.render(msg, True, color)
    gameDisplay.blit(themessage, [265, display_height/2])


def gameloop():
    gameExit = False
    gameOver = False
    lead_x = display_width/2
    lead_y = display_height/2
    lead_x_change = 10
    lead_y_change = 0
    FPS = 15
    block_size = 10        


    while not gameExit:       
        while gameOver == True:
            gameDisplay.fill(white)
            message_2_screen('Press c to continue and q to quit', red)
            pygame.display.update() 
            for event in pygame.event.get():#creating a loop for the quitting and continue
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        gameExit = True
                        gameOver = False
                    if event.key == pygame.K_c:
                        gameloop()
        for event in pygame.event.get():#loop for quitting
            if event.type is pygame.QUIT:
                gameExit = True                                    
            if event.type == pygame.KEYDOWN:#setting the control buttons
                if event.key == pygame.K_LEFT:
                    lead_x_change = -block_size
                    lead_y_change = 0
                elif event.key == pygame.K_RIGHT:
                    lead_x_change = block_size
                    lead_y_change = 0
                elif event.key == pygame.K_UP:
                    lead_y_change = -block_size
                    lead_x_change = 0
                elif key.event == pygame.K_DOWN:
                    lead_y_change = block_size
                    lead_x_change = 0
        if lead_x >= display_width or lead_y >=display_height or lead_x < 0 or lead_y < 0:#Setting limits for where the square can move
            gameExit = True
        lead_x+= lead_x_change
        lead_y+= lead_y_change
        gameDisplay.fill(white)

        pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size ])#setting the position of the square
        pygame.display.update()
        clock.tick(FPS)

        pygame.quit()

        quit()
gameloop()       

我几次检查了这个问题,主要集中在退出循环,但仍然没有。

1 个答案:

答案 0 :(得分:2)

quit()来电中删除一个缩进。这将确保在循环退出后仅调用

祝你好运,编码愉快!