STEP 5: Now that we've stored the sprite's rotation amount in the rotation variable, it's time to rotate more!
If you click the Console button, you can see the current rotation printed! Click again to close the console. Add Set Rotation indented inside the left_key() event. Change 45 to rotation + 3 . Run your code and press the left arrow key. You spin left!
When you press the left key, the code checks the current rotation of the hedgehog, then adds 3.
To navigate the page using the TAB key, first press ESC to exit the code editor.
stage.set_background("moon")
sprite = codesters.Sprite("hedgehog", 0, -210)
score = 0
#my_display = codesters.Display(my_var, x, y)
score_display = codesters.Display(score, -210, -210)
time = 60
#my_display = codesters.Display(my_var, x, y)
time_display = codesters.Display(time, 210, -210)
def left_key():
rotation = sprite.get_rotation()
print(rotation)
# add other actions...
stage.event_key("left", left_key)
t = codesters.Teacher()
sets = t.find_text('set_rotation')
sets_params = t.get_parameters_for_function('set_rotation')
func = t.find_block('def')
call = t.find_function('event_key')
try:
tval1 = sets_params[0][0].replace(" ", '')
except:
tval1 = "DNE"
try:
set_line = int(sets[0][0])
tval2b = t.get_indent_at_line(set_line)
except:
set_line = -1
tval2b = -1
try:
def_line = int(func[0][0])
except:
def_line = -1
try:
call_line = int(call[0][0])
except:
call_line = -1
t1 = TestObjective()
t1.add_success((tval1 == "rotation+3" or tval1 == "3+rotation") and set_line != -1 and def_line < set_line < call_line, "Great job!")
t1.add_failure(tval1 == "DNE", "Did you add Set Rotation with the event?")
t1.add_failure(tval1 != "rotation+3" and tval1 != "3+rotation", "Did you change the argument in .set_rotation?")
t1.add_failure(not (def_line < set_line < call_line), "Make sure your .set_rotation() command is inside your left key event.")
t1.add_failure(tval2b < 4, "Make sure your .set_rotation() command is indented 4 spaces in your left key event.")
t1.add_failure(tval2b > 4, "Make sure your .set_rotation() command is indented only 4 spaces inside your left key event.")
t4 = TestObjective()
t4.add_failure(len(call) != 1, "Oops! Did you delete your left_key event handler?")
tester = TestManager()
tester.add_test_list([t1, t4])
tester.run_tests()
tester.display_first_feedback()
Run Code
Activity Submitted!
Submit Work
Next Activity
Stop Running Code
Show Chart
Show Console
Reset Code Editor
Codesters How To (opens in a new tab)