#include <iostream>
using namespace std;
//find change of x and change of y to find positionX or positionY
bool north = false;
bool south = false;
bool west = false;
bool east = false;
char direction;
int PlayerX, PlayerY;
char MapChars[3] = {'@','.','^'};
const int Player = 0;
const int Grass = 1;
const int Mountain = 2;
const int MapSizeX = 10, MapSizeY = 10;
int BaseMap[MapSizeX][MapSizeY] =
{
{1, 1, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
};
void abovePlayer();
void reDraw();
void checkDirection();
int main()
{
reDraw();
return 0;
}
void loopGame()
{
cout<< "> ";
cin>> direction;
switch(direction)
{
case 'w':
north = true;
checkDirection();
break;
case 's':
south = true;
checkDirection();
break;
case 'd':
west = true;
checkDirection();
break;
case 'a':
east = true;
checkDirection();
break;
default:
loopGame();
}
}
void reDraw()
{
system("cls");
for (int y = 0; y < MapSizeY; y++)
{
for (int x = 0; x < MapSizeX; x++)
{
if (x == PlayerX && y == PlayerY)
{
cout << MapChars[Player];
}
else
{
cout << MapChars[(BaseMap[x][y])];
}
}
cout << endl;
}
cin.ignore();
loopGame();
}
void checkBoundaries()
{
// make it so that if Map[Mountain] player can't go there...
}
void checkDirection()
{
if (north == true)
{
if (PlayerY - 1 != Mountain)
{
PlayerX = PlayerX;
PlayerY = PlayerY - 1;
north = false;
reDraw();
}
else if(PlayerY - 1 == Mountain)
{
reDraw();
}
}
else if (east == true)
{
if (PlayerX - 1 != Mountain)
{
PlayerX = PlayerX - 1;
PlayerY = PlayerY;
east = false;
reDraw();
}
else if (Player - 1 == Mountain)
{
reDraw();
}
}
else if (west == true)
{
if (PlayerX + 1 != Mountain)
{
PlayerX = PlayerX + 1;
PlayerY = PlayerY;
west = false;
reDraw();
}
else if (PlayerX + 1 == Mountain)
{
reDraw();
}
}
else if (south == true)
{
if (PlayerY + 1 != Mountain)
{
PlayerX = PlayerX;
PlayerY = PlayerY +1;
south = false;
reDraw();
}
else if (PlayerY + 1 == Mountain)
{
reDraw();
}
}
}
Help with collisions...
Okay, this makes sense to me, but the program doesn't work like its suppose to.
It's suppose to do the collision detection in which if the next tile for the player is going to be a mountain the person can't move there ('@'). Been playing with it for a while if anyone has any ideas I would greatly appreciate the input. Oh, btw the moving works fine if I just take out the if statements about == the mountain...
When you say if (PlayerX - 1 != Mountain), I think you mean if ( BaseMap[PlayerX - 1][PlayerY] != Mountain). It will actually check the tile on the map instead of the value ;) What you were saying was: If the player's X position minus 1 is equal to 2. Therefore if you were at <3, 4> trying to move west, you couldn't since 3 - 1 == 2.
Make sure that PlayerX and PlayerY don't fall past 0 or rise past Mapsize - 1
EDIT:
When using the else statement, you don't need to check PlayerX - 1 == Mountain because it's definately not PlayerX - 1 != Mountain; that's why it's in the else part anyways!
Also, I've noticed that you are calling reDraw() if north, east, south, or west is true. So, why not perform this code right before the end of checkDirection()?
Make sure that PlayerX and PlayerY don't fall past 0 or rise past Mapsize - 1
EDIT:
if (PlayerX - 1 != Mountain){ PlayerX--; PlayerY = PlayerY; east = false; reDraw();}else { reDraw();}
When using the else statement, you don't need to check PlayerX - 1 == Mountain because it's definately not PlayerX - 1 != Mountain; that's why it's in the else part anyways!
Also, I've noticed that you are calling reDraw() if north, east, south, or west is true. So, why not perform this code right before the end of checkDirection()?
if ( north || east || south || west ) reDraw();
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement