交通灯乌龟Python
让我的程序等待用户键Spacebar开始程序,我遇到了一些麻烦。我必须使用Turtle图形来构建一个Python程序,以模拟该程序开始使用空白屏幕执行的交通信号灯。还可以创建一个按键事件(“空间”)和处理程序,以绘制带有绿灯的停止灯,并带有一个带有计时器事件的事件处理程序,该时间表事件在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()
I am having some trouble with making my program wait for the user key, spacebar, to begin the program. I have to build a Python program using turtle graphics to simulate a traffic light where the program starts execution with a blank screen. Also create a keypress event ("space") and handler to draw a stop light with a green light and an event handler with a timer event that goes off in 5 seconds. Here is what I have. anyone have any suggestions?
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是我对您的代码进行的重新处理,该代码解决了讨论的问题(
listan()
,将绘制功能的调用捆绑到一个由onkey
,state_num 未定义)它还解决了您可能会发现的其他一些问题,并通过使绘图中心在窗口中而不是中心的右侧来简化代码:
Below is my rework of your code that addresse issues discussed (
listen()
, bundling calls to drawing functions into one handler that is triggered byonkey
,state_num
not defined) It also addresses some other issues you might find of use and tries to simplifies the code by making the drawing centered on the window instead of just right of center: