Introduction to Programming

Activity 2: Rocketship Controls

Lesson 10: Space Battle Game

The Stage is set up — now it's time to bring the rocketship to life! In this activity, we will program the rocketship to move left and right based on arrow key input, and keep it within the bounds of the Stage.

Planning the Controls

In most space shooter games, the player's ship moves horizontally across the bottom of the screen. We will control the rocketship using the left and right arrow keys.

Rocketship Controls

KeyAction
Right ArrowMove rocketship to the right
Left ArrowMove rocketship to the left

Building the Movement Script

Select the Rocketship sprite and build the following script. You will find the blocks in the mentioned palettes:

  • When flag clicked — Events
  • Go to x: (0) y: (-150) — Motion (positions the ship at the bottom center)
  • Forever — Control
  • If (right arrow key pressed?) Then: Change x by 10 — Sensing, Control, Motion
  • If (left arrow key pressed?) Then: Change x by -10 — Sensing, Control, Motion
Block coding script for the rocketship showing when flag clicked, go to x 0 y -150, forever loop with if right arrow pressed change x by 10 and if left arrow pressed change x by -10

Keeping the Rocketship on Screen

Without boundaries, the rocketship can fly off the screen. Let's add conditions to keep it within the Stage.

  • Inside the Forever loop, add an 'If (x position > 220) Then: Set x to 220' block.
  • Add another 'If (x position < -220) Then: Set x to -220' block.
  • This will stop the rocketship from moving beyond the left and right edges of the Stage.
Additional blocks inside the forever loop checking if x position exceeds 220 or -220 and resetting it to stay within bounds

Testing the Controls

  • Click the green flag to start.
  • Use the left and right arrow keys to move the rocketship.
  • Make sure it stays within the Stage boundaries.
  • The rocketship should not be able to fly off the left or right edge of the screen.
Save your file before moving on to the next lesson where we will add shooting mechanics and the ghost enemy!

Subscribe to our newsletter.

Get updates to news and events.