交通灯乌龟Python

发布于 2025-01-25 01:38:24 字数 2330 浏览 2 评论 0原文

让我的程序等待用户键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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的快感 2025-02-01 01:38:24

以下是我对您的代码进行的重新处理,该代码解决了讨论的问题(listan(),将绘制功能的调用捆绑到一个由onkeystate_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()

Below is my rework of your code that addresse issues discussed (listen(), bundling calls to drawing functions into one handler that is triggered by onkey, 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:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文