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; 
 
setcolor(7);
}
else if(player2health<=0)
{
setcolor(55);
gotoxy(28,23);
cout<<"Player#1 Wins!"<<endl;
 
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 --;
}
} 
else if (player=='*')
{ 
if ( ( mazeData[row][col] ) ==73)
{
mazeData[row][col] =32;
player2health --;
 
}
} 
}
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 --;
 
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 --;
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 --;
 
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 --;
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 --;
 
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 --;
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 --;
 
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);
 
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<<" ";
} 
 
}

hi... i really want to help.. but can u please next time use indention in your codes.. so its easy to read..


actually i think i know ur problem..

i encountered it when im still doing C games..

but its hard to explain the solution i did. i just hope u understood.

first.
wat i did is make a infiniteloop or the main loop, it calls the
paintBAckground
paintPLAYERS
PAintAccesories(laser,other moving pieces)
and otheres..

the key there is to have a array that hold the position of each unit, players, lasers so u can paint it anytime u want it..

i hope u undrstand..

regards to your game

This article has been dead for over six months. Start a new discussion instead.