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

时间:2018-10-13 15:36:18

标签: python turtle-graphics

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创建乌龟这个井字游戏,但我被困住了。问题在于,在所有9个字段都填满了小数和十字后,我会继续在游戏板上绘制小数和十字。我该如何编码,以便9圈(9场)后不再能够继续绘图?

1 个答案:

答案 0 :(得分:0)

由于未使用的正方形在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()
相关问题