Friday, September 10, 2010

Slow motion movement on the grid

We had an idea of making the movement on the board, or I intend to call it grid, animated one. Instead of just simply displaying the last position, we display the player(s) position one step at a time. There are some issues in implementing the design, but this is the early design that works quite well.
Well, good enough for designing it in 5 hours, while watching Monster vs Alien...

/*
Problems: 
1.current_j and current_j values is wrong
2.
*/
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
using namespace std;

const int row=9,col=8;
int i,j,current_i,current_j,step;                  //global variables

struct WORLD                                       //world of the game
{
    char* board[row][col];                         //the board, or the playing field
    int game_element[row][col];                    //1 resting,2 question,3 portal/jump,4 battle
}world;                                            //note: make game_element at [0][0],[9][8]

int dice(void);
void intBoard(void);
void intgame_element(void);
void displayBoard(void);
void stepsBoard(int steps);
void slowstepsBoard(int steps);
void wait(float seconds);


/*void displayelement (void)
{
     for (i=0;i<row;i++)
     {
         for (j=0;j<col;j++)
             cout<<world.game_element[i][j]<<' ';
        
         cout<<endl;
     }        
}*/

int main()
{
    srand(time(NULL)) ;                           //set rand to use time as seed
    intBoard();
    intgame_element();
//    displayelement();
    displayBoard();
    label:
    system("PAUSE");
    system("cls");
    int steps=dice();
    //stepsBoard(steps);
    slowstepsBoard(steps);
    cout<<steps<<endl; //dice value
    cout<<"Coordinate:("<<(current_i+1)<<','<<(current_j+1)<<')'<<endl;
    if ( ( strcmp(world.board[current_i][current_j],"  P 1  ") != 0))
       goto label;
    else
    {
       cout<<endl<< "You won!"<<endl;
       system("PAUSE");                            
       return 0;
    }
    
}
   

int dice(void)
{
    return rand() % 6 + 1;
}
//======================================================================================================
//initialize board
void intBoard(void)
{
    for(i=0;i<row;i++)
        for(j=0;j<col;j++)
          world.board[i][j]="       ";
    world.board[0][0]=" START ";
    world.board[8][7]="  END  ";
    current_i=0; current_j=0;
}
//======================================================================================================
//initialize game_element
void intgame_element(void)
{
     for(i=0;i<row;i++)
        for(j=0;j<col;j++)
          world.game_element[i][j]=rand() % 4 + 1;
    world.game_element[row-1][col-1]=0; world.game_element[0][0]=0;
}
//======================================================================================================
//show game board
void displayBoard(void)
{
   world.board[0][0]=" START ";

    for(i=0;i<row;i++)
   {     cout<<endl;
        for(j=0;j<(col);j++)
            cout<<"========";
      cout<<endl<<'|';
      if (i%2==1)
          for(j=(col-1);j>=0;j--)
             cout<<world.board[i][j]<<'|';
      else
          for(j=0;j<col;j++)
              cout<<world.board[i][j]<<'|';
   }
   cout<<endl;
       for(j=0;j<(col);j++)
            cout<<"========";
         cout<<endl;
}
//======================================================================================================
//stepping around board
void stepsBoard(int steps)
{
   world.board[current_i][current_j]="       ";
   current_j=current_j+steps;
   if (current_j>=col)
   {
        current_j=current_j-col;
         current_i++;
   }
   world.board[(current_i)][(current_j)]="  P 1  ";
   if (current_i==row||(current_i==8&&current_j==6))//when reach destination or over it, assign to
   {
      intBoard();
      world.board[0][0]=" START ";
      world.board[8][7]="  P 1  ";   //last destination and break out of loop
      displayBoard();
   }
   if (current_i<0||current_j<0) //when going back to start
   {
      intBoard();
      world.board[0][0]=" START ";
      current_i=0; current_j=0;
      displayBoard();
   }
   else
      displayBoard();
}
//======================================================================================================
//to slow down the display of the board
void wait(float seconds)
{
  clock_t endwait;
  endwait=clock()+seconds*CLOCKS_PER_SEC;
  while(clock()<endwait){}   // jarak antara nombor yang akan dipaparkan
}
//======================================================================================================
//stepping around board-slow motion
void slowstepsBoard(int steps)
{
     
     world.board[current_i][current_j]="       ";
     for (int count=0;count<(steps)&&current_i<(row-1);count++)
     {
         if (current_j>=col)
         {
             current_j=current_j-col;
             current_i++;
         }
         system("cls");
         if (current_i==8&&current_j==6)
         {
            world.board[0][0]=" START ";
            world.board[8][7]="  P 1  ";   
            displayBoard();
            current_i=(8); current_j=(7);
         }
         world.board[current_i][current_j]="  P 1  ";
         displayBoard();
         wait(0.2);
         world.board[current_i][current_j]="       ";
         current_j++;       
     }
}
//======================================================================================================

No comments:

Post a Comment