Python 蟒蛇龟

Python 蟒蛇龟,python,turtle-graphics,tic-tac-toe,Python,Turtle Graphics,Tic Tac Toe,所以我是python新手,我用一个Ai编程了一个tic-tac-toe,可以和你对抗。所以一切正常,但我用文本框通知Ai玩家选择了什么。现在我想升级我的游戏,这样玩家就可以点击他想要填充的框,而不是在文本框中输入。所以我的想法是使用屏幕上的onscreenclick(),但我遇到了一些问题onscreenclick()返回在画布上单击的坐标,我想使用一个函数来确定播放器在哪个框中单击 我明白了: from turtle import * def whichbox(x,y): #obvious

所以我是python新手,我用一个Ai编程了一个tic-tac-toe,可以和你对抗。所以一切正常,但我用文本框通知Ai玩家选择了什么。现在我想升级我的游戏,这样玩家就可以点击他想要填充的框,而不是在文本框中输入。所以我的想法是使用屏幕上的
onscreenclick()
,但我遇到了一些问题
onscreenclick()
返回在画布上单击的坐标,我想使用一个函数来确定播放器在哪个框中单击 我明白了:

from turtle import * 

def whichbox(x,y): #obviously i got 9 boxes but this is just an example for box 1
    if x<-40 and x>-120:
        if y>40 and y<120:
            return 1
        else:
            return 0
    else:
        return 0

box=onscreenclick(whichbox)
print(box)
从海龟进口*
def whichbox(x,y):#显然我有9个盒子,但这只是盒子1的一个例子
如果x-120:

如果y>40且y假设您已经在turtle模块中命名了Screen(),那么您应该

screen.onscreenclick(whichbox)
而不是:

onscreenclick(whichbox)
例如:

from turtle import Turtle, Screen
turtle = Turtle()
screen = Screen()

def ExampleFunction():
    return 7

screen.onscreenclick(ExampleFunction)
此外,jasonharper说onscreenclick()函数无法返回任何值是正确的。因此,您可以在函数whichbox()中包含一个print函数,以便打印出一个值,如:

def whichbox(x,y): 
    if x<-40 and x>-120:
        if y>40 and y<120:
            print(1)
            return 1
        else:
            print(0)
            return 0
    else:
        print(0)
        return 0

它创建了一个lambda函数,该函数将(x,y)从onscreenclick()提供给包含whichbox()的print语句。

下面是一个简化的示例。如果单击正方形,它将在控制台窗口中打印其编号,从0到8:

from turtle import Turtle, mainloop

CURSOR_SIZE = 20
SQUARE_SIZE = 60

def drawBoard():
    for j in range(3):
        for i in range(3):
            square = Turtle('square', visible=False)
            square.shapesize(SQUARE_SIZE / CURSOR_SIZE)
            square.fillcolor('white')
            square.penup()
            square.goto((i - 1) * (SQUARE_SIZE + 2), (j - 1) * (SQUARE_SIZE + 2))

            number = j * 3 + i
            square.onclick(lambda x, y, number=number: whichsquare(number))
            square.showturtle()

def whichsquare(number):
    print(number)

drawBoard()

mainloop()

不涉及位置解码——我们让turtle为我们处理。

whichbox
无法返回
None
。此外,负x坐标是可疑的。你不能在调用whichbox时打印x和y吗?你需要获得x,y坐标并将它们发送到whichbox。是的,我可以打印x和y,但这不是我的目标。Sry如果这个问题是愚蠢的或明显的,但正如我所说,我不擅长编程,那么我怎么能在不完成程序的情况下将x和y放入变量中?这不是我使用的软件包,但我是说你不会向whichbox发送任何东西。如果onscreenclick的结果是(x,y),则需要执行以下操作:box=whichbox(onscreenclick[0],onscreenclick[1])或更改函数的预期值。按照当前编写的方式,您将变量whichbox(不是变量)发送到ScreenClick()上的函数
只会记住指定的函数,以便以后在单击时使用。它不会,也基本上不能,返回任何关于特定点击的信息,因为还没有发生点击。您要对单击做出响应的任何操作都应该完全包含在
whichbox()
函数中-从中返回任何内容都是毫无意义的。
from turtle import Turtle, mainloop

CURSOR_SIZE = 20
SQUARE_SIZE = 60

def drawBoard():
    for j in range(3):
        for i in range(3):
            square = Turtle('square', visible=False)
            square.shapesize(SQUARE_SIZE / CURSOR_SIZE)
            square.fillcolor('white')
            square.penup()
            square.goto((i - 1) * (SQUARE_SIZE + 2), (j - 1) * (SQUARE_SIZE + 2))

            number = j * 3 + i
            square.onclick(lambda x, y, number=number: whichsquare(number))
            square.showturtle()

def whichsquare(number):
    print(number)

drawBoard()

mainloop()