Friday, November 02, 2007

how to make space invaders in etoys

Make a bullet object

Make a new variable, call it newBullet and change the type to Player
variable > newBullet
menu > change value type > Player

Script your own Create Event, which makes new bullets
drag newBullet by assignment handle to the script
miscellaneous > bullet's copy, replace dot

Make another script which moves the bullets
bullet forward 5

Go back to the Create Event script and send a message to the new bullets that are being created to move
scripting > bullet start script
then replace bullet with the newBullet Player variable

Now whenever I run my Create Event it creates a new bullet which is fired off. What happens is that the new bullet instances are named bullet1, bullet2, etc. and each bullet has its own Player, which runs its own move script. You can't see this happening unless you grab one of the new bullets and open its viewer.

With all the new bullets I need some way to tidy up. So I'll write some scripts to clean up the siblings, just so I can play around and be able to tidy up afterward

Write a bullet delete script

Write a remove all siblings script which calls the bullet delete script

I want keyboard control for firing off bullets

Middle click on the World and open the World Viewer. Then open the input category and you will see world's lastKeystroke. Press some keys on the keyboard and you will see the values change in the viewer.

Tear off a Yes/No Test unit and drop it into the bullet create script

Drag world's lastKeystroke (not by the assignment arrow) into the Test section of the test unit. Then replace the part after the = sign with <up> arrow. Move the other already existing (three) lines of the create script into the Yes section of the test unit.

We want to simulate a keypress, ie. not have the <up> arrow always in place - if that happened we would have bullets being created continuously. Add a
world's lastKeystroke = <down> assignment to the bottom of the Yes section, so that <up> arrow turns into <down> arrow immediately after it is pressed

Since we are now doing keypress control we will have run the ticker continually on the create script. Unless we do something about it then this will be passed onto the create scripts of all the new bullets. So it's best to stop the create script running unnecessarily on the new bullets. Add a stop script create line to the Yes section of the test unit

I now want to destroy the surplus bullets as they reach the edge of the screen

Tear off a Yes/No Test unit and drop it into the bullet moves script.
test > bullet obtrudes (boolean)
Obtrudes means whether the object sticks out over its containers edge
Add bullet obtrudes (don't drag by assignment arrow) to the Test condition. Move the forward command to the No section of the test unit

At this point we want to stop the move script ticking and delete the bullet
scripting > bullet stop script

My project available:

Karl Ramberg's old tutorial: was helpful

Summary: So far we have created multiple bullets, fired them and destroyed them when they reach the edge of the screen

New things learnt:
  • How to create a Player variable
  • How to program key strokes
  • How to write your own Create, Key press and Destroy events
  • How to remove siblings and keep the original object
  • How to stop and start scripts programmatically

Still to do:
  • Introduce the shooter and move it left and right with the arrow keys
  • Create the new bullets where ever the shooter happens to be
  • Introduce enemies and get them moving down the screen towards the shooter
  • Make the enemy change to more threatening appearance as it gets closer
  • Shoot the enemy, create explosions, display points scored
  • Add dramatic music
  • Have good images for shooter, enemies and background image


Bill Kerr said...

Comment from Dreyfuss Pierre-André at the squeakland list:

"Nice, You have found the rule that with Etoys we have to see What an object knows and can do for itself to put the scripts in the right objects. So the bullet knows when it reaches the barrier and can delete itself.

I found about the same solution trying to solve this project.

To let the gun move in the same direction after shooting, I have tested the heading value. If it is -90 I put @lt;left@gt; in the input buffer, if it is 90 I put @lt;right@gt;

With the test on the key pressed, I have added to set heading to 0 if the @lt;down@gt; is pressed, This will enable to put @lt;down@gt; again after shooting if heading is 0. This way the gun remains still if the down key was pressed before shooting and go no moving to right or left."

Anonymous said...

Hi Bill,

I like your blog very much, thanks for writing it.
You might be interested in Peter's and mine implementation of Space Invaders with Etoys you can find on:

It dates back to the time there was no keyboard support in etoys so you have to use the mouse and click on the arrows to move the ship and on the square to shoot.

You might also want to have a look at



Raven said...

Hi, Bill...

I'm finding your old blog posts helpful in finding my way around Etoys.

I found this a great start, especially in learning how to generate siblings automatically. But I can't find any continuation. Did you finish this project (and where might I find more details), or is this all the further you took this one?


Bill Kerr said...

hi raven,

Sorry I never did finish it