PREVIEW: Here's an example of the asteroid dodge game you'll be creating today!

  • Click Run to play.
  • Press the space bar to keep the spaceship in the air. Make sure you don't hit any rocks!
  • When you're finished playing, click Submit and Next to move on.

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

stage.set_background("space") sprite = codesters.Sprite("spaceship") sprite.set_size(0.4) sprite.go_to(-200, 0) stage.set_gravity(10) stage.disable_all_walls() def space(): sprite.jump(5) # add other actions... stage.event_space_key(space) def interval(): # sprite = codesters.Rectangle(x, y, width, height, "color") top_height = random.randint(50, 300) top_rock = codesters.Sprite("asteroid", 300, 0) top_rock.set_height(top_height) top_rock.set_gravity_off() top_rock.set_top(250) bot_height = 350 - top_height bot_rock = codesters.Sprite("asteroid", 300, 0) bot_rock.set_height(bot_height) bot_rock.set_gravity_off() bot_rock.set_bottom(-250) top_rock.set_x_speed(-3) top_rock.set_y_speed(0) bot_rock.set_x_speed(-2) bot_rock.set_y_speed(0) # add any other actions... stage.event_interval(interval, 5) def collision(sprite, hit_sprite): sprite.go_to(0,0) text = codesters.Text("Game Over!", 0, 100, "yellow") sprite.event_collision(collision)
  • Run Code
  • Submit Work
  • Next Activity
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)