Python turtle事件中未设置全局变量

2024-04-23 21:02:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用Python海龟图形制作一个connect4游戏,它进行得很顺利,但后来发生了这样的事情:xy根本不会改变!代码如下:

import turtle
from turtle import *
from turtle import Turtle, Screen
speed(0)
xy = 1
def circle1 ():
    forward(90)
    fillcolor("white")
    begin_fill()
    circle(40)
    end_fill()
def getCoords(x,y):
    global xy
    #print("( " + str(x) + ", " + str(y) + " )")
    if y < 252 and x < -316:
        print ("test")
        xy = 1
    elif y < 252 and x > -316 and x < -187:
        xy = 2
    elif y < 252 and x > -187 and x < -59:
        xy = 3
    elif y < 252 and x > -59 and x < 65:
        xy = 4
    elif y < 252 and x > 65 and x < 194:
        xy = 5
    elif y < 252 and x > 194 and x < 327:
        xy = 6
    elif y < 252 and x > 327 and x < 453:
        xy = 7
window = Screen()
wn = window
wn.screensize()
wn.setup(width = 1.0, height = 1.0)
begin_fill()
width(5)
penup()
goto(-450,250)
right(90)
pendown()
forward(580)
left(90)
forward(450*2)
left(90)
forward(580)
left(90)
forward(450*2)
end_fill()
right(180)
forward(30)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
goto(-450,250)
left(90)
forward(160)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
penup()
goto(-450,250)
pendown()
left(90)
forward(290)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
penup()
goto(-450,250)
pendown()
left(90)
forward(410)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
penup()
goto(-450,250)
pendown()
left(90)
forward(540)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
penup()
goto(-450,250)
pendown()
left(90)
forward(670)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
penup()
goto(-450,250)
pendown()
left(90)
forward(800)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
turtle.onscreenclick(getCoords)
turtle.listen()
if xy == 1:
    textinput("test","dis is 1")

出于某种奇怪的原因,当我将所有内容都添加到标签上时,复制了其中的一部分,所以忽略最后一点。你知道吗


Tags: andrightleftfillendforwardwhitexy
1条回答
网友
1楼 · 发布于 2024-04-23 21:02:05

it was going well

你知道当你以三种不同的方式导入同一个模块时,事情并不是很顺利:

import turtle
from turtle import *
from turtle import Turtle, Screen

xy simply won't change!

它正在改变,但是在你的代码中没有任何东西能够显示改变。查看xy的唯一语句:

if xy == 1:
    textinput("test","dis is 1")

xy更改之前完成,并且不再运行。让我们添加一些代码来在turtle窗口中显示xy,这样您就可以看到您的点击正在产生效果。我们会清理你的代码:

from turtle import *

def circle6():
    fillcolor("white")
    for _ in range(6):
        begin_fill()
        circle(40)
        end_fill()
        forward(90)

def getCoords(x, y):
    global xy

    if y < 252:
        if x < -316:
            xy = 1
        elif -316 < x < -187:
            xy = 2
        elif -187 < x < -59:
            xy = 3
        elif -59 < x < 65:
            xy = 4
        elif 65 < x < 194:
            xy = 5
        elif 194 < x < 327:
            xy = 6
        elif 327 < x < 453:
            xy = 7

        marker.undo()
        marker.write("xy = {}".format(xy), xy, font=('Arial', 18, 'normal'))

xy = 0

wn = Screen()
wn.setup(width=1.0, height=1.0)

marker = Turtle(visible=False)
marker.penup()
marker.goto(0, 300)
marker.write("xy = {}".format(xy), font=('Arial', 18, 'normal'))

hideturtle()
speed('fastest')
penup()

goto(-450, 250)
right(90)

begin_fill()
forward(580)
left(90)
forward(450 * 2)
left(90)
forward(580)
left(90)
forward(450 * 2)
end_fill()

left(90)

for distance in range(25, 815, 130):

    goto(-450, 250)
    left(90)
    forward(distance)
    right(90)
    forward(60)

    circle6()

wn.onscreenclick(getCoords)

wn.mainloop()

enter image description here

相关问题 更多 >