我的任务是用乌龟绘制交通灯。每当我击中太空时,乌龟都会以不同的颜色移动,交通灯会发生变化。我已成功完成此任务。但是还有一些我无法完成的额外任务。
1)用R,G和B改变乌龟的颜色。例如:乌龟处于绿灯(底部)位置。但我想按R键将其改为红色。
2)我想用+和 - 来改变我的乌龟的pensize。
这是我的代码。我只是无法写出能够完成这两项任务的额外行。
@using (Ajax.BeginForm("AddComment", new { parametar = 0 }, new AjaxOptions()
{
UpdateTargetId = "beforeThis",
InsertionMode = InsertionMode.InsertBefore,
HttpMethod = "POST",
Url = Url.Action("AddComment")
}))
{
@Html.TextArea("Contents")
<input type="submit" value="Send" class="postavi btn btn-primary" />
}
答案 0 :(得分:1)
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()
答案 1 :(得分:0)
我以不同于@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'键将灯光推进到该颜色状态,而不仅仅是改变当前的光标颜色。并且'+'和' - '键改变笔轮廓大小,而不是光标大小。
另外,我重写了你的状态机逻辑和其他细节。