Asteroids clone example code

Show Index

This page demonstrates the code necessary to create an Asteroids clone. Some of it's features include:

-Images and sounds just like the original
-Asteroids break into smaller and smaller asteroids
-UFO's periodically appear to harass player
-Large UFO's fire in random directions but small UFO's fire directly at player
-Players ship has momentum and 'floats' across the screen
-Player can 'hyperspace' to a random location on the screen
-Player gains an additional life every 10,000 points
-All game objects 'explode' when destroyed

Jump ahead to see the final result...

Asteroids clone - Click to enlarge
Asteroids clone - Click to enlarge

Start with setting up graphics:

Graphics 800,600, 0, 1					;2D graphics at a resolution of 800x600
Text 400, 300, "Loading...", 1				;Write a temp message (could display an image here too)

Define Types:

;Define Types						;Use Types to store ship, asteroid, bullet, and particle information
Type ShipTYPE						;Set up the Ship type
	Field ID					;Type of ship (1=Player, 2=Slow UFO, 3=Fast UFO)
	Field X#					;X position
	Field Y#					;Y position
	Field rotation					;Current rotation of ship
	Field XVelocity#				;Ships X velocity
	Field YVelocity#				;Ships Y velocity
End Type
Type BulletTYPE						;Set up the Bullet type
	Field ID					;Type of bullet (1=player's, 2=UFO's)
	Field X#					;X position
	Field Y#					;Y position
	Field XVector#					;X, Y vectors of the bullets rotation, both are used to
	Field YVector#					; reduce the math required when updating the bullets position
	Field life					;Counter to keep track of bullets existence
End Type
Type AsteroidTYPE 					;Set up the Asteroid type
	Field ID					;Type of asteroid (1=Big, 2=Medium, 3=Small)
	Field X#					;X position
	Field Y#					;Y position
	Field rotation					;Current rotation of asteroid
	Field velocity#					;Asteroid velocity
End Type
Type ParticleTYPE					;Set up the Particle type
	Field X#					;X position
	Field Y#					;Y position
	Field XVector#					;X, Y vectors of the particles rotation, both are used to
	Field YVector#					; reduce the math required when updating the particles position
	Field life					;Counter to keep track of the particles existence
End Type

To do:

 

Unfinished...

And here is the entire code pieced together so that you can copy&paste it and immediately run it to see how it works.
Unfortunately, it doesn't all fit into one text box, so you'll have to add the code in the 2nd text box to the first.

Keys used in this example:

keyboard.jpg (7994 bytes)

	
Left key	Move left
Right key	Move Right
Up key		Thrust
Down key	Hyperspace
Spacebar	Fire
Esc key		Exits program
Files used in this example:

right click and select "Save Target As..."files used in this example

ship.png	fire.wav
shipFlame.png	bangLarge.ogg
bigA.png	bangMedium.ogg
medA.png	bangSmall.ogg
smallA.png	thrust.wav
slowUFO.png	slowUFO.wav
fastUFO.png	fastUFO.wav
		deepbeat.ogg
		extraShip.wav
If you've reached this page and there's no index on the left, Click here to show the Index Page