Python 在启动应用程序后退出

标签 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()       

我检查了问题几次,主要关注退出循环,但仍然没有任何结果。

最佳答案

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

祝你好运,编码愉快!

关于Python 在启动应用程序后退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32769459/

相关文章:

python - 为什么 Python "preemptively"在尝试计算非常大的数字时会挂起?

python - 如何在 Pytorch 中实现收缩自动编码器?

python-2.7 - 在python qt中带有线程的Qwebview

python - 使用 findall 替换并添加多行

python - 如何加载网页(不在浏览器中)然后获取该网页的网址?

python - 在 groupby pandas 之后对多列进行 t 测试

python - 删除以相同子字符串开头的第二个项目

python - 在matplotlib颜色栏中隐藏每个第n个刻度标签的最干净方法?

python - 如何为 python2.7 安装 pylint?

python - 在 PyInstaller 中将数据与 .spec 文件捆绑