2013-12-03 5 views
3

Python/Pygame을 배우려고합니다. 마우스 위치를 사용하는 프로그램을 만들었지 만 IDLE에서 명령 줄 프롬프트를 실행하면 마우스 위치가 업데이트되지 않고 그래픽 창을 클릭하면 응답하지 않는 모드가됩니다.Python/Pygame 마우스 위치가 업데이트되지 않고 프로그램 포커스가 사라짐

코드는 매우 간단합니다 (아래 참조). 인쇄 명령이 원래 마우스 위치를 반복해서 인쇄합니다. 어떤 아이디어?

import pygame 
from pygame.locals import * 
pygame.init() 
Screen = pygame.display.set_mode([1000, 600]) 
MousePos = pygame.mouse.get_pos() 
Contin = True 
while Contin: 
    print(MousePos) 

답변

4

, 대신 반복해서 같은 값을 인쇄하고 있습니다. 당신이해야하는 것입니다 :

import pygame 
from pygame.locals import * 
pygame.init() 
Screen = pygame.display.set_mode([1000, 600]) 
MousePos = pygame.mouse.get_pos() 
Contin = True 
while Contin: 
    MousePos = pygame.mouse.get_pos() 
    print(MousePos) 
    DoSomething(MousePos) 

참고 : 당신이 다른 이벤트를 처리 해달라고하면이뿐만 아니라 비 응답 모드로 들어갑니다.

여기 파이 게임에서 이벤트를 처리하는 더 좋은 방법입니다

while running: 
    event = pygame.event.poll() 
    if event.type == pygame.QUIT: 
     running = 0 
    elif event.type == pygame.MOUSEMOTION: 
     print "mouse at (%d, %d)" % event.pos 
+0

고마워! MousePos가 업데이트되지 않는다는 오류를 보지 못했다고 나는 어리 석다. 그러나 유능한 도움은 또한 반응이없는 문제를 해결 한 것으로 보인다. 많이 감사! – Enthuziast

0
이에 while 루프를 변경

: 새 값으로 MousePos를 업데이트하지 않는

while Contin: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      Contin = False 
    MousePos = pygame.mouse.get_pos() 
    print(MousePos) 
+0

고마워요 !!!! 나는 실수를 직접 보지 못하고 어리 석다. – Enthuziast