将用户检查添加到 Python 海龟井字游戏


import turtle
#1)draw board
sc= turtle.Screen()
sc.setup(300,300)
import turtle
sc= turtle.Screen()
sc.setup(300,300)
turtle.speed(0)
turtle.pensize(10)
turtle.bgcolor("black")
turtle.pencolor("white")
turtle.penup()
turtle.goto(-150,50)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(-150,-50)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(-50,150)
turtle.setheading(-90)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(50,150)
turtle.pendown()
turtle.forward(300)
turtle.penup()

#2) X's and O's
turtle.pencolor("yellow")
def kreuz(x,y):
turtle.penup()
turtle.goto(x+30,y-45)
turtle.setheading(-45)
turtle.pendown()
turtle.forward(50)
turtle.penup()
turtle.goto(x+60,y-40)
turtle.setheading(-130)
turtle.pendown()
turtle.forward(50)
turtle.penup()
def kreis(x,y):
turtle.penup()
turtle.goto(x+50,y-80)
turtle.setheading(0)
turtle.pendown()
turtle.circle(20)
turtle.penup()

AlleTeile=["","","","","","","","",""]
nächsterZug="X"
def zeichneTeile (AlleTeile):
x = -150
y = 150
for einTeil in AlleTeile:
if einTeil=="X":
kreuz(x,y)
elif einTeil=="O":
kreis(x,y)
else:
print("leeres feld")
x=x+100
if x > 50:
x=-150
y=y-100

zeichneTeile(AlleTeile)
def geklickt(x,y):
global nächsterZug,AlleTeile
senkrecht= (x+150) // 100
waagrecht= (-y+150)// 100
Bereich= senkrecht+waagrecht*3
Bereich=int(Bereich)
print("Du klicktest auf Bereich-Nummer", Bereich)
AlleTeile[Bereich]=nächsterZug
if nächsterZug =="X":
nächsterZug="O"
else:
nächsterZug="X"
zeichneTeile(AlleTeile)
turtle.onscreenclick(geklickt)
turtle.mainloop()

我想在 Python 中使用 turtle 创建这个井字游戏,但我被卡住了。问题是,在所有 9 个字段都充满了点和十字架之后,我一直在游戏板上画点和十字架。我如何对此进行编码,以便在 9 圈(9 个字段(后不再可能继续绘制?

由于未使用的方块由AlleTeile中的空字符串组成,这很容易修复,因为空字符串在布尔上下文中False。 在geklickt()的顶部,在global语句之后:

if all(AlleTeile):
print("All taken!")
return

我们可以稍后在该函数中重用相同的技巧,以防止现有方块的重写。Bereich计算后,我们可以执行以下操作:

print("Du klicktest auf Bereich-Nummer", Bereich, end="")
if AlleTeile[Bereich]:
print(" -- already taken!")
return
else:
print()

以下是我对上述更改的完整返工,一些偶像,代码清理和Google提供的英文翻译(和常识(:

from turtle import Screen, Turtle
# X's and O's
def cross(x, y):
turtle.penup()
turtle.goto(x + 30, y - 35)
turtle.setheading(-45)
turtle.pendown()
turtle.forward(50)
turtle.penup()
turtle.goto(x + 60, y - 30)
turtle.setheading(-130)
turtle.pendown()
turtle.forward(50)
turtle.penup()
def nought(x, y):
turtle.penup()
turtle.goto(x + 50, y - 70)
turtle.setheading(0)
turtle.pendown()
turtle.circle(20)
turtle.penup()
# Playing the game
def drawSquares():
screen.tracer(False)  # because this routine is a graphics bottleneck
x, y = -150, 150
for square in AllSquares:
if square == "X":
cross(x, y)
elif square == "O":
nought(x, y)
x += 100
if x > 50:
x = -150
y -= 100
screen.tracer(True)
def clicked(x, y):
global turn
if all(AllSquares):
print("All taken!")
return
vertical = (x + 150) // 100
horizontal = (150 - y) // 100
Area = int(vertical + horizontal * 3)
print("You clicked area number", Area, end="")
if AllSquares[Area]:
print(" -- already taken!")
return
else:
print()
AllSquares[Area] = turn
if turn == "X":
turn = "O"
else:
turn = "X"
drawSquares()
# Draw the board
screen = Screen()
screen.setup(330, 330)
screen.bgcolor("black")
turtle = Turtle(visible=False)
turtle.pensize(10)
turtle.color("white")
turtle.speed('fastest')
turtle.penup()
turtle.goto(-150, 50)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(-150, -50)
turtle.pendown()
turtle.forward(300)
turtle.setheading(-90)
turtle.penup()
turtle.goto(-50, 150)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(50, 150)
turtle.pendown()
turtle.forward(300)
turtle.penup()
# Start the game
turtle.color("yellow")
AllSquares = ["", "", "", "", "", "", "", "", ""]
turn = "X"
drawSquares()
screen.onscreenclick(clicked)
screen.mainloop()

最新更新