每当我尝试使用全局变量从 x 切换到 o 时,都会收到一条错误消息,提示“Turn is undefined”。所以我把所有东西都移到了一个函数中,以摆脱对全局的需求,但它仍然不起作用。然后在设置了一些打印后,我意识到当您单击多个框时,board[] 会被完全重写,而不是仅仅分配一个值并保留该值。我该如何解决这些问题?单击 2 个不同的方块后,请检查控制台以获得更清晰的图片。
import turtle
from turtle import *
pen = turtle.Turtle()
pen.speed(0)
setup(601,601)
#Creating Menu option buttons
def button(length):
for i in range(4):
pen.forward(length)
pen.left(90)
def column(n, length):
pen.penup()
pen.goto(-100,250)
pen.pendown()
pen.left(270)
for i in range(n):
button(length)
pen.forward(length)
pen.penup()
pen.left(90)
pen.forward(n * length)
pen.left(180)
pen.pendown()
column(5, 100)
#Menu Options
pen.penup()
pen.goto(-100, 190)
pen.write("START GAME", font=("Arial",12,"normal"))
pen.penup()
pen.goto(-75, 90)
pen.write("RULES", font=("Arial",12,"normal"))
pen.penup()
pen.goto(-97, -10)
pen.write("HIGH SCORE", font=("Arial",12,"normal"))
pen.penup()
pen.goto(-65, -110)
pen.write("FAQ", font=("Arial",12,"normal"))
pen.penup()
pen.goto(-93, -210)
pen.write("QUIT GAME", font=("Arial",12,"normal"))
def runGame():
pen = turtle.Turtle()
speed(0)
setup(1366,738)
title("Group 3's tictactoe")
pensize(15)
#Rows
def dRow():
up()
goto(-500,-100)
down()
forward(1000)
up()
goto(-500, 100)
down()
forward(1000)
dRow()
#columns
def dCol():
up()
goto(-175,-400)
left(90)
down()
forward(1000)
up()
goto(175, -400)
down()
forward(1000)
dCol()
#Win conditions
def place(x,y):
global turn
#Creating Board
board = ["","","",
"","","",
"","","",]
turn = "X"
point = ""
#create X's
def Xs(x, y):
up()
goto(x+30, y-35)
setheading(130)
down()
forward(100)
up()
goto(x+30, y+40)
setheading(230)
down()
forward(100)
up()
print(x, y)
#Create O's
def Os(x, y):
up()
goto(x,y-80)
setheading(0)
down()
circle(80)
up()
print(x, y)
#Check what square was clicked and assign that an (x or O) value to that point
if -526<x<-182 and 360>y>107:
point = 0
print("This is square: ",point)
board[int(point)] = turn
print("this board space is: ", board[point], board)
elif -168<x<167 and 360>y>107:
point = 1
print("This is square: ",point)
board[int(point)] = turn
print("this board space is: ", board[point], board)
elif 182<x<504 and 360>y>107:
point = 2
print("This is square: ",point)
board[int(point)] = turn
print("this board space is: ", board[point], board)
elif -506<x<-184 and 92>y>-92:
point = 3
print("This is square: ",point)
board[int(point)] = turn
print("this board space is: ", board[point], board)
elif -167<x<167 and 92>y>-92:
point = 4
print("This is square: ",point)
board[int(point)] = board[int(point)] + str(turn)
print("this board space is: ", board[point], board)
elif 185<x<505 and 92>y>-92:
point = 5
print("This is square: ",point)
board[int(point)] = turn
print("this board space is: ", board[point], board)
elif -504<x<-182 and -108>y>-360:
point = 6
print("This is square: ",point)
board[int(point)] = turn
print("this board space is: ", board[point], board)
elif -167<x<166 and -108>y>-360:
point = 7
print("This is square: ",point)
board[int(point)] = board[int(point)] + str(turn)
print("this board space is: ", board[point], board)
elif 185<x<531 and -108>y>-360:
point = 8
print("This is square: ",point)
board[int(point)] = board[int(point)] + str(turn)
print("this board space is: ", board[point], board)
else:
print("Do something")
print("The current entire board is: ",board)
def drawPieces(board):
for piece in board:
if piece == "X":
Xs(x,y)
elif board == "O":
Os(x,y)
def clicked(x,y):
if board[0] == "X" and board[4] == "X" and board[8] == "X":
style = ('Courier', 80, 'bold')
write('You WIN!', font=style, align='center')
else:
global turn
if turn == "X":
turn = "O"
drawPieces(board)
else:
turn = "X"
drawPieces(board)
print("This turn is: ",turn)
clicked(x, y)
onscreenclick(place)
mainloop()
#Making options clickable
def btnclick(x,y):
if -100 < x < 1 and 250 > y > 150:
print("Start Game")
print(x, y)
turtle.clearscreen()
runGame()
elif -100 < x < 1 and 150 > y > 50:
print("Rules")
print(x, y)
turtle.clearscreen()
screen = turtle.Screen()
answer = screen.textinput("Welcome to Our GAME!", "Whats your name?")
elif -100 < x < 1 and 50 > y > -50:
print("Highscore")
print(x, y)
turtle.clearscreen()
elif -100 < x < 1 and -50 > y > -150:
print("Hi")
print(x, y)
turtle.clearscreen()
elif -100 < x < 1 and -150 > y > -250:
print("Hi")
print(x, y)
turtle.clearscreen()
elif -100 < x < 1 and -250 > y > -350:
print("Hi")
print(x, y)
turtle.clearscreen()
else:
print("Click One Of The Options!")
print(x, y)
turtle.onscreenclick(btnclick, 1)
turtle.listen()
turtle.done()