我正在尝试用Python乌龟图形制作一个Connect 4游戏,并且进展顺利,但是随后发生了这种情况: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")
出于某种奇怪的原因,当我在它上面的所有选项卡上都复制了一部分时,请忽略最后一点。
答案 0 :(得分:1)
一切顺利
当您以三种不同的方式导入同一模块时,您知道事情进展并不顺利:
import turtle
from turtle import *
from turtle import Turtle, Screen
xy根本不会改变!
它正在 进行更改,但是您的代码中没有任何内容可以显示更改。唯一查看xy
的语句:
if xy == 1:
textinput("test","dis is 1")
在xy
更改之前完成,并且不再运行。让我们添加一些代码以在龟窗口中显示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()