Introduction to Programming

Activity 2: Ghost Controls

Lesson 11: Space Battle Game (Part 2)

This is the final activity of the Space Battle Game! We will now program the ghost enemy to move around the Stage unpredictably and detect when it is hit by a bullet or when it reaches the rocketship. Let the battle begin!

Planning the Ghost Behavior

Ghost Behavior

EventAction
Game startsGhost appears at a random position and moves around
Hit by bulletGhost hides, Score increases by 1, Ghost reappears at a new random position
Reaches rocketshipLives decreases by 1, Ghost reappears at a new random position
Lives reach 0Game over — display a message and stop all scripts

Programming the Ghost Movement

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

  • When flag clicked: Go to a random position, Show, point in random direction — Events, Motion, Looks
  • Forever: Move 3 steps, If on edge bounce — Control, Motion
  • Every few seconds: Turn a random number of degrees to change direction unpredictably
Block coding script for the ghost showing when flag clicked, go to random position, forever loop with move 3 steps and if on edge bounce, and periodic random turns

Detecting Bullet Hit

  • Inside the Forever loop, add an 'If (touching Bullet?) Then' block.
  • Inside it: Hide the ghost, wait 1 second, go to a random position, Show.

Detecting Rocketship Collision

  • Inside the Forever loop, add an 'If (touching Rocketship?) Then' block.
  • Inside it: Change Lives by -1, go to a random position.
  • Add an 'If (Lives = 0) Then' block: Say (Game Over!) for 3 seconds, Stop all.
Block coding scripts showing ghost detecting bullet hit and rocketship collision, handling score, lives, game over condition

Testing the Complete Game

  • Click the green flag to start the Space Battle Game.
  • Move the rocketship with the left and right arrow keys.
  • Press the spacebar to shoot bullets at the ghost.
  • Avoid letting the ghost reach your rocketship — you only have 3 lives!
  • Try to score as many points as possible before losing all your lives.
PictoBlox stage showing the Space Battle Game in action with the rocketship at the bottom, a bullet flying upward, the ghost moving around, and Score and Lives variables displayed
Congratulations! You have built a complete Space Battle Game from scratch using everything you have learned — variables, conditionals, loops, sensing, and more. Save your file as Space Battle Game — Final!

Subscribe to our newsletter.

Get updates to news and events.