1

每当我尝试使用全局变量从 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()
4

1 回答 1

2

您的代码中的主要问题是,每次调用时都会borad初始化。turnplace

def place(x,y):
        global turn
        #Creating Board
        board = ["","","",
                 "","","",
                 "","","",] 
        turn = "X"
        point = ""

"X"因此,每次新回合开始时都会创建一个新棋盘并设置回合。

在全局命名空间中创建一个bordandturn并在place:

#Creating Board
board = ["","","",
              "","","",
              "","","",] 
turn = "X"

def runGame():
    # [...]

    #Win conditions
    def place(x,y):
        global turn, board

        point = ""

        #create X's
        # [...]

此外,在clicked. 使用全局变量boardturn单击。drawPieces根本不起作用,因为x永远y不会改变,所有的部分都在同一个位置绘制。画出新的作品就足够了。Xs分别画Osclicked

def runGame():
    # [...]

    #Win conditions
    def place(x,y):
        global turn, board

        point = ""
        # [...]

        def clicked(x,y):
            global board, turn
            if board[0] == "X" and board[4] == "X" and board[8] == "X":
                style = ('Courier', 80, 'bold')
                write('You WIN!', font=style, align='center')
            else:
                if turn == "X":
                    Xs(x,y)
                    turn = "O"
                else:
                    Os(x,y)
                    turn = "X"
                print("This turn is: ",turn)
        clicked(x, y)

完整代码:

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"))

#Creating Board
board = ["","","",
              "","","",
              "","","",] 
turn = "X"

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, board

        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 piece == "O":
                    Os(x,y)

        def clicked(x,y):
            global board, turn
            if board[0] == "X" and board[4] == "X" and board[8] == "X":
                style = ('Courier', 80, 'bold')
                write('You WIN!', font=style, align='center')
            else:
                if turn == "X":
                    Xs(x,y)
                    turn = "O"
                else:
                    Os(x,y)
                    turn = "X"
                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()
于 2020-04-18T06:03:48.690 回答