Hello B4DC0D3,
Going over your program I had some compile problems.
Before I continue you did not mention what IDE/compiler you are using, so I do not know the correct way to do this. In my MSVS 2017 there is a menu option called "Tools" in the drop down menu the last choice is "Options". After choosing "Options" You will need to find something that deals wit the "Text Editor" then something like "C/C++" -> "Tabs". Whether you have "Tab Size" or "Indent Size" or both make them 4 and choose to use spaces for the tab key. This will take the exaggeration out of the indenting that happens with the tab key being defined as a tab character.
To start with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <string>
#include <iostream>
#include <windows.h>
#include <cstdlib>
//#include <ctime> // <--- Not used.
#include <chrono>
#include <thread>
//#include <dos.h> // <--- Not needed and is not used in the program.
//#include <stdlib.h> // <--- Should be <cstdlib> and redundant.
//#include <stdio.h> // <--- Should be covered and or included in <iostream>. Also should be <cstdio>
using namespace std;
char map[20][20] = // <--- (=) not needed with the uniform initializer {}. Also would be better defined in "main".
{
"#########", //if you want to create a map:
"# #", // # Wall, & Medkit,
"# ### #*#", // @ Player, * Enemy
"# #& #*#", // = Exit, - Door (door only opens with a key)
"# # #*#", // ~ key, H Bigger enemy,
"# #& #H#",
"# # #~#",
"# #& ###",
"#@# &#",
"#### ####",
"#& *#",
"# * #",
"# * *#",
"#& #",
"#& * =",
"####-####",
"# H #",
"#& & & &#",
"#########" // <--- This (,) not needed.
};
constexpr int gameSpeed = 100;
//int level = 1;
//bool stopgame = false;
//int hp = 50;
//int hpmax = 50;
//bool key = false;
int main(int argc, char** argv)
{
constexpr int MAXSIZE{ 20 };
int level = 1;
bool stopgame = false;
int hp = 50;
int hpmax = 50;
bool key = false;
while (stopgame == false && level == 1)
{
std::this_thread::sleep_for(std::chrono::milliseconds(gameSpeed));
system("cls");
|
As a global variable I changed "gameSpeed" to a constant because it should not be changed by the program. If you need to remove the "constexpr" and move into "main". The other variables are better defined in "main" where they are used.
A good practice is to avoid using global variables. As you programs grow and you start using functions it will be more difficult to track does where something went wrong and changed a global variable when you did not want it changed. The compiler does not warn or cause an error when you change a global variable.
Line 56 makes use of the header files <chrono> and <thread> along with the variable "gameSpeed". It helps make the display less jumpy with each clear screen you call each time through the loop.
Your for loops would work better as
for (int y = 0; y < MAXSIZE; y++)
. This works with line 46 in the above code. This way you only have 1 place to change and every MAXSIZE will reflect this change. It saves time going through all of you code to make changes ans you are likely to miss 1 or 2 somewhere.
Another option is to define "MAXROWS" and "MAXCOLS" to work with your array better. As it is you have more rows and columns than you need based on what you are using.
You case statements can be written as:
1 2 3 4 5
|
case '#':
map[y][x] = 219;
break;
case 'a':
|
The {}s are not needed for a case unless you are defining a variable, but the drawback to that is when the closing } is reached the variable is destroyed an not usable outside of the block. The blank line is optional.
The other reason I have used this is the 219. 219 is larger than a "signed char" can hold, so you may not be getting the character you want when it cuts the 219 down to something a "signed char" can hold. I did fix the problem be defining the array as
unsigned char map[20][20]
.
In the if statement for VK_DOWN you have:
1 2 3 4 5 6 7 8 9 10 11
|
case '=':
{
level = 2;
}
case '*':
{
hp -= 5;
map[y][x] = ' ';
y += 1;
map[y2][x] = '@';
}break;
|
First off yo do not need the {}s. Second when "case '=':" is chosen it falls through to "case '*':" before it finds a break to leave the switch. I do not think that is what you want.
In the "case '=':" you set level, but you have no idea what the value of "key" is, so all it does is exit when it gets back to the while loop to find that "level" no longer == 1. And speaking of that the value of "stopgame" never changes in your program. So does it really have any use?
It looks like you will need to check and change all the "case '=':"s to only work if "key" is true.
Your last while loop at the end of "main" is an endless loop because neither "stopgame" or "level" have their values changed while inside the loop, so there is no way out. I did add this to the loop at the end
stopgame = true;
. This worked and allowed the program to end. The end result here is that the while loop is not needed. I would suggest:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
} // End of while loop
system("cls");
cout <<
"\n\n -YOU WIN-"
"\n Made by kevin 'Noclip' L. from canada and"
"\n Roberto 'B4DC0D3' de gennaro from argentina\n\n";
std::this_thread::sleep_for(std::chrono::seconds(5));
return 0; // <--- Not required, but makes a good break point.
}
|
The 5 is the number of whole seconds to pause, sleep or wait.
Just a thought. Your last screen would look like:
-YOU WIN-
Made by kevin 'Noclip' L. from canada and
Roberto 'B4DC0D3' de gennaro from argentina
|
Keep or not. Change anyway you like.
Andy