EXTEND 3: Now that we are creating a random speed every time we create food, let's use that speed!

  • In GRAPHICS, from add Set y Speed indented inside the interval event.
  • Change sprite to food. This assigns the .set_y_speed() command to the food we just created!
  • In .set_y_speed(), change 5 to rand_drop. The food now drops at a randomly picked speed.

Run your game. Now the food drops at different speeds each time!

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(0, -220) stage.disable_floor() score = 0 #my_display = codesters.Display(my_var, x, y) score_board = codesters.Display(score, -200, 150) def right_key(): sprite.move_right(30) # add other actions... stage.event_key("right", right_key) def left_key(): sprite.move_left(30) # add other actions... stage.event_key("left", left_key) def interval(): x = random.randint(-250, 250) # sprite = codesters.Circle(x, y, diameter, "color") food = codesters.Circle(x, 260, 10, "sienna") rand_drop = random.randint(-15, -5) # add any other actions... stage.event_interval(interval, 1) def collision(sprite, hit_sprite): global score stage.remove_sprite(hit_sprite) score += 1 score_board.update(score) # add any other actions... sprite.event_collision(collision)
  • Run Code
  • Submit Work
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)