/**\brief This file will handle the levels of the game and the core parts of it.**/
#include "Levels.h"
#include "Enemy.h"
usingnamespace std;
externbool stopGame;
externint gameSpeed;
bool levelOne() {
registerchar Map[10][21] = {"####################", //Create the first map with two indexes
/* # is the barrier or walls**/ "#< #",
"# #",
"# #",
"# P #",/// P = Minion Enemy
"# #",
"# #",
"# #",
"# x #",///x = Player: Starting coordinates: (10,10)
"####################"};
while (stopGame == false) {
system("cls");
///RENDER THE MAP:
for (int row = 0; row < 10; row++) { //This iterates the first index (the rows)
cout << "\t\t\t\t\t"; //This centers the map to the screen (or almost centers it -_-)
for (int column = 0;column < 21; column++) { //This iterates the second index (the columns)
cout <<Map[row][column]; //print the finished result
}
cout << endl; //end the line after every row of the column has been sent to cout.
}
for (int y = 0; y < 10; y++) { //This is for the switch statement so that the program will react to certain types /
for (int x = 0;x < 21; x++) { //of objects when we print the map.
switch (Map[y][x])
{
case'x': {
if (GetAsyncKeyState(VK_UP)) {
int y2 = (y-1); // This variable will hold the new increment of the moved player
switch (Map[y2][x])
{
case' ':
{
Map[y][x] = ' ';
y -= 1;
Map[y2][x] = 'x';
}break;
}
}
if (GetAsyncKeyState (VK_DOWN))
{
int y2 = (y+1); // This variable will hold the new increment of the moved player
switch (Map[y2][x])
{
case' ':
{
Map[y][x] = ' ';
y += 1;
Map[y2][x] = 'x';
}break;
}
}
if (GetAsyncKeyState(VK_RIGHT))
{
int x2 = (x+1); // This variable will hold the new increment of the moved player
switch (Map[y][x2])
{
case' ':
{
Map[y][x] = ' ';
x += 1;
Map[y][x2] = 'x';
}break;
}
}
if (GetAsyncKeyState(VK_LEFT))
{
int x2 = (x-1); // This variable will hold the new increment of the moved player
switch (Map[y][x2])
{
case' ':
{
Map[y][x] = ' ';
x -= 1;
Map[y][x2] = 'x';
}break;
}
}
}break;
case'P': {
int x2 = (x-1);
int x2r = (x+1);
switch (Map[y][x2])
{
case' ': {
Map[y][x] = ' ';
x -= 1;
Map[y][x2] = 'P';
}break;
switch (Map[y][x2]) {
case'#': {
Map[y][x] = ' ';
x2r += 1;
Map[y][x2r] = 'P';
}
}
}
};
}
}
}
Sleep(gameSpeed);
}
system("pause");
returntrue;
}
For some reason, When the enemy "P" moves and hits the "#", it stops moving without starting to move in a different direction. I want a linear pattern when this enemy hits the "#", so it will move left and right. How do I fix this?
I think you should have more information than just a character. If i am understanding correctly, you want the monster to change direction when it hits a wall. That means you need something saying which direction it should go and well the data alone "P" can't do that.
You could create some sort of "Entity" class as per usual, and have a board of Entities instead.
Entity* level[10][20] = {};
Having nullptr be an empty space. Then you create a Monster class that derives Entity. If don't know what inheritance is or virtual functions, i would suggest you read up on that.
1 2 3 4
class Monster : public Entity
{
bool movingLeft; // moving right if false
};
Your entity class would have some sort of "update" function. Really there are a couple ways to do this, this is just one way.
Why do you have a switch statement inside another switch statement? You do realize that that should never occur, because it is part of the case ' ' statement that breaks out early.
There is a switch on the character there. If that character is a space, it does things, and then meets the break statement. However, IF that break statement wasn't there, then it could continue on to the next switch, which only has the case of the character being a '#'. Do this instead:
If i understand correctly, you create a pure virtual method in Entity and the Monster derivative of Entity has a method that overrides it. But, how does if(board[i][j]) work? Board is a pointer to an array of Entity objects, so how does it return a boolean value (0 or non zero) when the pointer does not point to an integer?