WARNING: This program has a bug, which means we need to fix it! This bug is a logic error.

RULE: Collision events take two parameters, sprite and hit_sprite. The sprite refers to the sprite connected to the event handler. The hit_sprite becomes whatever the main sprite hits.

  • Click Run and move the mouse to use the bat to hit a ball. Oops! The ball should disappear, not the bat!
  • Fix the code so.remove_sprite() removes hit_sprite (the ball) instead of sprite ( the bat) on line 9!

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

stage.set_background("baseballfield") stage.disable_floor() sprite = codesters.Sprite("baseballbat") sprite.set_size(.5) timer = 5 def collision(sprite, hit_sprite): sprite.turn_right(360) stage.remove_sprite(sprite) # add any other actions... sprite.event_collision(collision) ### Don't touch any code below this! ### ### The error occurs above ### def interval(): global timer timer -= 1 x = random.randint(-250, 250) ball = codesters.Sprite("baseball", x, 260) ball.set_y_speed(random.randint(-15, -10)) # end the program after 5 seconds if timer == 0: stage.remove_all_events() # add any other actions... stage.event_interval(interval, 1) def mouse_move(): x = stage.mouse_x() y = stage.mouse_y() sprite.set_position(x, y) # add other actions... stage.event_mouse_move(mouse_move)
  • Run Code
  • Submit Work
  • Next Activity
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)