2011-04-11 3 views
3

코드는 파이 게임 화면을로드하지만, 닫으려면 X를 클릭하면 반응이 없습니다. 저는 32 비트 파이썬과 32 비트 파이 게임을 사용하는 64 비트 시스템에서 실행 중입니다. http://www.pygame.org/docs/ref/event.html닫을 때 파이 게임이 멈 춥니 다.

편집 : 이것은 아주 간단한 문제입니다

from livewires import games, color 

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

games.screen.mainloop() 

답변

2

, 당신은 "QUIT"이벤트에서 이벤트 설명서를 참조하십시오 처리 할 필요가 는 이제 나에게 발생하면 처리 할 수 ​​있다는 " QUIT "이벤트가 작동하지 않고 코드에 대한 자세한 내용은 알지 못합니다.

은 "QUIT"이벤트를 처리하는 간단한 방법의 간단한 예 :

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() 
8

Mach1723의 answer는 정확하지만 나는 메인 루프의 또 다른 변형 제안하고 싶습니다

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. 
2

파이 게임 사용시 QUIT를 포함한 모든 이벤트를 처리해야하므로 종료 이벤트를 처리하지 않으면 프로그램이 종료되지 않습니다. 여기에 코드가 있습니다.

import sys 
import pygame 
from pygame.locals import * 

def main(): 
    running = True 
    while running: 
     for event in pygame.event.get(): 
      if event.type==QUIT: #QUIT is defined at pygame.locals 
       runnning = False 
    #other game stuff to be done 

if __name__=='__main__': 
    pygame.init() 
    pygame.display.set_mode((640,480)) 
    main() 
4

다음 코드를 권장합니다. 첫째, 그것은 Clock을 포함하므로 프로그램은 아무것도하지 않고 이벤트를 폴링하는 CPU를 먹지 않습니다. 둘째, pygame.quit()를 호출하여 Windows에서 IDLE로 실행할 때 프로그램이 멈추는 것을 방지합니다.

# 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() 
+0

이 방법이 가장 효과적입니다. 일단 완료되면 모든 상태를 쉽게 저장할 수 있습니다. – ninMonkey

1

이미 만든 경우 다음 코드 if event.type == QUIT:에 추가하는 파이 게임 창을 닫을 수 간단하게 기능 for event in pygame.event.get():은 루프를 만들려면이 사용하고 함수 while True:와 게임 루프를 만들기 위해 ' True '루프는 마지막 코드에만 추가하면됩니다.

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

주 먼저 파이 게임과 SYS를 가져온해야합니다 이제 pygame.quit()sys.exit() 온 추가, 여기에 완성 된 코드입니다.