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
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

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.

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!

.webp&w=3840&q=75)
.webp&w=3840&q=75)
.webp&w=3840&q=75)
.webp&w=3840&q=75)
.webp&w=3840&q=75)