退出函数在pygame中不起作用

时间:2016-08-23 03:29:24

标签: python-3.x

我希望系统在我点击" x"在pygame的窗口。我希望能够在需要时调用quit_game函数。因此,如果用户确实退出,它实际上会退出。我尝试了sys exitpygame.quit(),但我还没有成功实现这些。现在我只是在介绍屏幕中内置了退出功能。我使用的是python 3.4.3。这是代码。

import math
import random
import time
import pygame
import sys
import glob
pygame.init()
move=0
FPS=60
blue=(0,0,255)
white=(255,255,255)
black=(0,0,0)
green=(0,155,0)
display_width=800
display_height=600
gamedisplay=pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Stacker')
clock=pygame.time.Clock()
speed=2
smallfont=pygame.font.SysFont("Arial",int(display_width/32))
mediumfont=pygame.font.SysFont("Arial",int(display_width/16))
largefont=pygame.font.SysFont("Arial",int(display_width/10))
gamedisplay.fill(green)
block=display_height/12
pygame.display.update()
def quit_game():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
            quit()
def intro_screen():
    welcome_message = mediumfont.render(str("Welcome to Stacker!!!"), True,black)
    gamedisplay.blit(welcome_message,(display_width/4,display_height/40))
    how_to_play_1=smallfont.render(str("Your goal is to get to the major prize bar"),True,black)
    gamedisplay.blit(how_to_play_1,(display_width/3.619909502,display_height/2))
    how_to_play=smallfont.render(str("Press the space bar to stop the shape"),True,black)
    gamedisplay.blit(how_to_play,(display_width/3.48583878,display_height/(12/7)))
    quit_game()
    pygame.display.update()
def middle_block():
    pygame.draw.rect(gamedisplay, blue,(display_width/(32/15),display_height-block,block,block))
    pygame.display.update()
def left_block():
    pygame.draw.rect(gamedisplay, blue,(display_width/(32/13),display_height-block,block,block))
    pygame.display.update()
def right_block():
    pygame.draw.rect(gamedisplay, blue,(display_width/(32/17),display_height-block,block,block))
    pygame.display.update()
def major_screen():
    major_message = mediumfont.render(str("Major Prize Here!"), True,black)
    gamedisplay.blit(major_message,(display_width/(10/3),display_height/40))
    pygame.display.update()
intro_screen()
pygame.time.delay(8000)
gamedisplay.fill(green)
major_screen()
pygame.draw.rect(gamedisplay, blue,(display_width-display_width,display_height/8,display_width,display_height/60))
pygame.draw.rect(gamedisplay, blue,(display_width-display_width,display_height/2.4,display_width,display_height/60))
middle_block()
left_block()
right_block()
pygame.display.update()

1 个答案:

答案 0 :(得分:0)

您的代码没有游戏循环。你可能会这么想:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
            quit()

你的游戏循环吗?虽然这几乎是正确的,但它缺少一个关键部分。 while 循环。你告诉Python循环Pygame正在跟踪的每一个事件。完成循环一次后,它会停止。您希望Python 持续循环Pygame事件,直到窗口关闭。为此,请创建一个变量来控制循环。例如running = Truenot_quit = True。接下来使用控制变量作为条件,围绕for循环包装while循环。最后,如果用户关闭窗口,请将控制变量设置为False

running = True # our variable to control the loop.
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False # break the loop
            pygame.quit()
            sys.exit()
            quit()

这应该可以让你关闭窗口。但是我应该注意,除非在你的游戏循环中,否则你的blitting /绘制到屏幕上的文本/形状都不可见。因此,您可能需要重新考虑一下代码。

相关问题