2017-09-24 7 views
1

화살표 키에 따라 상자가 화면에서 움직이는 게임을 작성하려고합니다. '스페이스'버튼을 누르면 일시 중지됩니다.Tkinter 바인딩 : 루프 반복 자체 (파이썬)

아무 이유없이, 스페이스 버튼을 누르면 아무것도 발생하지 않은 것처럼 'NewGame()'루프로 돌아갑니다. 왜 이런 일이 일어나는 걸까요?

from Tkinter import * 

HEIGHT = 400 
WIDTH = 300 
cHEIGHT=HEIGHT-100 
cWIDTH=WIDTH 
TOPLEFT=3 
BUTTOMRIGHT=13 
RECTANGLE_SIDE=BUTTOMRIGHT-TOPLEFT 

def NewGame(): 

    def Key(event): 
     while True: 
      (x1,y1,x2,y2)=canvas.coords(head) 
      if event.keysym =='Right': 
       canvas.move(head,1,0) 
       root.update() 
       if x1>=cWIDTH: 
        canvas.move(head, -cWIDTH,0) 
      elif event.keysym=='Left': 
       canvas.move(head,-1,0) 
       root.update() 
       if x2<=0: 
        canvas.move(head, cWIDTH,0) 
      elif event.keysym=='Up': 
       canvas.move(head,0,-1) 
       root.update() 
       if y2<=0: 
        canvas.move(head, 0,cHEIGHT) 
      elif event.keysym=='Down': 
       canvas.move(head,0,1) 
       root.update() 
       if y1>=cHEIGHT: 
        canvas.move(head, 0,-cHEIGHT) 
      elif event.keysym=='space': 
       break 

    canvas.delete("all") 
    head=canvas.create_rectangle(TOPLEFT,TOPLEFT,BUTTOMRIGHT,BUTTOMRIGHT) 
    root.bind('<Key>', Key) 

root = Tk() 
root.geometry(('%dx%d')%(HEIGHT,WIDTH)) 

b1 = Button(root, text = 'New Game', command=NewGame) 
b1.pack() 

canvas=Canvas(root, height = cHEIGHT, width = cWIDTH) 
canvas.pack() 

root.mainloop() 

답변

1

당신은 while 루프를 제거해야합니다.

while 루프가 True에서 실행되도록 설정합니다. 물론 루프에서 벗어나지 않으면 무한정 실행됩니다. 루프에서 깨는 유일한 경우는 event.keysym=='space' 일 때입니다.

event.keysymspace과 같으면 while 루프가 무한 반복됩니다.

def Key(event): 
     (x1,y1,x2,y2)=canvas.coords(head) 
     if event.keysym =='Right': 
      canvas.move(head,1,0) 
      root.update() 
      if x1>=cWIDTH: 
       canvas.move(head, -cWIDTH,0) 
     elif event.keysym=='Left': 
      canvas.move(head,-1,0) 
      root.update() 
      if x2<=0: 
       canvas.move(head, cWIDTH,0) 
     elif event.keysym=='Up': 
      canvas.move(head,0,-1) 
      root.update() 
      if y2<=0: 
       canvas.move(head, 0,cHEIGHT) 
     elif event.keysym=='Down': 
      canvas.move(head,0,1) 
      root.update() 
      if y1>=cHEIGHT: 
       canvas.move(head, 0,-cHEIGHT) 
     elif event.keysym=='space': 
      pass 
+0

이, 도움이되지 않는 다른 키를 누를 때까지 움직 만들 수 있습니다. 이것이 while while : while 루프를 사용한 이유입니다. 내 프로그램을 실행할 때 'space'가 눌려지면 while 루프가 종료되지 않습니다. –

0

당신은 당신이 event.keysym=='space' 상태가 발생할 때까지 루프 실행을 갖고 싶어. 대신 True에 루프를 실행하고 space 기다리는

당신은 while 루프 해당 조건을 사용하여 시도하고만큼 space 아직 누르면되지 않은으로 실행하도록 할 수 있습니다, 그것을 깰 누르면된다.

으로는 다음과 같습니다 : I 화살표 키 중 하나 개를 눌러 원하기 때문에

def Key(event): 
     while event.keysym!='space': 
      (x1,y1,x2,y2)=canvas.coords(head) 
      if event.keysym =='Right': 
       canvas.move(head,1,0) 
       root.update() 
       if x1>=cWIDTH: 
        canvas.move(head, -cWIDTH,0) 
      elif event.keysym=='Left': 
       canvas.move(head,-1,0) 
       root.update() 
       if x2<=0: 
        canvas.move(head, cWIDTH,0) 
      elif event.keysym=='Up': 
       canvas.move(head,0,-1) 
       root.update() 
       if y2<=0: 
        canvas.move(head, 0,cHEIGHT) 
      elif event.keysym=='Down': 
       canvas.move(head,0,1) 
       root.update() 
       if y1>=cHEIGHT: 
        canvas.move(head, 0,-cHEIGHT) 
+0

나는 그것을 시험해 보았다. 여전히 작동하지 않습니다. –