2016-07-29 10 views
0

私はPythonで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

私は(名前の由来。)あなたは、Pythonでタートルを使用していることを仮定してい そのような場合、ここで役に立つ記事へのリンクです:Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates 私が知っている、私は知っています。私はちょうどリンクの答えが次の男と同じくらい嫌い。しかし、私がリンクを与えた投稿は、あなたの質問に私ができるよりもはるかに良い仕事をするでしょう。あなたのタイトルで述べたように

〜Mr.Python

0

と仮定すると、あなたがturtleを使用している:

あなたは明らかに gotoを命名したコールバック関数は、2つのパラメータを取ることを意味します
>>> 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

また、ユーザーがクリックした場所が既に占有されているか空であるかをテストする他の関数を作成したい場合は、上記の関数をgotoの上に置き、ドットを描画すると思います。しかし、条件がFalseの場合、どうすればゲームをスキップしてその人にリクライクさせることができますか?ここで休憩しますか? – HelpPlz

+0

@HelpPlz、 'turtle.onscreenclick()'によって呼び出され、渡されるパラメータを制御しないので、 'turn'と' is_winner'を 'goto()'に渡すことはできません。あなたの他の質問については、この現在の質問を閉じて、追加したコードを追加して新しい質問をすることをお勧めします。 – cdlane

関連する問題