我有一些困难,让我的程序等待用户键,空格键,开始程序。我必须建立一个Python程序使用海龟图形来模拟交通灯,在那里,程序开始执行与一个空白的屏幕。还创建一个keypress事件("space")和处理程序,用绿灯绘制停止灯,用5秒的计时器事件绘制事件处理程序。这是我所拥有的。有人有什么建议吗?
import turtle
wn = turtle.Screen()
wn.title("traffic light")
wn.bgcolor("sky blue")
def draw_housing():
house = turtle.Turtle()
house.pensize(3)
house.color("black", "darkgrey")
house.begin_fill()
house.forward(80)
house.left(90)
house.forward(200)
house.circle(40, 180)
house.forward(200)
house.left(90)
house.end_fill()
def draw_post():
post = turtle.Turtle()
post.pensize(3)
post.color("black")
post.penup()
post.goto(50,0)
post.pendown()
post.begin_fill()
for i in range(2):
post.right(90)
post.forward(80)
post.right(90)
post.forward(25)
post.end_fill()
# This function will draw 3 black lights which will work as off signal lights
def draw_off_lights():
distance = 50
for i in range(3):
off_turtle = turtle.Turtle()
off_turtle.speed(0)
off_turtle.penup()
off_turtle.forward(40)
off_turtle.left(90)
off_turtle.forward(distance)
off_turtle.shape("circle")
off_turtle.shapesize(3)
off_turtle.fillcolor("white")
distance+=70
draw_post()
draw_housing()
draw_off_lights()
light = turtle.Turtle()
light.penup()
# Position first onto the place where the green light should be
light.forward(40)
light.left(90)
light.forward(50)
light.shape('circle')
light.shapesize(3)
light.fillcolor('green')
# This variable holds the current state of the machine
state_num = 0
def state_machine():
global state_num
if state_num == 0: # Transition from state 0 to state 1
state_num = 1
light.forward(70)
light.fillcolor('yellow')
wn.ontimer(state_machine, '2000')
elif state_num == 1: # Transition from state 1 to state 2
state_num = 2
light.forward(70)
light.fillcolor('red')
wn.ontimer(state_machine, '5000')
else: # Transition from state 2 to state 0
state_num = 0
light.back(140)
light.fillcolor('green')
wn.ontimer(state_machine, '5000')
state_machine()
wn.onkey(draw_post, 'space')
wn.listen
wn.mainloop()发布于 2022-05-02 16:34:38
下面是我针对讨论的问题(listen(),将对函数的调用绑定到一个由onkey触发的处理程序,state_num未定义)的代码的重做,它还解决了其他一些您可能会发现的问题,并试图通过使绘图以窗口为中心而不是仅以中间位置为中心来简化代码:
from turtle import Screen, Turtle
DISTANCE = 70
def draw_housing():
housing.begin_fill()
housing.forward(40)
housing.left(90)
housing.forward(200)
housing.circle(40, 180)
housing.forward(200)
housing.left(90)
housing.forward(40)
housing.end_fill()
def draw_post():
post.begin_fill()
for _ in range(2):
post.forward(12.5)
post.right(90)
post.forward(80)
post.right(90)
post.forward(12.5)
post.end_fill()
# This function will draw 3 black lights which will work as off signal lights
def draw_off_lights():
off_turtle.forward(50)
for _ in range(3):
off_turtle.stamp()
off_turtle.forward(DISTANCE)
def user_started():
screen.onkey(None, 'space') # prevent double activation
draw_post()
draw_housing()
draw_off_lights()
# Position first onto the place where the green light should be
light.forward(50 + DISTANCE*2)
light.showturtle()
state_machine()
screen = Screen()
screen.title("traffic light")
screen.bgcolor("sky blue")
housing = Turtle()
housing.hideturtle()
housing.pensize(3)
housing.fillcolor("darkgrey")
post = Turtle()
post.hideturtle()
post.pensize(3)
off_turtle = Turtle()
off_turtle.hideturtle()
off_turtle.shape("circle")
off_turtle.shapesize(3)
off_turtle.penup()
off_turtle.setheading(90)
light = off_turtle.clone()
light.fillcolor('green')
# This variable holds the current state of the machine
state_num = 2
def state_machine():
global state_num
if state_num == 0: # Transition from state 0 to state 1
state_num = 1
light.forward(DISTANCE)
light.fillcolor('yellow')
screen.ontimer(state_machine, 2000)
elif state_num == 1: # Transition from state 1 to state 2
state_num = 2
light.forward(DISTANCE)
light.fillcolor('red')
screen.ontimer(state_machine, 5000)
else: # Transition from state 2 to state 0
state_num = 0
light.back(DISTANCE*2)
light.fillcolor('green')
screen.ontimer(state_machine, 5000)
screen.onkey(user_started, 'space')
screen.listen()
screen.mainloop()https://stackoverflow.com/questions/72063070
复制相似问题