STEP 11: Let's use a random x-coordinate to set the position of the falling food!

  • Change the name of the random integer variable from my_var to rand_x.
  • Change the range in random.randint() from (1, 10) to (-250, 250).

Now, your program creates and stores a random number between -250 and 250 every 2 seconds! We will use that number to create a new piece of fish food and place it on the stage every 2 seconds.

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

stage.set_background("underwater") sprite = codesters.Sprite("fish") sprite.go_to(-100, -220) stage.disable_floor() stage.set_gravity(4) sprite.set_gravity_off() 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) score = 0 #my_display = codesters.Display(my_var, x, y) score_board = codesters.Display(score, -200, 200) def interval(): my_var = random.randint(1, 10) # add any other actions... stage.event_interval(interval, 2)
  • Run Code
  • Submit Work
  • Next Activity
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)