Extend: Let's assign our newest event to a brand new key: "w", for width!

Remember, the line that reads stage.event_key("a",·a_key)¬ is called the event handler.

  • In the line that reads: def·a_key():, change the event name to w_key().
  • In the event handler, change the first argument to "w" .
  • Change the second argument in the event handler to w_key.

To navigate the page using the TAB key, first press ESC to exit the code editor.

stage.set_background_color("black") # sprite = codesters.Point(x, y) sprite = codesters.Point(50, 100) sprite.set_color("green") sprite.pen_down() def right_key(): sprite.move_right(20) # add other actions... stage.event_key("right", right_key) def left_key(): sprite.move_left(20) # add other actions... stage.event_key("left", left_key) def up_key(): sprite.move_up(20) # add other actions... stage.event_key("up", up_key) def down_key(): sprite.move_down(20) # add other actions... stage.event_key("down", down_key) my_list = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"] def space_bar(): my_var = random.choice(my_list) sprite.set_color(my_var) # add other actions... stage.event_key("space", space_bar) def a_key(): pass # delete after adding indented code width = int(sprite.ask("Enter a number from 1 to 10.")) sprite.pen_width(width) # add other actions... stage.event_key("a", a_key)
  • Run Code
  • Submit Work
  • Next Activity
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)