Bubble Chaos: a complete flash game in only 131 lines

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:

ACTIONSCRIPT:
  1. game_playing = 0;
  2. shot_sound = new Sound();
  3. shot_sound.attachSound("shoot_sound");
  4. pop_sound = new Sound();
  5. pop_sound.attachSound("ppop_sound");
  6. title_screen();
  7. function title_screen() {
  8.     attachMovie("main_menu", "main_menu", 1);
  9.     if (_root.plname) {
  10.         main_menu.player_name.text = _root.plname;
  11.     }
  12.     main_menu.play_button.onPress = function() {
  13.         _root.plname = main_menu.player_name.text;
  14.         removeMovieClip(main_menu);
  15.         game_init();
  16.     };
  17. }
  18. function game_init() {
  19.     game_playing = 1;
  20.     level = 1;
  21.     game_over = 0;
  22.     firing = 0;
  23.     score = 0;
  24.     Mouse.hide();
  25.     _root.createEmptyMovieClip("game_scene", 1);
  26.     game_scene.attachMovie("back", "back", 2);
  27.     game_scene.attachMovie("hero", "hero", 3, {_y:400});
  28.     game_scene.hero.onEnterFrame = function() {
  29.         (_xmouse<40) ? this._x=40 : (_xmouse>460) ? this._x=460 : this._x=_xmouse;
  30.         if (slow_enemy>1) {
  31.             slow_enemy -= 0.1;
  32.         }
  33.     };
  34.     game_scene.attachMovie("crosshair", "crosshair", 4);
  35.     game_scene.crosshair.onEnterFrame = function() {
  36.         this._x = game_scene.hero._x;
  37.         this._y = _ymouse;
  38.     };
  39.     game_scene.attachMovie("points", "points", 10003);
  40.     game_scene.points.my_score.text = 0;
  41.     enterlevel(1);
  42. }
  43. function enterlevel() {
  44.     combo = 0;
  45.     kills = 0;
  46.     slow_enemy = 1;
  47.     for (x=1; x<=_root.level+2; x++) {
  48.         enemy = game_scene.attachMovie("alien", "alien_"+game_scene.getNextHighestDepth(), game_scene.getNextHighestDepth(), {_x:50+Math.random()*400, _y:50+Math.random()*200});
  49.         enemy.enemynum.text = x;
  50.         enemy.points = x;
  51.         dir = Math.random()*360;
  52.         enemy.speedx = Math.cos(dir*Math.PI/180)*x/2;
  53.         enemy.speedy = Math.sin(dir*Math.PI/180)*x/2;
  54.         enemy.onEnterFrame = function() {
  55.             this._x += (this.speedx/slow_enemy);
  56.             this._y += (this.speedy/slow_enemy);
  57.             if (((this._x-25<0) and (this.speedx<0)) or ((this._x+25>500) and (this.speedx>0))) {
  58.                 this.speedx *= -1;
  59.             }
  60.             if (((this._y-25<0) and (this.speedy<0)) or ((this._y+25>400) and (this.speedy>0))) {
  61.                 this.speedy *= -1;
  62.             }
  63.             dist_x = this._x-_root.game_scene.hero._x;
  64.             dist_y = this._y-_root.game_scene.hero._y;
  65.             abs_dist = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
  66.             if (abs_dist<65) {
  67.                 pop_sound.start();
  68.                 removeMovieClip(this);
  69.                 removeMovieClip(game_scene.crosshair);
  70.                 removeMovieClip(game_scene.hero);
  71.                 Mouse.show();
  72.                 game_over = 1;
  73.                 game_scene.attachMovie("play_again", "play_again", game_scene.getNextHighestDepth());
  74.                 game_scene.play_again._x = 250;
  75.                 game_scene.play_again._y = 300;
  76.                 game_scene.play_again.onRelease = function() {
  77.                     removeMovieClip(game_scene);
  78.                     title_screen();
  79.                 };
  80.             }
  81.             bullet_dist_x = this._x-game_scene.fire._x;
  82.             bullet_dist_y = this._y-game_scene.fire._y;
  83.             bullet_abs_dist = Math.sqrt(bullet_dist_x*bullet_dist_x+bullet_dist_y*bullet_dist_y);
  84.             if (bullet_abs_dist<28) {
  85.                 if (this.points == combo) {
  86.                     combo_bonus += this.points*100;
  87.                 } else {
  88.                     combo_bonus = 0;
  89.                 }
  90.                 combo = this.points+1;
  91.                 height_bonus = (this.points*100)*(Math.ceil((400-this._y)/40)-1);
  92.                 score += ((this.points*100)+height_bonus+combo_bonus);
  93.                 game_scene.points.my_score.text = score;
  94.                 earned = game_scene.attachMovie("disp_score", "disp_score_"+game_scene.getNextHighestDepth(), game_scene.getNextHighestDepth(), {_x:this._x, _y:this._y});
  95.                 earned.disp.text = "Bubble score: "+(this.points*100)+"\nHeight bonus: "+height_bonus+"\nCombo bonus: "+combo_bonus;
  96.                 earned.rem = 0;
  97.                 earned.onEnterFrame = function() {
  98.                     this._y -= 0.5;
  99.                     this.rem++;
  100.                     if (this.rem>100) {
  101.                         removeMovieClip(this);
  102.                     }
  103.                 };
  104.                 pop_sound.start();
  105.                 removeMovieClip(this);
  106.                 removeMovieClip(_root.game_scene.fire);
  107.                 firing = 0;
  108.                 kills++;
  109.                 slow_enemy = 3;
  110.                 if (kills == _root.level+2) {
  111.                     _root.level++;
  112.                     enterlevel(_root.level);
  113.                 }
  114.             }
  115.         };
  116.     }
  117. }
  118. function onMouseDown() {
  119.     if ((!firing) and (!game_over) and (game_playing)) {
  120.         firing = 1;
  121.         shot_sound.start();
  122.         game_scene.attachMovie("fire", "fire", 1000, {_x:_xmouse, _y:357});
  123.         game_scene.fire.onEnterFrame = function() {
  124.             this._y -= 5;
  125.             if (this._y<0) {
  126.                 removeMovieClip(this);
  127.                 firing = 0;
  128.             }
  129.         };
  130.     }
  131. }

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.

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

4 Responses to “Bubble Chaos: a complete flash game in only 131 lines”

  1. mousey on May 12th, 2007 4:13 pm

    wow really good clear explanation 10/10 as always!

  2. And Mar on May 12th, 2007 8:29 pm

    Cool man!

  3. Callum on May 19th, 2007 9:49 pm

    owow. another amazing tut by emanuele. Well done. Im going to edit this abit and post the result xD Thanks.

    ~Callum

  4. styxtwo on October 24th, 2007 8:11 pm

    wow really nice game, you should make the balls react when they hit each other. that would be really cool.

Leave a Reply