Python 以tic tac趾板游戏的绘图为中心

Python 以tic tac趾板游戏的绘图为中心,python,turtle-graphics,Python,Turtle Graphics,我的代码绘制网格并设置Xs和Os。我的问题是,它在屏幕的右上角完成所有操作。我想找到一个方法来做这一切,但中心。问题是,我希望这是尽可能的审美。一切正常,除了这个职位,我一点问题都没有。我找不到命令将我的板设置在屏幕中央 from turtle import* def tttDrawLineFromMidpoint(pen, orientation, lineLength): pen.setheading(orientation) pen.down() pen.fd(l

我的代码绘制网格并设置Xs和Os。我的问题是,它在屏幕的右上角完成所有操作。我想找到一个方法来做这一切,但中心。问题是,我希望这是尽可能的审美。一切正常,除了这个职位,我一点问题都没有。我找不到命令将我的板设置在屏幕中央

from turtle import*

def tttDrawLineFromMidpoint(pen, orientation, lineLength):
    pen.setheading(orientation)
    pen.down()
    pen.fd(lineLength/2)
    pen.bk(lineLength)
    pen.fd(lineLength/2)

def tttDrawXFromMidpoint(pen, cellSize):
        lineLength = 1.41 * cellSize
        tttDrawLineFromMidpoint(pen, 45, lineLength)
        tttDrawLineFromMidpoint(pen, 135, lineLength)

def tttDrawX(pen, cellsize, row, col):

        x=col*cellsize
        y=row*cellsize

        x+=cellsize/2
        y+=cellsize/2

        pen.up()
        pen.goto(x,y)

        xSize = cellsize/2
        tttDrawXFromMidpoint(pen, xSize)

def tttDrawCircleFromPoint(pen, orientation,lineLength):
        pen.setheading(orientation)
        pen.down()
        pen.up()
        pen.fd(lineLength/1.5)
        pen.lt(45)
        pen.bk(25)
        pen.down()
        pen.circle(lineLength/2)

def tttDrawOFromPoint(pen, cellSize):
        lineLength = cellSize
        tttDrawCircleFromPoint(pen,90, lineLength)

def tttDrawO(pen, cellsize, row, col):

        x=col*cellsize
        y=row*cellsize

        x+=cellsize/2
        y+=cellsize/2

        pen.up()
        pen.goto(x,y)

        oSize = cellsize/2
        tttDrawOFromPoint(pen, oSize)

def tttDrawLineFromEnd(pen, orientation, lineLength):
        pen.setheading(orientation)
        pen.down()
        pen.fd(lineLength)
        pen.bk(lineLength)

def tttDrawGrid(pen,cellSize):
    pen.ht()

    for i in range (2):
                pen.up()
                pen.goto(0, cellSize * (i+1))
                tttDrawLineFromEnd(pen,0,3*cellSize)
    for i in range(2):
                pen.up()
                pen.goto(cellSize*(i+1),0)
                tttDrawLineFromEnd(pen,90,3*cellSize)

def ttt(cellSize):
        marker = Turtle()
        marker.width(3)
        marker.color('purple')
        tttDrawGrid(marker,cellSize)

def tttDrawXOTest(cellSize):
        marker = Turtle()
        marker.width(3)
        marker.color('purple')
        tttDrawGrid(marker,cellSize)
        xTurtle = Turtle()
        xTurtle.width(3)
        xTurtle.color('orange')
        for row in range(3):
            for col in range(3):
                    tttDrawX(xTurtle, cellSize, row, col)

        xTurtle = Turtle()
        xTurtle.width(3)
        xTurtle.color('pink')
        for row in range(3):
            for col in range(3):
                    tttDrawO(xTurtle, cellSize, row, col)


t=Turtle()
t.ht()
ttt(100)
print("Let's Play Tic Tac Toe >:D")
print("X goes first!")
x = input("Choose a row between 0-2: ")
x = int(x)
x = x
y = input("Choose a coluumn between 0-2: ")
y = int(y)
tttDrawX(t,100,x,y)

print("O goes next!")
x = input("Choose a row between 0-2: ")
x = int(x)
x = x
y = input("Choose a coluumn between 0-2: ")
y = int(y)
tttDrawO(t,100,x,y)

exitonclick()

因为这个问题是关于将电路板居中的,所以我在下面的示例代码中删除了除电路板图纸以外的所有内容。关键是要在零点附近进行数学运算,因此tic-tac趾板中的两条线的位置实际上只因符号不同而不同:

from turtle import Turtle, Screen

def tttDrawLineFromEnd(pen, orientation, lineLength):
    pen.setheading(orientation)
    pen.down()
    pen.forward(lineLength)

def tttDrawGrid(pen, cellSize):
    for sign in (-1, 1):
        pen.up()
        pen.goto(-3 * cellSize / 2, sign * cellSize / 2)
        tttDrawLineFromEnd(pen, 0, 3 * cellSize)

    for sign in (-1, 1):
        pen.up()
        pen.goto(sign * cellSize / 2, -3 * cellSize / 2)
        tttDrawLineFromEnd(pen, 90, 3 * cellSize)

def ttt(cellSize):
    marker = Turtle(visible=False)
    marker.width(3)
    marker.color('purple')
    tttDrawGrid(marker, cellSize)

ttt(100)

screen = Screen()

screen.exitonclick()

使用turtle编程时使用的一个技巧是,在创建turtle之后,我调用
turtle.dot()
,在屏幕的正中央创建一个可见点,这样我就知道我是否在预期的位置绘制。然后,我在最后的代码中删除了这个调试辅助工具。

这是为了一个类,还是为了练习,还是为了制作一个好的游戏?因为海龟只在课堂作业中有用。家庭作业是创建圆并进行X和O测试。我已经做到了,只是它没有居中-_;-这个游戏是由几个任务编译而成的。每个家庭作业都是在做游戏的另一部分。