Python中的事件驱动编程:更改颜色&;乌龟的大小
我的任务是用乌龟画一个红绿灯。每次我撞到太空,乌龟都会以不同的颜色移动,交通灯也会改变。我已经成功地完成了这项任务。但还有一些额外的任务我无法完成 1) 用R、G和B改变海龟的颜色。例如:海龟处于绿灯(底部)位置。但我想按R把它改成红色 2) 我想用+和-,改变我海龟的养老金 这是我的代码。我就是写不出额外的行来完成这两项任务Python中的事件驱动编程:更改颜色&;乌龟的大小,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我的任务是用乌龟画一个红绿灯。每次我撞到太空,乌龟都会以不同的颜色移动,交通灯也会改变。我已经成功地完成了这项任务。但还有一些额外的任务我无法完成 1) 用R、G和B改变海龟的颜色。例如:海龟处于绿灯(底部)位置。但我想按R把它改成红色 2) 我想用+和-,改变我海龟的养老金 这是我的代码。我就是写不出额外的行来完成这两项任务 import turtle # Tess becomes a traffic light. turtle.setup(400,500) wn = t
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()
我对您的需求的解释与@avalanche不同——我的解决方案如下。这一切仍然取决于添加更多的关键事件(比如你的空格键事件),正如@avalanche为他展示的so+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”键将灯光推进到该颜色状态,而不仅仅是更改当前光标的颜色。“+”和“-”键改变的是画笔轮廓的大小,而不是光标的大小
此外,我还修改了状态机逻辑和其他细节。我发现“-”(和“+”)在Tk键绑定方面有特殊意义,所以请尝试使用wn.onkey(较小的“减”)
(和wn.onkey(较大的“加”)
)。
""" 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()