首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我关闭Pygame屏幕时它会冻结

当我关闭Pygame屏幕时它会冻结
EN

Stack Overflow用户
提问于 2011-04-11 09:03:52
回答 4查看 7.6K关注 0票数 3

代码加载了一个pygame屏幕窗口,但当我单击X关闭它时,它变得没有响应。我在64位系统上运行,使用的是32位python和32位pygame。

代码语言:javascript
复制
from livewires import games, color

games.init(screen_width = 640, screen_height = 480, fps = 50)

games.screen.mainloop()
EN

回答 4

Stack Overflow用户

发布于 2011-04-11 12:09:28

Mach1723的answer是正确的,但我想建议主循环的另一个变体:

代码语言:javascript
复制
while 1:
    for event in pygame.event.get():
        if event.type == QUIT: ## defined in pygame.locals
            pygame.quit()
            sys.exit()

        if event.type == ## Handle other event types here...

    ## Do other important game loop stuff here.
票数 9
EN

Stack Overflow用户

发布于 2011-04-20 22:39:39

我推荐下面的代码。首先,它包括时钟,这样你的程序就不会消耗CPU,除了轮询事件之外什么也不做。其次,它调用pygame.quit()来防止程序在windows的IDLE下运行时冻结。

代码语言:javascript
复制
# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://cs.simpson.edu/?q=python_pygame_examples

import pygame

# Define some colors
black    = (   0,   0,   0)
white    = ( 255, 255, 255)
green    = (   0, 255,   0)
red      = ( 255,   0,   0)

pygame.init()

# Set the height and width of the screen
size=[700,500]
screen=pygame.display.set_mode(size)

pygame.display.set_caption("My Game")

#Loop until the user clicks the close button.
done=False

# Used to manage how fast the screen updates
clock=pygame.time.Clock()

# -------- Main Program Loop -----------
while done==False:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done=True # Flag that we are done so we exit this loop

    # Set the screen background
    screen.fill(black)

    # Limit to 20 frames per second
    clock.tick(20)

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit ()
票数 4
EN

Stack Overflow用户

发布于 2011-04-11 10:58:36

这是一个非常简单的问题,您需要处理"QUIT“事件,请参阅事件文档:http://www.pygame.org/docs/ref/event.html

编辑:我现在想到你可能正在处理"QUIT“事件,它不工作,但没有更多的细节到你的代码,我不知道。

下面是一个处理"QUIT“事件的简单方法的快速示例:

代码语言:javascript
复制
import sys
import pygame

# Initialize pygame
pygame.init()
pygame.display.set_mode(resolution=(640, 480))

# Simple(ugly) main loop
curEvent = pygame.event.poll()

while curEvent.type != pygame.QUIT:
      # do something
      curEvent = pygame.event.poll()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5615860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档