Sry guys..havin troubl figurin out sum of this posting stuff.....


OK guys, Im writing this game, and i can't seem to figure out one thing. This is an extra fun thing that one of my Profs suggested. If i get it working like i want..he may give me extra credit. So heres the problem. This is a game where 2 guys(icons), move through a map and can lay bombs and shoot at each other. the players move using the arrow keys and a,w,d,x. they lay bombs using pageup and "f". They shoot using j and t. What i need to figure out is how to allow them to move while the laser is shooting. The laser uses a loop, and in order to move, the loop has to finish. If you have any ideas..id love to hear them. Thanks!

#include <cstdlib>
#include <iostream>
#include <conio2.h>
#include <fstream>
#include <time.h>
#include <windows.h>
#include <winbase.h>

#define SND_FILENAME 0x20000
#define SND_ASYNC 1

using namespace std;
const int ROWS=24;
const int COLS=79;
void paintMazeAndScreen(ifstream& inFile, int mazeData[][COLS+1]);
void initialPlacement(int& row1, int& col1, int& row2, int& col2,int,int);
void potentialUpMove(char player, int& row, int& col, int mazeData[][COLS+1],int&,int&,int row1,int row2, int col1,int col2,int& lastMove);
void potentialDownMove(char player, int& row, int& col, int mazeData[][COLS+1],int&,int&,int row1,int row2, int col1,int col2,int& lastMove);
void potentialRightMove(char player, int& row, int& col, int mazeData[][COLS+1], int&,int&,int row1,int row2, int col1,int col2,int& lastMove1);
void potentialLeftMove(char player, int& row, int& col, int mazeData[][COLS+1], int&,int&,int row1,int row2, int col1,int col2,int& lastMove1 );
void handleTraps(char player, int& row, int& col, int mazeData[][COLS+1], int& player1health, int& player2health);
void setcolor(unsigned short color);
void shootLaser(int mazeData[][COLS+1], int row, int col,int lastMove,int row1, int col1, int row2,int col2, int& player1health, int& player2health, bool& dodge);
int main()
{
 ifstream inFile;
 
 int x;
 int row1;
 int col1;
 int row2;
 int col2;
 char player1='+';
 char player2='*';
 int player1health=5;
    int player2health=5;
    int play1TrapCount=20;
    int play2TrapCount=20;
 int mazeData[ROWS+1][COLS+1]; 
    int lastMove1;
    int lastMove2;
    bool Dodge;
 //Write a grid to the screen from a file
 inFile.open("maze.txt");
 if (inFile)
 {
        paintMazeAndScreen(inFile, mazeData);
        initialPlacement(row1,col1,row2,col2,player1health,player2health); 
       
        int row = 1;
        int col = 1; 
          
  while(player1health>=1&&player2health>=1)
  {
        
         
     if (kbhit())
   {
                
    x=getch();
                 
    
    switch(x)
    {
     //up
        case 72: potentialUpMove(player1, row1, col1, mazeData, player1health, player2health,row1,row2,col1,col2,lastMove1);break;
                 case 119: potentialUpMove(player2, row2, col2, mazeData, player1health, player2health,row1,row2,col1,col2,lastMove2);break;
                
     //down
     case 80: potentialDownMove(player1, row1, col1, mazeData,player1health, player2health,row1,row2,col1,col2,lastMove1);break;
     case 120: potentialDownMove(player2, row2, col2, mazeData,player1health, player2health,row1,row2,col1,col2,lastMove2);break;
     
     //left
     case 75: potentialLeftMove( player1, row1, col1, mazeData,player1health, player2health,row1,row2,col1,col2,lastMove1);break;
     case 97: potentialLeftMove( player2, row2, col2, mazeData,player1health, player2health,row1,row2,col1,col2,lastMove2);break;
     
     //right
     case 77: potentialRightMove(player1, row1, col1, mazeData,  player1health, player2health,row1,row2,col1,col2,lastMove1);break;
     case 100:potentialRightMove(player2, row2, col2, mazeData,  player1health, player2health,row1,row2,col1,col2,lastMove2);break;
     
     
     //Traps
                 case 73:
                        if(play1TrapCount>0)
                        {
                            if(mazeData[row1][col1]!=73)
                            {
                            mazeData[row1][col1]=73;
                            play1TrapCount --;
                            }
                        }
                        break; 
                   case 102:
                        if(play2TrapCount>0)
                        {
                            if(mazeData[row2][col2]!=102)
                            {
                            mazeData[row2][col2]=102;
                            play2TrapCount --;
                            }
                        }
                        break;
                        case 106: shootLaser( mazeData,  row1,  col1,lastMove1, row1,  col1,  row2,  col2,  player1health,  player2health, Dodge);break;
                        case 116: shootLaser( mazeData,  row2,  col2,lastMove2, row1,  col1,  row2,  col2,  player1health,  player2health, Dodge);break;        
    }
    
  
     gotoxy (col1,row1);   
     cout << '+';
   
     gotoxy (col2,row2);   
     cout << '*';     
    
   }
        
        }
 }
 else
 {
  cout << "Unable to open grid file"<<endl;
 }
 if(player1health<=0)
 {  
        setcolor(55);
        gotoxy(28,23);
        cout<<"Player #2 Wins!"<<endl;   
        PlaySound("applause_2.wav",NULL,SND_FILENAME|SND_ASYNC); 
        setcolor(7);
    }
    else if(player2health<=0)
    {
        setcolor(55);
        gotoxy(28,23);
        cout<<"Player#1 Wins!"<<endl;
        PlaySound("applause_2.wav",NULL,SND_FILENAME|SND_ASYNC); 
        setcolor(7);
    }
    gotoxy(28,24);
    system("PAUSE");
    return EXIT_SUCCESS;
}
void initialPlacement(int& row1, int& col1, int& row2, int& col2, int player1health,int player2health)
{
 row1 = 1;
    col1 = 1;      
 row2 = 24;
    col2 = 79; 
    gotoxy(col1,row1);
 cout << '+';
    gotoxy(col2,row2);
 cout << '*';
 gotoxy(col1,row1); 
    gotoxy(60,25);
    cout<<"player1health: "<<player1health;
    gotoxy(1,25);
     cout<<"player2health: "<<player2health;  
}
void paintMazeAndScreen(ifstream& inFile, int mazeData[][COLS+1])
{
    setcolor(3);
 for(int row=1;row<=ROWS;row++)
 {
  for(int col=1;col<=COLS;col++)
  {
   inFile >> mazeData[row][col];
   gotoxy(col,row);
   cout << char(mazeData[row][col]);
  }
 }
 setcolor(7);
}
void potentialUpMove(char player, int& row, int& col, int mazeData[][COLS+1], int& player1health,int& player2health,int row1,int row2, int col1,int col2,int& lastMove)
 {
    if( player=='+')    
   {
         lastMove=1;
    }
    else
    {
    lastMove=5;
    }
  if ((row >= 1))
 {
  if (   ( (mazeData[row-1][col]==32) || (mazeData[row-1][col] ==73) ||(mazeData[row-1][col] ==102)) 
       && (!((row-1)==(row2)&& (col==col2))) && (!( (row-1)==(row1) &&(col==col1)))   ) 
                                           
       
  {
          gotoxy(col,row);
          cout<<" "; 
   row--;
   
   handleTraps (player,  row,  col,  mazeData, player1health,  player2health);
    
      gotoxy(60,25);
      cout<<"player1health: "<<player1health;
      gotoxy(1,25);
      cout<<"player2health: "<<player2health;          
               
        }
    }
}
void potentialDownMove(char player, int& row, int& col, int mazeData[][COLS+1],int& player1health,int& player2health,int row1,int row2, int col1,int col2,int&  lastMove)
{
     if( player=='+')    
   {
    lastMove=2;
    }
    else
    {
    lastMove=6;
    }
    
   if ((row < 23)) 
 {
  if ( ( (mazeData[row+1][col]==32)||(mazeData[row+1][col]==73)||(mazeData[row+1][col]==102))
  &&(!((row+1)==(row2)&&(col==col2))) &&(!( (row+1)==(row1) &&(col==col1)))   )
  
  {
             gotoxy(col,row);
          cout<<" "; 
        row++;
      handleTraps (player,  row,  col,  mazeData, player1health,  player2health);
      
      gotoxy(60,25);
      cout<<"player1health: "<<player1health;
      gotoxy(1,25);
      cout<<"player2health: "<<player2health;                 
        }
     }
}     
void potentialRightMove(char player, int& row, int& col, int mazeData[][COLS+1], int& player1health,int& player2health,int row1,int row2, int col1,int col2,int& lastMove)
{
     if( player=='+')    
   {
         lastMove=3;
    }
    else
    {
    lastMove=7;
    }
   
    if ((col < 80)) 
   {
  if ( ( (mazeData[row][col+1]==32) ||(mazeData[row][col+1]==73) ||(mazeData[row][col+1]==102) )
  &&(!((col+1)==(col2)&&(row==row2))) &&(!( (col+1)==(col1) &&(row==row1)))  )
  
  {
           gotoxy(col,row);
          cout<<" ";  
  col++;
    handleTraps (player,  row,  col,  mazeData, player1health,  player2health);
    
      gotoxy(60,25);
      cout<<"player1health: "<<player1health;
      gotoxy(1,25);
      cout<<"player2health: "<<player2health;                  
        }
   }
}
void potentialLeftMove(char player, int& row, int& col, int mazeData[][COLS+1], int& player1health,int& player2health,int row1,int row2, int col1,int col2,int& lastMove)
{
    if( player=='+')    
   {
         lastMove=4;
    }
    else
    {
    lastMove=8;
    }
    
     if ((col > 1))
 {
  if ( ( (mazeData[row][col-1]==32)||(mazeData[row][col-1]==73) ||(mazeData[row][col-1]==102) )
  &&(!((col-1)==(col2)&&(row==row2))) &&(!( (col-1)==(col1) &&(row==row1))) )
  
  {
         gotoxy(col,row);
          cout<<" "; 
     col--;
    handleTraps (player,  row,  col,  mazeData, player1health,  player2health);
    
      gotoxy(60,25);
      cout<<"player1health: "<<player1health;
      gotoxy(1,25);
      cout<<"player2health: "<<player2health;                   
        }
 }
}
void handleTraps(char player, int& row, int& col, int mazeData[][COLS+1], int& player1health, int& player2health)
{
    if( player=='+')
    {
  if ( ( mazeData[row][col] )  ==102)
   {
        mazeData[row][col] =32;
        player1health --;
        PlaySound("explosion-02.wav",NULL,SND_FILENAME|SND_ASYNC); 
    }
    }                  
    else if (player=='*')
    {    
     if ( ( mazeData[row][col] )  ==73)
       {
            mazeData[row][col] =32;
            player2health --;
             PlaySound("explosion-02.wav",NULL,SND_FILENAME|SND_ASYNC); 
            }
        }           
}
void setcolor(unsigned short color)                 //The function that
{                                                   //sets the colour
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}
void shootLaser(int mazeData[][COLS+1], int row, int col,int lastMove,int row1, int col1, int row2,int col2, int& player1health, int& player2health, bool& Dodge)
{
    //shoot up first player
    if(lastMove==1)
    {
     while(mazeData[row-1][col]==32||(mazeData[row-1][col]==73) ||(mazeData[row-1][col]==102) )
    {
        Dodge=true;
        if((row-1==row2)&&(col==col2))
        {
            player2health --;
            player2health --;
            PlaySound("bang_1.wav",NULL,SND_FILENAME|SND_ASYNC); 
            gotoxy(1,25);
            cout<<"player2health: "<<player2health;
            
            
        }
      Dodge=false;   
    setcolor (12);   
    gotoxy(col,row-1);
    cout<<"|"<<endl;
    Beep(200,10);  
    row--;
    Sleep(100);
    gotoxy(col,row);
    cout<<" "<<endl;
   
    setcolor(7);   
    
    }
 gotoxy(col,row);
 cout<<" ";                 
    }
    //shoot down first player
    else
  if(lastMove==2)
    {
     while(mazeData[row+1][col]==32||(mazeData[row+1][col]==73) ||(mazeData[row+1][col]==102) )
    {
        Dodge=true;
        if((row+1==row2)&&(col==col2))
        {
            player2health --;
            player2health --;
            PlaySound("bang_1.wav",NULL,SND_FILENAME|SND_ASYNC); 
            gotoxy(1,25);
            cout<<"player2health: "<<player2health;
             
        }
        Dodge=false;
     setcolor (12);   
    gotoxy(col,row+1);
    cout<<"|"<<endl;
    row++;
    Beep(200,10);
    Sleep(100);
    gotoxy(col,row);
    cout<<" "<<endl;
    
    
    setcolor(7);    
    }
 gotoxy(col,row);
 cout<<" ";        
    }
    //shoot right first player
  else
  if(lastMove==3)
    {
    while(mazeData[row][col+1]==32||(mazeData[row][col+1]==73) ||(mazeData[row][col+1]==102) )
    {
        if((row==row2)&&(col+1==col2))
        {
            Dodge=true;
            player2health --;
            player2health --;
            PlaySound("bang_1.wav",NULL,SND_FILENAME|SND_ASYNC); 
            gotoxy(1,25);
            cout<<"player2health: "<<player2health;
             
        }
        Dodge=false;
     setcolor (12);   
    gotoxy(col+1,row);
    cout<<"-"<<endl;
    col++;
    Beep(200,10);
    Sleep(100);
    gotoxy(col,row);
    cout<<" "<<endl;
    
    
    setcolor(7);    
    }
 gotoxy(col,row);
 cout<<" "; 
}  
    //shoot left   first player
   else
   if (lastMove==4)
    {
     while(mazeData[row][col-1]==32||(mazeData[row][col-1]==73) ||(mazeData[row][col-1]==102) )
    {
        Dodge=true;
        if((row==row2)&&(col-1==col2))
        {
            player2health --;
            player2health --;
            PlaySound("bang_1.wav",NULL,SND_FILENAME|SND_ASYNC); 
            gotoxy(1,25);
            cout<<"player2health: "<<player2health;
             
        }
        Dodge=false;
    setcolor (12);   
    gotoxy(col-1,row);  
    cout<<"-"<<endl;
    col--;
    Beep(200,10);
    Sleep(100);
    gotoxy(col,row);
    cout<<"  "; 
    
    setcolor(7);
    }
    
gotoxy(col,row);
cout<<" ";
    }    
  //shoot up second player
    if(lastMove==5)
    {
     while(mazeData[row-1][col]==32)
    {
        Dodge=true;
        if((row-1==row1)&&(col==col1))
        {
            player1health --;
            player1health --;
            PlaySound("bang_1.wav",NULL,SND_FILENAME|SND_ASYNC); 
            gotoxy(60,25);
            cout<<"player1health: "<<player1health;
             
        }
        Dodge=false;
     setcolor (10);   
    gotoxy(col,row-1);
    cout<<"|"<<endl;
    row--;
    Beep(200,10);
    Sleep(100);
    gotoxy(col,row);
    cout<<" "<<endl;
    
    setcolor(7);    
    }
 gotoxy(col,row);
 cout<<" ";                 
    }
    //shoot down second player
    else
  if(lastMove==6)
    {
     while(mazeData[row+1][col]==32)
    {
        Dodge=true;
        if((row+1==row1)&&(col==col1))
        {
            player1health --;
            player1health --;
            PlaySound("bang_1.wav",NULL,SND_FILENAME|SND_ASYNC); 
            gotoxy(60,25);
            cout<<"player1health: "<<player1health;
             
        }
        Dodge-false;
     setcolor (10);   
    gotoxy(col,row+1);
    cout<<"|"<<endl;
    row++;
    Beep(200,10);
    Sleep(100);
    gotoxy(col,row);
    cout<<" "<<endl;
    
    
    setcolor(7);    
    }
 gotoxy(col,row);
 cout<<" ";        
    }
    //shoot right second player
  else
  if(lastMove==7)
    {
    while(mazeData[row][col+1]==32)
    {
        Dodge=true;
        if((row==row1)&&(col+1==col1))
        {
            player1health --;
            player1health --;
            PlaySound("bang_1.wav",NULL,SND_FILENAME|SND_ASYNC); 
            gotoxy(60,25);
            cout<<"player1health: "<<player1health;
             
        }
        Dodge=false;
     setcolor (10);   
    gotoxy(col+1,row);
    cout<<"-"<<endl;
    col++;
    Beep(200,10);
    Sleep(100);
    gotoxy(col,row);
    cout<<" "<<endl;
    
    
    setcolor(7);    
    }
 gotoxy(col,row);
 cout<<" "; 
}  
    //shoot left  second player
   else
   if (lastMove==8)
    {
     while(mazeData[row][col-1]==32)
    {
        Dodge=true;
        if((row==row1)&&(col-1==col1))
        {
            player1health --;
            player1health --;
            gotoxy(60,25);
            PlaySound("bang_1.wav",NULL,SND_FILENAME|SND_ASYNC); 
            cout<<"player1health: "<<player1health;
             
        }
        Dodge=false;
    setcolor (10);   
    gotoxy(col-1,row);  
    cout<<"-"<<endl;
    col--;
    Beep(200,10);
    Sleep(100);
    gotoxy(col,row);
    cout<<"  "; 
    
    setcolor(7);
    }
    
gotoxy(col,row);
cout<<" ";
    }    
 
}

i think u havent read my reply on your last post.. do check it out..

I indented it this time. Is it still incorrect?

its correct.. but i also give you some advise in that thread.. check it.. then tell me if you understnd

I understand the idea...i was thinkin sumthing like that. Or sum kind of conditional statement before the kbhit. Im having trouble figuring out what to write though.

think of this..


if you are familiar with REFRESH RATE..

this is the clue... if you see any game with refresh rate, it means that the screen is refreshing all time time.. the fatser the better..

so how they do it..

like wat i explain to tyou then...

they paste everything one by one.. even there no changes happening..
see the idea..
explore more man.. i believe in you..

OK. If the maze and players refresh though, will that still allow it to move..even though the loop is still running?

ofcourse..
now the array the hold their position is your cue..

changing the position.. will make it move..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.