STEP 18: Almost done! We just need to increase our score and show it whenever we hit a star.

  • From add Add Value indented inside the collision event. Change my_var to score.
  • Add Update Display to the event. Change my_display to score_display and my_var to score.
  • In GRAPHICS from add Remove Sprite to the event. Change sprite to hit_sprite.

Now, whenever hedgehog hits a star, the star disappears and you earn a point!

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) sprite.set_rotation(rotation+3) # add other actions... stage.event_key("left", left_key) def right_key(): rotation = sprite.get_rotation() print(rotation) sprite.set_rotation(rotation-3) # add other actions... stage.event_key("right", right_key) def interval(): global time time -=1 time_display.update(time) if time % 2 == 0: x = random.randint(-230,230) y = random.randint(0,230) star = codesters.Star(x, y, 5, 20, "yellow") if time == 0: stage.remove_all_events() stage.event_interval(interval, 1) def release_key(sprite): sprite.say("BLAST OFF!", 1, "white") sprite.move_forward(575) stage.wait(0.1) sprite.go_to(0, -210) # add other actions... sprite.event_key_release("space", release_key) def collision(sprite, hit_sprite): global score # add any other actions... sprite.event_collision(collision)
  • Run Code
  • Submit Work
  • Next Activity
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)