Categories
BLOG

asteroids in scratch

User:RokCoder/Asteroids Tutorial

Contents

  • 1 Asteroids
    • 1.1 Stage 1 – Adding the Player
    • 1.2 Stage 2 – Adding an asteroid
    • 1.3 Stage 3 – Destroying an asteroid
    • 1.4 Stage 4 – Adding the game loop
    • 1.5 Stage 5 – Making the game more challenging
    • 1.6 Stage 6 – Adding the sound effects
    • 1.7 Stage 7 – Scope for improvement

Asteroids

In this tutorial you’re going to develop a full arcade game. The player controls a spaceship that can spin around and shoot bullets. He must destroy asteroids that are flying through space before they crash into him.

The accompanying project can be found here and contains all of the graphics and sound effects required to complete the tutorial.

Stage 1 – Adding the Player

First, you want to show the correct backdrop to the game. The Stage has three costumes. You want to display the Space costume when playing the game so select the Stage and add the following script

The player’s spaceship is placed at the centre of the screen and can be rotated left or right when the player presses the left or right arrow keys. Select the Player sprite and add the following script

If you press the green flag you’ll now have a nice background and a spaceship that you can rotate left and right.

Although it’s not a problem at the moment, you need to make sure that all the other sprites in the game are hidden at this point. Add the following script to the Bullet, Asteroid and Explosion sprites

Next, you want to be able to fire bullets by pressing the space bar. Every time you fire a bullet, you’re going to create a clone of the Bullet sprite. Each bullet is going to appear where the spaceship is positioned and it’s going to fire in the direction that that spaceship is facing. When the bullet reaches the edge of the screen you’re going to remove it from the game. Select the Bullet sprite and add the following code

Now you need to make it so that the bullets are created when the player presses the space bar. Select the Player sprite and, underneath the script that lets the player rotate left and right, add the following

If you press the green flag now, you can rotate the spaceship and you can also fire lots of bullets!

There are two things wrong though. Firstly, the bullets appear on top of the spaceship when they’re fired and, secondly, if you hold the space bar down you fire a huge stream of bullets. You need to fix both of those issues

You can make the bullets appear behind the player’s spaceship by moving them back a number of layers. Simply add the following to the top of the script in the Bullet sprite

Changing the script so that you only fire one bullet when the space bar is pressed is a little more complicated. To achieve this you’re going to create a local (for this sprite only) variable called pressing space? in Player. This is set to one when the space bar is pressed and is set to zero when it isn’t. Change your script in the Player sprite that fires bullets to the following

If you press the green flag now, you can turn your ship and fire bullets properly. The only thing you’re missing is something to fire the bullets at. It’s time to add an asteroid to the game!

Stage 2 – Adding an asteroid

There are three costumes in the Asteroid sprite. A large asteroid, a medium asteroid and a small asteroid. You want to start with the large asteroid so add the following to the script that you already have in the Asteroid sprite

Because you’ll eventually want more than one asteroid at a time, you’re going to create them as clones. To start with, you just want one asteroid that starts at a random place on the screen and drifts in a random direction. You don’t want the asteroid too near to the player or moving towards the player when it is created. Add the follow script to Asteroid to achieve this

When the asteroid moves off one side of the screen you want it to wrap around to the other side. To do this you need to add the following script underneath the one you just entered

All that’s left to do is to actually create the clone. You can do that from the Player sprite script. Add the block to create the clone into the script that’s already in that sprite so that the top of it looks like this

Now, if you press the green flag, you’ll see an asteroid drifting across the screen!

Stage 3 – Destroying an asteroid

Wouldn’t it be nice if the bullets could actually destroy the asteroid? That’s pretty easy to do by checking if sprites are touching each other. Add the following to the Asteroid sprite at the beginning of the forever loop

And add the following to the Bullet sprite at the beginning of the forever loop

That looks like it should do just what is needed but if you run the game now you’ll see a problem when you shoot the asteroid. Either the bullet will disappear and the asteroid will continue or the asteroid will disappear and the bullet will continue. The reason for this is because of the order in which the scripts are executed. If the asteroid finds that it’s touching the bullet, it will instantly remove itself from the game. But then, when the bullet checks to see if it’s touching an asteroid, the asteroid is no longer there! The easiest way to work around this is to add a very short pause before removing the sprite from the game.

In both of the short scripts you just added, add the following block before deleting the clones

It should now work as you would want it to. You can destroy the asteroid and the bullet that does this will also disappear. It’s a little bit of an anticlimax though! Wouldn’t it be better if the asteroid actually exploded?

Notice that the Explosion sprite has eight costumes. If you play those costumes in order, you’ll get a lovely explosion animation. Because you might have more than one explosion at a time, you’re going to create an explosion clone whenever you destroy something.

First, you need to ensure that the animation always starts on the first costume. Add the following block to the script that you’ve already put in the Explosion sprite

And when you create an explosion clone, you want it to run through all of the costumes in order before removing the clone from the game. You need to know where to display the explosion on the screen so you need to add two global (for all sprites) variables, Explosion X and Explosion Y. When an asteroid is shot it will store its position in those variables and then create the explosion clone. Add the following script to Explosion which will create the explosion using those variables for positioning, play through the costumes and then remove itself from the game

And to actually create the explosion, add the following script just before the asteroid clone deletes itself in the Asteroid script

Press the green flag now and you can shoot the asteroid and watch it explode!

It’s not a very challenging game yet, though, as the asteroid can pass straight through the spaceship without doing any harm to it. You need the asteroid to destroy the player’s ship if it collides. Add the following to the Player sprite just before the end of the forever loop

Now, the game is a little more challenging! You need to destroy the asteroid before it can destroy you.

Stage 4 – Adding the game loop

It’s a nice game now but every time you get killed, the game just ends. It would be much nicer if you could start again without having to rerun it each time. Notice that we have several costumes in Stage

Change the script that you have in Stage to the following

It now displays the “press space bar” screen until the space key has been pressed. It then broadcasts a message to play the game and waits until the game has finished after which it displays the game over message for two seconds and then loops through the whole process again.

You need to change the script in the Player sprite so that the game starts when the Play Game message is received and not when the green flag is pressed. In the Player sprite replace

Add the following script so that the player’s spaceship isn’t displayed until the game starts

The last detail is to make sure there are no asteroid clones when a new game starts. Go to the Asteroid sprite and add the following script

If you play the game now you have a start screen, a game and an end sequence if the asteroid collides with the player. You might notice that every time you press space to start a game, you also fire a bullet from the spaceship. You can fix this quite easily by adding a block to the script in the Player sprite so that it starts like this

Stage 5 – Making the game more challenging

Remember that you have three costumes in the Asteroid sprite? It would be better if when you destroy the large asteroid, it turns into a medium asteroid; when you destroy a medium asteroid it, it turns into a small asteroid; and when you destroy a small asteroid it is gone for good. Smaller asteroids should move faster, too.

In the Asteroid sprite, create a local (for this sprite only) variable called speed and then modify the script that already exists in that sprite so that it looks like this

Now change the following block

And now for the part that changes a large asteroid to a medium one, a medium asteroid to a small one and destroys a small one completely. Underneath the block that creates the explosion, replace the delete this clone block with the following

Notice that you’re also changing the direction that the asteroid is moving in whenever we hit it with a bullet. Play the game now and see what happens when you shoot the asteroid.

To make things a little more difficult for the player, make it so that each asteroid breaks into two smaller asteroids when shot. Modify the script that you just added so that it looks like this

It now creates another clone as well as modifying the current one when shot.

Play the game and shoot an asteroid. It’s almost right but you’ll notice that when you shoot an asteroid, one of the smaller asteroids appears at a random position on the screen! If you look at the start of the clone script in the Asteroid sprite you can see why this is happening. Change the start of the script so that it only picks a random position for the large asteroid

And to make it into a proper game, you need a new wave of asteroids whenever you’ve cleared all the ones from the screen. Add a counter variable to the game that is increased by one every time an asteroid clone is created, and is decreased by one every time one is destroyed, so you’ll know when all the asteroids have been shot. Create a global (for all sprites) variable called Active Asteroids.

Set it to zero at the beginning of each game. It will be increased when you clone a new asteroid. Loop until it is back to zero which means the asteroids have been destroyed. To do this, modify the main script in the Player sprite so that it looks like this

Add the following block at the beginning of the clone script in the Asteroid sprite

Add the following block just before the clone is deleted

If you play the game now, a new asteroid will be created every time you’ve destroyed the last one. That’s a lot more fun but it would be even better if the game got harder on each level. What you’re going to do next is to create more asteroids each time they’ve all been destroyed. Add a new local (for this sprite only) variable in the Player sprite called asteroid count. Modify the main script so that it starts like this

And the end of the script so that it looks like this

And that’s it! The game is now complete. almost. Isn’t it a little bit quiet though?

Stage 6 – Adding the sound effects

In the Bullet sprite add the following block at the start of the clone script

Add the following to the Asteroid sprite at the top of the script when the asteroid is touching a bullet

Add the following to the Player sprite just before it stops the script from running when the player has touched an asteroid

And for the final touch we want a pulsing beat in the background that gets gradually faster as each level continues

Create a global (for all sprites) variable called Sound Accelerating? and another called Sound Delay

Add the following scripts to Stage

Modify the main script in Stage to look like this

And, finally, in the Player sprite, add the following block at the top of the forever loop so that the speed of the background pulsing beat is reset at the beginning of each new wave of asteroids

And you should now have a complete Asteroids game!

Stage 7 – Scope for improvement

There’s plenty of scope for improvements. Here are a few suggestions –

  • Add a score
  • Add a hi-score
  • Add multiple lives
  • Add a bonus life when you reach a certain score
  • Add a thrust control so that the player can move

User:RokCoder/Asteroids Tutorial Contents 1 Asteroids 1.1 Stage 1 – Adding the Player 1.2 Stage 2 – Adding an asteroid 1.3 Stage 3 – Destroying an asteroid 1.4 Stage 4

Asteroids from Scratch

Recapturing my misspent youth, a quarter at a time

Become a Hackaday.io member

Not a member? You should Sign Up.

Already have an account? Log In.

Just one more thing

To make the experience fit your profile, pick a username and tell us what interests you.

We found and based on your interests.

  • Description
  • Details
  • Files 1
    • View all
  • Components 0
  • Logs 0
  • Instructions 0
  • Discussion 3

Team ( 1 )

Related lists

Video Games

Both retro and novelty. Games, consoles, controllers, gadgets.

This project was created on 11/08/2019 and last updated a year ago.

Description

A confession: I was an Asteroids addict. Back in high school, anytime I could free up a couple of bucks, I’d head down to the bowling alley in town, sidle into the arcade, and feed my addiction. I was never very good – my all-time high score was only 235,000 – but there was something about the physics of flying that spaceship and watching the rubble scatter in a fair approximation of the law of conservation of momentum.

Now that I’m older but no wiser, I feel like it’s time to have my own Asteroids machine. Sure, I could probably buy one, but that would be a wanker move – what’s more typical than an old guy buying toys, amiright? If I’m gonna do this, I’m gonna do it my way – I’ll build the thing myself. And not an emulation, mind you – I want to build the original machine, using the original parts. Yep – a 6502A, the X-Y monochrome display, the original hardware sound system. Nothing imitated, nothing synthesized or sampled. All original.

Details

Years ago, when I actually worked in an arcade, I got to know the guy who fixed the machines. He was a nice guy and suffered my teenage geek pestering well. I eventually got up the nerve to ask him for the schematics for my favorite game, Asteroids. I thought for sure he’d flip out on me, telling me it was top secret corporate information and that Atari would throw him in jail if he released it. Instead, he reached into an Asteroids cabinet, grabbed the documents pouch, and said, “Sure, here you go.”

I pored over those prints at home that night. Most of it I didn’t understand at the time, and I knew there was no way I was going to be able to buy the parts needed to build my own machine from scratch. But one area of the schematic interested me – the audio section. In it I saw parts that I recognized, stuff that I had used in my lame little experiments or had seen in my Forrest Mims books. It turns out the sounds are completely generated in hardware – no samples, no synthesizers, just cheapo 555s and the like. That I figured I could build, so I filed away the schematics to await the day that I could afford to buy the parts.

Forty years later, those schematics are long gone. But they live on in the Internet, and I found them again the other day. I’m starting to capture the schematics for the audio section in KiCad, mainly so I can design PCBs for this when it gets a little further along. I’ll breadboard it all first, of course, but I figure getting the schematic off paper is probably a good first step.

I’ve also started tracking down some of the obsolete and harder-to-find parts, like the TDA2002 amplifiers for the finals and the LM566 VCO chip. Those were both eBay purchases and are on the slow boat from China. The rest of the BOM – 555s, 74LSxx, 40xx, etc. – should be pretty easily sourced from Digi-Key or Mouser. I’ve only made one substitutions so far – the original calls for a 9316 synchronous 4-bit counter, but the 74LS161 is supposed to be a pin-for-pin replacement, so I went with that rather than try to hunt down something that might not exist. Plus KiCad already had a symbol for the modern device.

I’m shooting for all through-hole here, just for the old school look. I probably will include some kind of microcontroller, though, since the original sound hardware was controlled by IO from the 6502A microprocessor. Things like the rate of the thumping or the different tones of the explosions are controlled by setting four or five bits, and rather than try to do that with dip switches or some kind of encoders, I can have an Arduino twiddle those bits programatically to demo the sounds. Might be fun to have it “play” a random game with just the audio.

As for the audio amp, I could just use something off the shelf, but I’m trying for authenticity here. I want this to sound like I remember, so I’ll make sure I use the original audio components. The power supply gives me pause, though – I might have to make a concession here and go with SMPS modules to get the 12V, 10.3V, and 5V rails that I need. Not really worried about 5 or 12, but that 10.3 supply is unregulated and powers the amp. It comes off a center-tapped secondary on a big multi-tapped power transformer, and just has a half-wave rectifier and a big electrolytic filter. The ripple that’s coming across might be part of the charm of the audio stage, so I might have to build that power supply stage too.

Files

Asteroids-sp.pdf

Adobe Portable Document Format – 2.64 MB – 11/09/2019 at 18:35

Recapturing my misspent youth, a quarter at a time