A “very old” Flash Sokoban prototype

This is a Sokoban prototype I made in 2005 after reading some tutorials on gotoandplay.it.

It does not look that awesome and I am afraid the code sucks a bit, but I think it could be a good start for a project.

I plan to work on a Flash version of Sokoban someday, meanwhile you could have a look to my Javascript version.

Just in case you don't know what is Sokoban...

From Wikipedia: Sokoban (warehouse keeper) is a transport puzzle in which the player pushes boxes around a maze, viewed from above, and tries to put them in designated locations. Only one box may be pushed at a time, not two, and boxes cannot be pulled. As the puzzle would be extremely difficult to create physically, it is usually implemented as a video game.

I won't comment the code because it's an old work, I will do it when I'll create a new version.

Anyway it's not that hard to understand, specially if you are an old time reader

ACTIONSCRIPT:
  1. map = new Array();
  2. diamond_map = new Array();
  3. create_map();
  4. draw_map();
  5. _root.attachMovie("item", "sprite", 10000);
  6. sprite._x = 40;
  7. sprite._y = 20;
  8. sprite.px = 2;
  9. sprite.py = 1;
  10. sprite.speed = 4;
  11. sprite.steps = (20/sprite.speed);
  12. sprite.moving = false;
  13. sprite.count = 0;
  14. function create_map() {
  15.     map[0] = new Array(1, 1, 1, 1, 1, 1, 1, 1);
  16.     map[1] = new Array(1, 0, 0, 0, 0, 0, 0, 1);
  17.     map[2] = new Array(1, 0, 0, 0, 1, 0, 0, 1);
  18.     map[3] = new Array(1, 0, 2, 0, 0, 0, 0, 1);
  19.     map[4] = new Array(1, 0, 0, 0, 1, 2, 0, 1);
  20.     map[5] = new Array(1, 0, 0, 2, 0, 0, 0, 1);
  21.     map[6] = new Array(1, 0, 0, 0, 0, 0, 0, 1);
  22.     map[7] = new Array(1, 1, 1, 1, 1, 1, 1, 1);
  23. }
  24. function draw_map() {
  25.     pos = 0;
  26.     n_diamond = 0;
  27.     for (var y = 0; y<20; y++) {
  28.         for (var x = 0; x<20; x++) {
  29.             pos = x+(y*20);
  30.             map_value = map[y][x];
  31.             switch (map_value) {
  32.             case 0 :
  33.                 _root.attachMovie("ground", "ground_"+pos, pos);
  34.                 _root["ground_"+pos]._x = x*20;
  35.                 _root["ground_"+pos]._y = y*20;
  36.                 break;
  37.             case 1 :
  38.                 _root.attachMovie("wall", "tile_"+pos, pos);
  39.                 _root["tile_"+pos]._x = x*20;
  40.                 _root["tile_"+pos]._y = y*20;
  41.                 break;
  42.             case 2 :
  43.                 _root.attachMovie("ground", "ground_"+pos, pos);
  44.                 _root["ground_"+pos]._x = x*20;
  45.                 _root["ground_"+pos]._y = y*20;
  46.                 n_diamond++;
  47.                 _root.attachMovie("diamond", "diamond_"+n_diamond, 10000+n_diamond);
  48.                 _root["diamond_"+n_diamond]._x = x*20;
  49.                 _root["diamond_"+n_diamond]._y = y*20;
  50.                 diamond_map[y*20+x] = n_diamond;
  51.                 // relative position of diamonds
  52.                 break;
  53.             }
  54.         }
  55.     }
  56. }
  57. sprite.onEnterFrame = function() {
  58.     if (!this.moving) {
  59.         if (Key.isDown(Key.LEFT)) {
  60.             try_to_go = "left";
  61.         }
  62.         if (Key.isDown(Key.RIGHT)) {
  63.             try_to_go = "right";
  64.         }
  65.         if (Key.isDown(Key.UP)) {
  66.             try_to_go = "up";
  67.         }
  68.         if (Key.isDown(Key.DOWN)) {
  69.             try_to_go = "down";
  70.         }
  71.         switch (try_to_go) {
  72.         case "left" :
  73.             destination = map[this.py][this.px-1];
  74.             if (destination == 2) {
  75.                 // if there is a diamond...
  76.                 destination = map[this.py][this.px-2];
  77.                 if (destination == 0) {
  78.                     this.pushing_diamond = true;
  79.                     this.diamond_to_push = diamond_map[this.py*20+this.px-1];
  80.                 }
  81.             }
  82.             if (destination == 0) {
  83.                 if (this.pushing_diamond) {
  84.                     map[this.py][this.px-1] = 0;
  85.                     map[this.py][this.px-2] = 2;
  86.                     diamond_map[this.py*20+this.px-2] = diamond_map[this.py*20+this.px-1];
  87.                     diamond_map[this.py*20+this.px-1] = 0;
  88.                 }
  89.                 this.px--;
  90.                 dir = "left";
  91.                 this.moving = true;
  92.             }
  93.             break;
  94.         case "right" :
  95.             destination = map[this.py][this.px+1];
  96.             if (destination == 2) {
  97.                 // if there is a diamond...
  98.                 destination = map[this.py][this.px+2];
  99.                 if (destination == 0) {
  100.                     this.pushing_diamond = true;
  101.                     this.diamond_to_push = diamond_map[this.py*20+this.px+1];
  102.                 }
  103.             }
  104.             if (destination == 0) {
  105.                 if (this.pushing_diamond) {
  106.                     map[this.py][this.px+1] = 0;
  107.                     map[this.py][this.px+2] = 2;
  108.                     diamond_map[this.py*20+this.px+2] = diamond_map[this.py*20+this.px+1];
  109.                     diamond_map[this.py*20+this.px+1] = 0;
  110.                 }
  111.                 this.px++;
  112.                 dir = "right";
  113.                 this.moving = true;
  114.             }
  115.             break;
  116.         case "up" :
  117.             destination = map[this.py-1][this.px];
  118.             if (destination == 2) {
  119.                 // if there is a diamond...
  120.                 destination = map[this.py-2][this.px];
  121.                 if (destination == 0) {
  122.                     this.pushing_diamond = true;
  123.                     this.diamond_to_push = diamond_map[(this.py-1)*20+this.px];
  124.                 }
  125.             }
  126.             if (destination == 0) {
  127.                 if (this.pushing_diamond) {
  128.                     map[this.py-1][this.px] = 0;
  129.                     map[this.py-2][this.px] = 2;
  130.                     diamond_map[(this.py-2)*20+this.px] = diamond_map[(this.py-1)*20+this.px];
  131.                     diamond_map[(this.py-1)*20+this.px] = 0;
  132.                 }
  133.                 this.py--;
  134.                 dir = "up";
  135.                 this.moving = true;
  136.             }
  137.             break;
  138.         case "down" :
  139.             destination = map[this.py+1][this.px];
  140.             if (destination == 2) {
  141.                 // if there is a diamond...
  142.                 destination = map[this.py+2][this.px];
  143.                 if (destination == 0) {
  144.                     this.pushing_diamond = true;
  145.                     this.diamond_to_push = diamond_map[(this.py+1)*20+this.px];
  146.                 }
  147.             }
  148.             if (destination == 0) {
  149.                 if (this.pushing_diamond) {
  150.                     map[this.py+1][this.px] = 0;
  151.                     map[this.py+2][this.px] = 2;
  152.                     diamond_map[(this.py+2)*20+this.px] = diamond_map[(this.py+1)*20+this.px];
  153.                     diamond_map[(this.py+1)*20+this.px] = 0;
  154.                 }
  155.                 this.py++;
  156.                 dir = "down";
  157.                 this.moving = true;
  158.             }
  159.             break;
  160.         }
  161.         try_to_go = "";
  162.         // dont want sprite to keep on moving
  163.     }
  164.     if (this.moving) {
  165.         this.moving = true;
  166.         if (this.count>=this.steps) {
  167.             this.moving = false;
  168.             if (this.pushing_diamond) {
  169.                 this.pushing_diamond = false;
  170.             }
  171.             this.count = 0;
  172.         } else {
  173.             switch (dir) {
  174.             case "left" :
  175.                 this._x -= this.speed;
  176.                 if (this.pushing_diamond) {
  177.                     _root["diamond_"+this.diamond_to_push]._x -= this.speed;
  178.                 }
  179.                 break;
  180.             case "right" :
  181.                 this._x += this.speed;
  182.                 if (this.pushing_diamond) {
  183.                     _root["diamond_"+this.diamond_to_push]._x += this.speed;
  184.                 }
  185.                 break;
  186.             case "up" :
  187.                 this._y -= this.speed;
  188.                 if (this.pushing_diamond) {
  189.                     _root["diamond_"+this.diamond_to_push]._y -= this.speed;
  190.                 }
  191.                 break;
  192.             case "down" :
  193.                 this._y += this.speed;
  194.                 if (this.pushing_diamond) {
  195.                     _root["diamond_"+this.diamond_to_push]._y += this.speed;
  196.                 }
  197.                 break;
  198.             }
  199.             this.count++;
  200.         }
  201.     }
  202. };

Play the game with arrow keys...

... Then download the source and suggest something

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.

6 Responses to “A “very old” Flash Sokoban prototype”

  1. Maciek on November 13th, 2007 7:42 pm

    lol nice game;]

  2. Maciek on November 13th, 2007 8:41 pm

    I really would like to see a soft body engine tho;]

  3. Eblup on November 14th, 2007 7:05 am

    can you make some AI or a A* pathfinding tut plz. it would really bring a lot of support back.

  4. shiv1411 on November 14th, 2007 12:00 pm

    hi!
    as usual gr8 and incomplete. I think you should publish all parts of a particular tut in one go. Anyways gr8 tut!

  5. shiv1411 on November 14th, 2007 1:26 pm

    hey Emanuele,
    Can I suggest you something?
    Would you make a game like cricket or football in Flash. I know its hard, but u r a genius!
    Try using cute graphics as well.

  6. Thomas on November 16th, 2007 5:18 am

    Woohoo!!!! I don’t know why, but I always end up on the end of my seat thinking “Oh man! How’s he gonna code this function!”

    And then it’s solved and I’m all happy!

Leave a Reply