Bubble Chaos: a complete flash game in only 131 lines

Read all posts about "" game

Last minute edit: I open this quick note just to say I am pleased that Bubble Chaos, with its only 131 lines of coding, got a “Decent Score” in NewGrounds portal

NewGrounds

Time to introduce Bubble Chaos, a complete flash game in only 131 lines of actionscript… brackets included of course!

Bubble Chaos

Let’s examine the features of this game:

Splash screen
Option to enter the name, that will remain during all game session (for future highscores implementation)
Infinite levels of increasing difficulty
(pretty) Complex score system
Decent action

The aim of the game is simple: destroy all bubbles with your cannon. You can shot only one missile at time. You get more points if you destroy the bubbles in their numerical order and if you destroy them when they are close to the top of the screen.

As you can see, the game is not-so-simple despite of its only 131 lines of code

Here it is the actionscript, only in the first frame:

game_playing = 0;
shot_sound = new Sound();
shot_sound.attachSound("shoot_sound");
pop_sound = new Sound();
pop_sound.attachSound("ppop_sound");
title_screen();
function title_screen() {
	attachMovie("main_menu", "main_menu", 1);
	if (_root.plname) {
		main_menu.player_name.text = _root.plname;
	}
	main_menu.play_button.onPress = function() {
		_root.plname = main_menu.player_name.text;
		removeMovieClip(main_menu);
		game_init();
	};
}
function game_init() {
	game_playing = 1;
	level = 1;
	game_over = 0;
	firing = 0;
	score = 0;
	Mouse.hide();
	_root.createEmptyMovieClip("game_scene", 1);
	game_scene.attachMovie("back", "back", 2);
	game_scene.attachMovie("hero", "hero", 3, {_y:400});
	game_scene.hero.onEnterFrame = function() {
		(_xmouse<40) ? this._x=40 : (_xmouse>460) ? this._x=460 : this._x=_xmouse;
		if (slow_enemy>1) {
			slow_enemy -= 0.1;
		}
	};
	game_scene.attachMovie("crosshair", "crosshair", 4);
	game_scene.crosshair.onEnterFrame = function() {
		this._x = game_scene.hero._x;
		this._y = _ymouse;
	};
	game_scene.attachMovie("points", "points", 10003);
	game_scene.points.my_score.text = 0;
	enterlevel(1);
}
function enterlevel() {
	combo = 0;
	kills = 0;
	slow_enemy = 1;
	for (x=1; x<=_root.level+2; x++) {
		enemy = game_scene.attachMovie("alien", "alien_"+game_scene.getNextHighestDepth(), game_scene.getNextHighestDepth(), {_x:50+Math.random()*400, _y:50+Math.random()*200});
		enemy.enemynum.text = x;
		enemy.points = x;
		dir = Math.random()*360;
		enemy.speedx = Math.cos(dir*Math.PI/180)*x/2;
		enemy.speedy = Math.sin(dir*Math.PI/180)*x/2;
		enemy.onEnterFrame = function() {
			this._x += (this.speedx/slow_enemy);
			this._y += (this.speedy/slow_enemy);
			if (((this._x-25<0) and (this.speedx<0)) or ((this._x+25>500) and (this.speedx>0))) {
				this.speedx *= -1;
			}
			if (((this._y-25<0) and (this.speedy<0)) or ((this._y+25>400) and (this.speedy>0))) {
				this.speedy *= -1;
			}
			dist_x = this._x-_root.game_scene.hero._x;
			dist_y = this._y-_root.game_scene.hero._y;
			abs_dist = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
			if (abs_dist<65) {
				pop_sound.start();
				removeMovieClip(this);
				removeMovieClip(game_scene.crosshair);
				removeMovieClip(game_scene.hero);
				Mouse.show();
				game_over = 1;
				game_scene.attachMovie("play_again", "play_again", game_scene.getNextHighestDepth());
				game_scene.play_again._x = 250;
				game_scene.play_again._y = 300;
				game_scene.play_again.onRelease = function() {
					removeMovieClip(game_scene);
					title_screen();
				};
			}
			bullet_dist_x = this._x-game_scene.fire._x;
			bullet_dist_y = this._y-game_scene.fire._y;
			bullet_abs_dist = Math.sqrt(bullet_dist_x*bullet_dist_x+bullet_dist_y*bullet_dist_y);
			if (bullet_abs_dist<28) {
				if (this.points == combo) {
					combo_bonus += this.points*100;
				} else {
					combo_bonus = 0;
				}
				combo = this.points+1;
				height_bonus = (this.points*100)*(Math.ceil((400-this._y)/40)-1);
				score += ((this.points*100)+height_bonus+combo_bonus);
				game_scene.points.my_score.text = score;
				earned = game_scene.attachMovie("disp_score", "disp_score_"+game_scene.getNextHighestDepth(), game_scene.getNextHighestDepth(), {_x:this._x, _y:this._y});
				earned.disp.text = "Bubble score: "+(this.points*100)+"\nHeight bonus: "+height_bonus+"\nCombo bonus: "+combo_bonus;
				earned.rem = 0;
				earned.onEnterFrame = function() {
					this._y -= 0.5;
					this.rem++;
					if (this.rem>100) {
						removeMovieClip(this);
					}
				};
				pop_sound.start();
				removeMovieClip(this);
				removeMovieClip(_root.game_scene.fire);
				firing = 0;
				kills++;
				slow_enemy = 3;
				if (kills == _root.level+2) {
					_root.level++;
					enterlevel(_root.level);
				}
			}
		};
	}
}
function onMouseDown() {
	if ((!firing) and (!game_over) and (game_playing)) {
		firing = 1;
		shot_sound.start();
		game_scene.attachMovie("fire", "fire", 1000, {_x:_xmouse, _y:357});
		game_scene.fire.onEnterFrame = function() {
			this._y -= 5;
			if (this._y<0) {
				removeMovieClip(this);
				firing = 0;
			}
		};
	}
}

and here it is the linkage reference table for you to understand how did I call objects

Linkage reference

A detailed tutorial will follow in the next days, meanwhile I am providing 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