我做了一个tic / tac / toe游戏,我一直有一个缩进错误,关于如何有太多的标签和空格混合,但我尝试逐行重新缩进,它不起作用。我甚至把它放在“Sublime Text”中,它会自动重新缩进行或将空格变成制表符。它仍然无法正常工作。有没有人有任何建议,也许有一些明显的错误,我错过了搞乱整个事情?
您的代码中存在一些缩进错误,但我没有发现混合制表符和空格有任何问题。相反,压痕深度不一致,在某些地方,不正确。下面是您的代码清理,您应该能够复制并粘贴到文件中并运行:
from turtle import *
# draw board
pieces = ["", "", "", "", "", "", "", "", ""]
turn = "X"
setup(600, 600)
bgcolor("black")
pencolor("white")
hideturtle()
speed('fastest')
pensize(10)
penup()
# Horizontal bars
goto(-300, 100)
pendown()
forward(600)
penup()
goto(-300, -100)
pendown()
forward(600)
penup()
# Vertical bars
goto(-100, 300)
setheading(-90)
pendown()
forward(600)
penup()
goto(100, 300)
pendown()
forward(600)
penup()
pencolor("green")
# Draw noughts and crosses
def cross(x, y):
penup()
goto(x + 20, y - 20)
setheading(-45)
pendown()
forward(226)
penup()
goto(x + 180, y - 20)
setheading(-135)
pendown()
forward(226)
penup()
def nought(x, y):
penup()
goto(x + 100, y - 180)
setheading(0)
pendown()
circle(80)
penup()
def drawPieces(pieces):
x, y = -300, 300
for piece in pieces:
if piece == "X":
cross(x, y)
elif piece == "O":
nought(x, y)
x += 200
if x > 100:
x = -300
y -= 200
def clicked(x, y):
global turn, pieces
onscreenclick(None) # disable handler when inside handler!
column = (x + 300) // 200
row = (y - 300) // -200
square = int(row * 3 + column)
print("You clicked ", x, ",", y, " which is square ", square)
if pieces[square] == "":
pieces[square] = turn
if turn == "X":
turn = "O"
else:
turn = "X"
drawPieces(pieces)
else:
print("That square is already taken")
onscreenclick(clicked)
# Start the game
onscreenclick(clicked)
mainloop()