2016-07-29 4 views
0

저는 Connect 4를 파이썬으로 만들려고합니다.하지만 화면 클릭 좌표를 가져 오는 방법을 알아낼 수 없으므로 사용할 수 있습니다. 지금은 보드를 그린 다음 누군가가 클릭하고 점을 찍은 다음 while 루프의 맨 위로 돌아가서 화면을 지우고 다시 시도하겠습니다. 나는 몇 가지 옵션을 시도했지만 아무도 나를 위해 일한 것 같지 않았다.클릭 이벤트를 인식하기 위해 Python 거북이 찾기

def play_game(): 
""" 
When this function runs, allows the user to play a game of Connect 4 
against another person 
""" 
turn = 1 
is_winner = False 
while is_winner == False: 
    # Clears screen 
    clear() 
    # Draws empty board 
    centers = draw_board() 
    # Decides whose turn it is, change color appropriately 
    if turn % 2 == 0: 
     color = RED 
    else: 
     color = BLACK 
    # Gets coordinates of click 
    penup() 
    onscreenclick(goto) 
    dot(HOLE_SIZE, color) 
    turn += 1 
+0

어떤 옵션을 시도 했습니까? 또한, 어떤 GUI 라이브러리를 사용하고 있습니까? 그것을 알지 못하면 대답하기가 어렵습니다. – wirefox

+0

@wirefox 아마도 거북이 - 적어도 그것이 제목이 말한 것입니다. –

+0

@WayneWerner 예, 저에게 거북이가 명확하게 GUI 라이브러리로 해결되지 않습니다. 그는 일반 거북이 에이전트에 대해 이야기하고 다른 것을 사용하여 그릴 수있었습니다 – wirefox

답변

0

나는 당신이 파이썬에서 거북이를 사용 있으리라 믿고있어 을 그런 경우, 여기에 도움이 게시물에 대한 링크입니다 (따라서 이름입니다.) : Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates 내가 알고, 내가 알고있다. 나는 그냥 다음 사람만큼 답변을 싫어. 그러나 내가 링크를 준 게시물은 아마도 내가 할 수있는 것보다 더 나은 질문에 대답 할 수있을 것입니다. 제목에서 언급 한 바와 같이

~ Mr.Python

0

가정은 turtle을 사용하고 있습니다 :

당신은 분명히 goto라는 이름의 한 콜백 함수는 두 개의 매개 변수를 사용한다는 것을 의미
>>> import turtle 
>>> help(turtle.onscreenclick) 

Help on function onscreenclick in module turtle: 

onscreenclick(fun, btn=1, add=None) 
    Bind fun to mouse-click event on canvas. 

    Arguments: 
    fun -- a function with two arguments, the coordinates of the 
      clicked point on the canvas. 
    num -- the number of the mouse-button, defaults to 1 

    Example (for a TurtleScreen instance named screen) 

    >>> onclick(goto) 
    >>> # Subsequently clicking into the TurtleScreen will 
    >>> # make the turtle move to the clicked point. 
    >>> onclick(None) 

, X와 Y 위치.

import turtle 

def goto(x, y): 
    print('Moving to {}, {}'.format(x,y)) 
    turtle.goto(x, y) 

turtle.onscreenclick(goto) 
turtle.goto(0,0) 

클릭 할 때마다 거북이가 다른 위치로 이동합니다. turtle에는 이미 이벤트 루프가 있습니다. 자신의 이벤트 루프가 필요하지 않습니다. 클릭에 응답하십시오.

1

다른 답변도 의도 한 것처럼, 나는 실제 문제를 해결하지 못한다고 생각합니다. 당신은 당신의 코드에서 무한 루프를 도입하여 이벤트를 잠근 :

is_winner = False 
while is_winner == False: 

당신은 거북이의 그래픽이 작업을 수행 할 수 없습니다 - 당신은 이벤트 핸들러 및 초기화 코드를 설정하지만, 메인 루프로 제어를 넘겨 이벤트 핸들러. 당신이 그렇게 할 수있는 방법을 내 다음 재 쇼 :

import turtle 

colors = ["red", "black"] 
HOLE_SIZE = 2 

turn = 0 
is_winner = False 

def draw_board(): 
    pass 
    return (0, 0) 

def dot(color): 
    turtle.color(color, color) 
    turtle.stamp() 

def goto(x, y): 
    global turn, is_winner 

    # add code to determine if we have a winner 

    if not is_winner: 
     # Clears screen 
     turtle.clear() 
     turtle.penup() 

     # Draws empty board 
     centers = draw_board() 

     turtle.goto(x, y) 

     # Decides whose turn it is, change color appropriately 
     color = colors[turn % 2 == 0] 
     dot(color) 

     turn += 1 
    else: 
     pass 

def start_game(): 
    """ 
    When this function runs, sets up a new 
    game of Connect 4 against another person 
    """ 

    global turn, is_winner 

    turn = 1 
    is_winner = False 

    turtle.shape("circle") 
    turtle.shapesize(HOLE_SIZE) 

    # Gets coordinates of click 
    turtle.onscreenclick(goto) 

start_game() 

turtle.mainloop() 

실행 그것은 당신은 당신이 설명 원하는 동작을 확인할 수 있습니다.

+0

turn 및 is_winner 전역 변수를 start_game에서 생성하고 goto 매개 변수로 전달하는 대신에 무엇이 이점입니까? – HelpPlz

+0

또한 사용자가 클릭 한 장소가 이미 점유되었거나 비어 있는지 테스트하는 다른 함수를 만들려면 위의 점을 그리는 바로 위의 함수를 호출 할 것이라고 추측합니다. 그러나 조건이 거짓 인 경우 어떻게하면 게임을 건너 뛰고 사람이 다시 걸리게 할 수 있습니까? 여기서 일하니? – HelpPlz

+0

@HelpPlz, 우리는'turtle.onscreenclick()'에 의해 호출 된 함수로서'turn'과'is_winner'를'goto()'에 넘길 수없고 전달되는 매개 변수를 제어하지 않습니다. 다른 질문에 대해서는이 현재의 질문을 닫고 추가 한 코드가 있으면 무엇이든 새로 질문하십시오. – cdlane

관련 문제