Please login or register.

Login with username, password and session length
Advanced search  

News:

Forum rules - please read before posting, it can save you a lot of time.

Author Topic: Match 3 game - source code - update 01-04-2011  (Read 8729 times)

0 Members and 1 Guest are viewing this topic.

siatek

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 1
    • View Profile
Match 3 game - source code - update 01-04-2011
« on: March 27, 2011, 01:37:26 PM »

Hi

this is my new version of match 3 game code ... I hope you like it ... now its more clear and i fixed some bugs ...
i will try to do a little sample project soon ... actually i used a google/ripped graphics ;)

regards ...



#include "scripts\base.inc"
#include "scripts\keys.inc"

actor.SkipTo(2048, 2048);
actor.Direction = DI_DOWN;
actor.Active = false;
Game.InventoryVisible = false;

var tile;
var board;
var picked;
var current;
var arrEnts;
var arrTiles;
var arrDestroyed;
var swap = false;
var sleeper = 200;

   Game.Interactive = false;
   Initialize();
   Randomize();
   Redraw();
   Falling();
   Game.Interactive = true;

on "LeftClick" {

   var entity;
   
   tile.X = ToInt(Math.Ceil((Scene.MouseX-board.X)/64)-1);
   tile.Y = ToInt(Math.Ceil((Scene.MouseY-board.Y)/64)-1);
      
   if (tile.X < 0 || tile.X > board.Width-1 || tile.Y < 0 || tile.Y > board.Height-1) {
   
      swap = false;
      
   }

   else {

      if (swap == false) {
         
         picked.X = ToInt(Math.Ceil((Scene.MouseX-board.X)/64)-1);
         picked.Y = ToInt(Math.Ceil((Scene.MouseY-board.Y)/64)-1);
         
         picked.Color = arrTiles[picked.Y*board.Width+picked.X];
      
         swap = true;      
      
      }
      else {
               
         current.X = ToInt(Math.Ceil((Scene.MouseX-board.X)/64)-1);
         current.Y = ToInt(Math.Ceil((Scene.MouseY-board.Y)/64)-1);

         if ( ((current.X == picked.X - 1) && (current.Y == picked.Y)) || ((current.X == picked.X + 1) && (current.Y == picked.Y)) || ((current.Y == picked.Y - 1) && (current.X == picked.X)) || ((current.Y == picked.Y + 1) && (current.X == picked.X))) {

            Game.Interactive = false;

            current.Color = arrTiles[current.Y*board.Width+current.X];

            arrTiles[picked.Y*board.Width+picked.X] = current.Color;
            arrTiles[current.Y*board.Width+current.X] = picked.Color;

            swap = false;
               
            CheckDestroyed();
            Redraw(); //test
            Sleep(sleeper); //test
            Falling();

            Game.Interactive = true;
         
         }         

         else {
            swap = false;
         }
         
      }
         
      Redraw();      

   }
}

// scene state
global StatePuzzle_Game;
// default values
if(StatePuzzle_Game==null){
  StatePuzzle_Game.Visited = false;
  // add scene states here
}

if(!StatePuzzle_Game.Visited) {
  StatePuzzle_Game.Visited = true;    
}

function Initialize() {

   var i, j;
   var entity;
   var reg;

   tile.Width = 64;
   tile.Height = 64;
   tile.X = 0;
   tile.Y = 0;
   board.Width = 8;
   board.Height = 8;
   board.X = (1024 - (board.Width * tile.Width) ) / 2;
   board.Y = (768 - (board.Height * tile.Height) ) / 2;
   picked.Color = 0;
   picked.X = 0;
   picked.Y = 0;
   current.Color = 0;
   current.X = 0;
   current.Y = 0;

   for(j=0; j<board.Height; j=j+1) {     
      for(i=0; i<board.Width; i=i+1) {      

         arrEnts[j*board.Width+i] = Scene.LoadEntity("scenes\Puzzle_Game\gfx\tiles\tile.entity");

         entity = arrEnts[j*board.Width+i];
         entity.X = board.X + i * tile.Width;
         entity.Y = board.Y + j * tile.Height;
         entity.Scalable = false;
         entity.Interactive = false;
         entity.StickToRegion("foreground");

         arrTiles[j*board.Width+i] = 0;

         arrDestroyed[j*board.Width+i] = 0;  
      }
   }
}

function Redraw() {

   var Filename;
   
   for(var j=0; j<board.Height; j=j+1) {
      for(var i=0; i<board.Width; i=i+1) {
         var entity;
         entity = arrEnts[j*board.Width+i];
         Filename = "scenes\Puzzle_Game\gfx\tiles\" + arrTiles[j*board.Width+i] + ".png";         
         if(entity.GetSprite()!=Filename) entity.SetSprite(Filename);
      }
   }
}

function Randomize() {

   for(var j=0; j<board.Height; j=j+1) {     
      for(var i=0; i<board.Width; i=i+1) {      
         if (arrTiles[j*board.Width+i] == 0) {
            arrTiles[j*board.Width+i] = Random (2,6);  
         }
      }
   }
}

function CheckDestroyed() {

   var aCount = 0;
   var destroyed = false;
   var i = 0;
   var j = 0;

   for(j=0; j<board.Height-2; j=j+1)
      {     
         for(i=0; i<board.Width; i=i+1)
         {      
            if (arrTiles[j*board.Width+i] == arrTiles[(j+1)*board.Width+i] && arrTiles[(j+1)*board.Width+i] == arrTiles[(j+2)*board.Width+i])
            {
               arrDestroyed[j*board.Width+i] = 1;
               arrDestroyed[(j+1)*board.Width+i] = 1;
               arrDestroyed[(j+2)*board.Width+i] = 1;
            }         
         }
      }

   for(j=0; j<board.Height; j=j+1)
      {     
         for(i=0; i<board.Width-2; i=i+1)
         {      
            if (arrTiles[j*board.Width+i] == arrTiles[j*board.Width+(i+1)] && arrTiles[j*board.Width+(i+1)] == arrTiles[j*board.Width+(i+2)])
            {
               arrDestroyed[j*board.Width+i] = 1;
               arrDestroyed[j*board.Width+(i+1)] = 1;
               arrDestroyed[j*board.Width+(i+2)] = 1;
            }         
         }
      }

      for(j=0; j<board.Height; j=j+1) {     
         for(i=0; i<board.Width; i=i+1) {      
            if (arrDestroyed[j*board.Width+i] == 1) {
               arrTiles[j*board.Width+i] = 1;
               destroyed = true;
            }
         
         }
      }

   return destroyed;
}

function ResetDestroyed() {
   for(var j=0; j<board.Height; j=j+1) {     
      for(var i=0; i<board.Width; i=i+1) {      
         arrDestroyed[j*board.Width+i] = 0;  
      }
   }
}

function Falling() {

   var loop = true;
   var i;
   var j;
   var k;
   var bonus = 1;

   while (loop == true) {

      for (k=0; k<board.Height ; k=k+1) {
         for(j=board.Height-1; j>0; j=j-1) {     
            for(i=board.Width-1; i>=0; i=i-1) {      
               if (arrTiles[j*board.Width+i]==1) {
                  arrTiles[j*board.Width+i]=arrTiles[(j-1)*board.Width+i];
                  arrTiles[(j-1)*board.Width+i]=1;
               }      
            }
         }
      }

      Redraw();//test
      Sleep(sleeper);//test
      
      for(j=0; j<board.Height; j=j+1) {     
         for(i=0; i<board.Width; i=i+1) {                                          
            if (arrTiles[j*board.Width+i] == 1) {
               arrTiles[j*board.Width+i] = Random (2,6);  

            }
         }
      }

      Redraw();//test
      Sleep(sleeper);//test

      Game.Msg(CountDestroyed());

      ResetDestroyed();
   
      if (CheckDestroyed()==false) {
         loop = false;      
      };

      Game.Msg("Bonus x"+bonus);
      bonus = bonus + 1 ;
   }
}

function CountDestroyed() {

   var counter = 0;
   var i;
   var j;

   for(j=0; j<board.Height; j=j+1) {     
      for(i=0; i<board.Width; i=i+1) {                                          
         if (arrDestroyed[j*board.Width+i] == 1) {
            counter = counter + 1;
         }
      }
   }

   return counter;

}

« Last Edit: April 01, 2011, 10:11:35 PM by siatek »
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Match 3 game - source code
« Reply #1 on: March 28, 2011, 07:42:38 AM »

Thanks :) Although perhaps a small project demonstrating the script would be helpful.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave
 

Page created in 0.02 seconds with 20 queries.