我的任务是用画一个红绿灯。每次我撞到这个空间时,都会以不同的颜色移动,交通信号灯也会发生变化。我已经成功完成了这项任务。但是有一些额外的任务我无法完成。
1(用R,G和B改变的颜色。例如:处于绿灯(底部(位置。但是我想通过按 R 将其更改为红色。
2(我想用+和-更改的笔大小。
这是我的代码。我只是无法编写额外的行来完成这两项任务。
import turtle # Tess becomes a traffic light.
turtle.setup(400,500)
wn = turtle.Screen()
wn.title("Tess becomes a traffic light!")
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
def draw_housing():
""" Draw a nice housing to hold the traffic lights """
tess.pensize(3)
tess.color("black", "darkgrey")
tess.begin_fill()
tess.forward(80)
tess.left(90)
tess.forward(200)
tess.circle(40, 180)
tess.forward(200)
tess.left(90)
tess.end_fill()
draw_housing()
tess.penup()
# Position tess onto the place where the green light should be
tess.forward(40)
tess.left(90)
tess.forward(50)
# Turn tess into a big green circle
tess.shape("circle")
tess.shapesize(3)
tess.fillcolor("green")
# A traffic light is a kind of state machine with three states,
# Green, Orange, Red. We number these states 0, 1, 2
# When the machine changes state, we change tess' position and
# her fillcolor.
# This variable holds the current state of the machine
state_num = 0
def advance_state_machine():
global state_num
if state_num == 0: # Transition from state 0 to state 1
tess.forward(70)
tess.fillcolor("orange")
state_num = 1
elif state_num == 1: # Transition from state 1 to state 2
tess.forward(70)
tess.fillcolor("red")
state_num = 2
else: # Transition from state 2 to state 0
tess.back(140)
tess.fillcolor("green")
state_num = 0
# Bind the event handler to the space key.
wn.onkey(advance_state_machine, "space")
wn.listen() # Listen for events
wn.mainloop()
import turtle # Tess becomes a traffic light.
turtle.setup(400,500)
wn = turtle.Screen()
wn.title("Tess becomes a traffic light!")
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
def draw_housing():
""" Draw a nice housing to hold the traffic lights """
tess.pensize(3)
tess.color("black", "darkgrey")
tess.begin_fill()
tess.forward(80)
tess.left(90)
tess.forward(200)
tess.circle(40, 180)
tess.forward(200)
tess.left(90)
tess.end_fill()
draw_housing()
tess.penup()
# Position tess onto the place where the green light should be
tess.forward(40)
tess.left(90)
tess.forward(50)
# Turn tess into a big green circle
tess.shape("circle")
tess.shapesize(3)
tess.fillcolor("green")
# A traffic light is a kind of state machine with three states,
# Green, Orange, Red. We number these states 0, 1, 2
# When the machine changes state, we change tess' position and
# her fillcolor.
# This variable holds the current state of the machine
state_num = 0
def advance_state_machine():
global state_num
if state_num == 0: # Transition from state 0 to state 1
tess.forward(70)
tess.fillcolor("orange")
state_num = 1
elif state_num == 1: # Transition from state 1 to state 2
tess.forward(70)
tess.fillcolor("red")
state_num = 2
else: # Transition from state 2 to state 0
tess.back(140)
tess.fillcolor("green")
state_num = 0
def blue():tess.fillcolor('blue')
def red():tess.fillcolor('red')
def green():tess.fillcolor('green')
def bigger():tess.shapesize(tess.shapesize()[0]+1)
def smaller():
try:tess.shapesize(tess.shapesize()[0]-1) # try so that it wont return exception if the size is 1. size cant be less than 1
except:pass
# Bind the event handler to the space key.
wn.onkey(advance_state_machine, "space")
wn.onkey(red, "r") # press "r" key to change the color to red
wn.onkey(green, "g") # press "g" key to change the color to green
wn.onkey(blue, "b") # press "b" key to change the color to blue
wn.onkey(bigger, "+") # press "+" key to increase the circle size
wn.onkey(smaller, "s") # press "s" key to decrease the circle size. i tried minus key but didnt work.
wn.listen() # Listen for events
#wn.mainloop()
我对您的要求的解释与@avalanche不同 - 我的解决方案如下。 这一切仍然取决于添加更多关键事件(例如空格键事件(,就像@avalanche显示的那样 +1 为他:
""" Tess becomes a traffic light. """
from turtle import Turtle, Screen
def draw_housing():
""" Draw a nice housing to hold the traffic lights """
tess.pensize(3)
tess.color('black', 'darkgrey')
tess.begin_fill()
tess.forward(80)
tess.left(90)
tess.forward(200)
tess.circle(40, 180)
tess.forward(200)
tess.end_fill()
wn = Screen()
wn.setup(400, 500)
wn.title("Tess becomes a traffic light!")
wn.bgcolor('lightgreen')
tess = Turtle()
draw_housing()
# Position tess onto the place where the green light should be
tess.penup()
tess.left(90)
tess.forward(40)
tess.left(90)
tess.forward(50)
# Turn tess into a green circle
tess.shape('circle')
tess.shapesize(3)
tess.fillcolor('green')
# A traffic light is a kind of state machine with three states,
# Green, Amber, Red. We number these states 0, 1, 2
# When the machine changes state, we change tess' position and
# her fillcolor.
SLOW, STOP, GO = range(3)
# Y position, color, next state; 'orange' filling in for 'amber'
STATE_MACHINE = {
SLOW: (120, 'orange', STOP),
STOP: (190, 'red', GO),
GO: (50, 'green', SLOW)
}
# This variable holds the current state of the machine
state_num = SLOW
def advance_state_machine():
global state_num
position, color, next_state = STATE_MACHINE[state_num]
tess.sety(position)
tess.fillcolor(color)
state_num = next_state
def bigger():
stretch_wid, stretch_len, outline = tess.shapesize()
tess.shapesize(stretch_wid, stretch_len, outline + 1)
def smaller():
stretch_wid, stretch_len, outline = tess.shapesize()
if outline > 0:
tess.shapesize(stretch_wid, stretch_len, outline - 1)
def stop():
global state_num
state_num = STOP
advance_state_machine()
def slow():
global state_num
state_num = SLOW
advance_state_machine()
def go():
global state_num
state_num = GO
advance_state_machine()
# Bind the event handlers
wn.onkey(advance_state_machine, 'space')
wn.onkey(stop, 'r') # press 'r' key to change the light to red
wn.onkey(slow, 'y') # press 'y' key to change the light to yellow
wn.onkey(go, 'g') # press 'g' key to change the light to green
wn.onkey(bigger, 'plus') # press '+' key to increase the circle size
wn.onkey(smaller, 'minus') # press '-' key to decrease the circle size.
wn.listen() # Listen for events
wn.mainloop()
一个区别是"r"、"y"和"g"键将灯光推进到该颜色状态,而不仅仅是更改当前的光标颜色。 "+"和"-"键更改笔轮廓大小,而不是光标大小。
另外,我重新设计了您的状态机逻辑和其他细节。