Create a Flash game like Metro Siberia Underground – AS3 version

Read all posts about "" game

More than 4 years ago I played a really fun game called Metro Siberia Underground, basically an helicopter game, and made an AS2 prototype.

Now I ported it into AS3 because I think the prototype can be improved or ported into other languages, and because I always liked one-button games.

The code is not commented because the entire logic behind it has been already explained in the original post.

package {
	import flash.display.Sprite;
	import flash.filters.GlowFilter;
	import flash.events.Event;
	import flash.events.MouseEvent;
	public class Main extends Sprite {
		private var shipFilter:GlowFilter=new GlowFilter(0x00ff00,0.8,4,4,2,3,false,false);
		private var smokeFilter:GlowFilter=new GlowFilter(0xff0000,0.8,4,4,2,3,false,false);
		private var fuelFilter:GlowFilter=new GlowFilter(0x00ffff,0.8,4,4,2,3,false,false);
		private var rockFilter:GlowFilter=new GlowFilter(0xffffff,0.8,4,4,2,3,false,false);
		private var scoreFilter:GlowFilter=new GlowFilter(0xff00ff,0.8,2,4,2,3,false,false);
		private var gravity:Number=0.1;
		private var thrust:Number=0.25;
		private var yspeed:Number=0;
		private var xspeed:Number=5;
		private var distance:Number=0;
		private var smokeInterval:Number=10;
		private var framesPassed:Number=0;
		private var fuelFrequency:Number=10;
		private var gasoline:Number=500;
		private var rockFrequency:Number=50;
		private var engines:Boolean=false;
		private var ship:Ship=new Ship();
		private var score:Score=new Score();
		private var rockCanvas:Sprite=new Sprite();
		private var fuelCanvas:Sprite=new Sprite();
		private var smokeCanvas:Sprite=new Sprite();
		private var fuelVector:Vector.=new Vector.();
		private var rockVector:Vector.=new Vector.();
		private var smokeVector:Vector.=new Vector.();
		public function Main() {
			addChild(ship);
			ship.x=120;
			ship.y=240;
			ship.filters=new Array(shipFilter);
			addChild(score);
			addChild(rockCanvas);
			addChild(fuelCanvas);
			addChild(smokeCanvas);
			score.filters=new Array(scoreFilter);
			addEventListener(Event.ENTER_FRAME,update);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,engineOn);
			stage.addEventListener(MouseEvent.MOUSE_UP,engineOff);
		}
		private function engineOn(e:MouseEvent):void {
			engines=true;
			framesPassed=smokeInterval;
		}
		private function engineOff(e:MouseEvent):void {
			engines=false;
			smokeInterval=10;
		}
		private function update(e:Event):void {
			if (Math.random()*10000)&&(engines)) {
				yspeed-=thrust;
				smokeInterval-=0.25;
				gasoline-=1;
				framesPassed++;
				if (smokeInterval=0; i--) {
				fuelVector[i].x-=xspeed*1.2;
				var dist_x:Number=ship.x+28*Math.cos(angle)-fuelVector[i].x;
				var dist_y:Number=ship.y+28*Math.sin(angle)-fuelVector[i].y;
				var dist:Number = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
				if (dist<10) {
					gasoline+=100;
					fuelCanvas.removeChild(fuelVector[i]);
					fuelVector.splice(i,1);
				}
				else {
					if (fuelVector[i].x<-10) {
						fuelCanvas.removeChild(fuelVector[i]);
						fuelVector.splice(i,1);
					}
				}
			}
			for (i=rockVector.length-1; i>=0; i--) {
				rockVector[i].x-=xspeed;
				if (rockVector[i].x<-20) {
					rockCanvas.removeChild(rockVector[i]);
					rockVector.splice(i,1);
				}
			}
			for (i=smokeVector.length-1; i>=0; i--) {
				smokeVector[i].x-=xspeed;
				smokeVector[i].width+=0.2;
				smokeVector[i].height+=0.2;
				smokeVector[i].alpha-=0.04;
				if (smokeVector[i].alpha<0) {
					smokeCanvas.removeChild(smokeVector[i]);
					smokeVector.splice(i,1);
				}
			}
			if (ship.y>480 || ship.y<0 || rockCanvas.hitTestPoint(ship.x+28*Math.cos(angle), ship.y+28*Math.sin(angle), true) || rockCanvas.hitTestPoint(ship.x+8*Math.cos(angle+Math.PI/2), ship.y+8*Math.sin(angle+Math.PI/2), true) || rockCanvas.hitTestPoint(ship.x+8*Math.cos(angle-Math.PI/2), ship.y+8*Math.sin(angle-Math.PI/2), true)) {
				yspeed=0;
				ship.y=200;
				gasoline=500;
				distance=0;
				rockVector=new Vector.();
				removeChild(rockCanvas);
				rockCanvas=new Sprite();
				addChild(rockCanvas);
				fuelVector=new Vector.();
				removeChild(fuelCanvas);
				fuelCanvas=new Sprite();
				addChild(fuelCanvas);
				smokeVector=new Vector.();
				removeChild(smokeCanvas);
				smokeCanvas=new Sprite();
				addChild(smokeCanvas);
			}
		}
	}
}

and this is the result:

Press mouse button to fly. I am also thinking about a Box2D helicopter game, it would be interesting.

Download the source code

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers