0

目前在 Python 3 中制作井字游戏。我现在的问题是我无法在用户点击的地方绘制圆圈。它没有给我任何错误,但是当我单击板时,它没有画一个圆圈:

from graphics import *
import random

def draw_board(winsizex, win):
    win.setCoords(0.0, 0.0, 3.0, 3.0)
    win.setBackground('white')
# Draw the vertical lines
    Line(Point(1,0), Point(1,3)).draw(win)
    Line(Point(2,0), Point(2,3)).draw(win)
# Draw the horizontal lines
    Line(Point(0,1), Point(3,1)).draw(win)
    Line(Point(0,2), Point(3,2)).draw(win)
    if winsizex < 100:
        win.close()
    if winsizex > 1000:
        win.close()

    def player_turns(draw_board, win):
        c = 9
    for i in range(c):
        if c%2==0:
            print("Player two take your turn")
            player1 = win.getMouse()
            play1x=player1.getX()
            play1y=player1.getY()
            #play1.draw(win)
            circ = Circle(Point(play1x, play1y), 30)
            circ.setOutline('black')
            circ.draw(win)

        else:
            print('Player one take your turn')
            play2 = win.getMouse()
            play2x=play2.getX()
            play2y=play2.getY()
            #play2.draw(win)
            circ = Circle(Point(play2x, play2y), 30)
            circ.setOutline('black')
            circ.draw(win)
        c=c-1
def main():
    winsizex = int(input("Input the size of the window: "))
    winsizey = winsizex
    win = GraphWin("Tic-Tac-Toe", winsizex, winsizey)
    draw_board(winsizex, win)
    player_turns(draw_board, win)



main()
4

1 回答 1

0

您的代码存在几个问题,但使圆圈不可见的问题是:

win.setCoords(0.0, 0.0, 3.0, 3.0)
...
circ = Circle(Point(play2x, play2y), 30)

您将坐标系更改为最大限制为 3.0,然后绘制了一个半径为 30 的圆。该圆被绘制,但它太大了,您看不到它。给定坐标系,0.3 的半径更真实。以下是我对您的示例的修改,以解决此问题和其他问题:

from graphics import *

def draw_board(winsizex, win):
    win.setCoords(0.0, 0.0, 3.0, 3.0)

    # Draw the vertical lines
    Line(Point(1, 0), Point(1, 3)).draw(win)
    Line(Point(2, 0), Point(2, 3)).draw(win)

    # Draw the horizontal lines
    Line(Point(0, 1), Point(3, 1)).draw(win)
    Line(Point(0, 2), Point(3, 2)).draw(win)

    if not 100 < winsizex < 1000:
        win.close()

def player_turns(win):
    for c in range(1, 10):
        if c % 2 == 1:
            print("Player one take your turn")
            player1 = win.getMouse()
            circ = Circle(player1, 0.3)
        else:
            print('Player two take your turn')
            player2 = win.getMouse()
            circ = Circle(player2, 0.3)

        circ.draw(win)

winsize = int(input("Input the size of the window: "))
win = GraphWin("Tic-Tac-Toe", winsize, winsize)
draw_board(winsize, win)
player_turns(win)
于 2017-11-11T05:35:31.180 回答