In an attempt to make a roguelike game, I've posted two threads so far on this forum. Now their's a problem with keyboard input. I'm using "conio.h" and getch(); to have the player control everything, but in the variables that determine the @'s position, something isn't working. Here's the code:
#include <iostream>
#include <cstdlib>
#include <conio.h>
usingnamespace std;
int mainBreak = 0;
int pInput;
int posx;
int posy;
void mapFn();
void playerInput();
int main()
{
int inputTimer = 0;
posx = 1;
posy = 1;
for (mainBreak == 0; mainBreak == 0;)
{
playerInput();
mapFn();
pInput = getch();
system("CLS");
}
return 0;
}
void mapFn()
{
int mapArr[20][20];
int markerX = 0;
int markerY = 0;
int x = 0;
int y = 0;
int mapTracker = 0;
for (markerX = 0; markerX < 20; markerX++)
{
for (markerY = 0; markerY < 20; markerY++)
{
mapArr[markerX][markerY] = 2;
}
}
mapArr[posx][posy] = 1;
for (y = 0; y < 20; y++)
{
for (x = 0; x < 20; x++)
{
if (mapArr[y][x] == 2)
{
cout << ".";
mapTracker++;
}
if (mapArr[y][x] == 1)
{
cout << "@";
mapTracker++;
}
if (mapTracker == 19)
{
cout << "\n";
mapTracker = 0;
}
}
}
}
void playerInput()
{
// Q - NW
if (pInput == 113)
{
posx--;
posy--;
}
// W - N
if (pInput == 119)
{
posy--;
}
// E - NE
if (pInput == 101)
{
posx++;
posy--;
}
// A - W
if (pInput == 97)
{
posx--;
}
// S - Hold
if (pInput == 115)
{
}
// D - E
if (pInput == 100)
{
posy++;
}
// Z - SW
if (pInput == 122)
{
posx--;
posy++;
}
// X - S
if (pInput == 120)
{
posx++;
posy--;
}
// C - SE
if (pInput == 99)
{
posx++;
posy++;
}
if (posx < 1)
{posx = 1;}
if (posy < 1)
{posy = 1;}
}
Posx should be the players position on the x-axis, and posy is the players position on the y-axis. I know that the different movement keys have the correct number because I tested them by having it print what pInput was whenever I pressed a key in a different program. I've tried it a couple of different ways, and come up with no solutions. It isn't even logical; when the @ gets halfway down the screen (by pressing X) it starts going diagonally. Anybody know what stupid mistakes I'm sure I put in there?
#include <iostream>
#include <cstdlib>
#include <conio.h>
usingnamespace std;
int mainBreak = 0;
int pInput;
int posx;
int posy;
void mapFn();
void playerInput();
int main()
{
int inputTimer = 0;
posx = 1;
posy = 1;
for (mainBreak == 0; mainBreak == 0;)
{
playerInput();
mapFn();
pInput = getch();
system("CLS");
}
return 0;
}
void mapFn()
{
int mapArr[20][20];
int markerX = 0;
int markerY = 0;
int x = 0;
int y = 0;
int mapTracker = 0;
for (markerX = 0; markerX < 20; markerX++)
{
for (markerY = 0; markerY < 20; markerY++)
{
mapArr[markerX][markerY] = 2;
}
}
mapArr[posx][posy] = 1;
for (y = 0; y < 20; y++)
{
for (x = 0; x < 20; x++)
{
if (mapArr[x][y] == 2)
{
cout << ".";
mapTracker++;
}
if (mapArr[x][y] == 1)
{
cout << "@";
mapTracker++;
}
if (mapTracker == 20)
{
cout << "\n";
mapTracker = 0;
}
}
}
}
void playerInput()
{
// Q - NW
if (pInput == 113)
{
posx--;
posy--;
}
// W - N
if (pInput == 119)
{
posy--;
}
// E - NE
if (pInput == 101)
{
posx++;
posy--;
}
// A - W
if (pInput == 97)
{
posx--;
}
// S - Hold
if (pInput == 115)
{
}
// D - E
if (pInput == 100)
{
posx++;
}
// Z - SW
if (pInput == 122)
{
posx--;
posy++;
}
// X - S
if (pInput == 120)
{
//posx++;
posy++;
}
// C - SE
if (pInput == 99)
{
posx++;
posy++;
}
if (posx < 0)
{posx = 0;}
if (posy < 0)
{posy = 0;}
if (posx > 19)
{posx = 19;}
if (posy > 19)
{posy = 19;}
}
Your first problem was that your event for X was
1 2
posx++;
posy++;
Hence, the diagonals. Also, you were setting a newline one spot too early and it was drawing an off grid.
One other thing, your x and y values for drawing the map were backwards and you didn't have any checks for if you tried to go outside of the map. I hope you don't mind that I fixed them lol
And could I ask you one more thing? I'm confused about what was changed. How did the newline affect it? Did it just draw one more period, or did it offset everything? Also, when you say you switched the x and y values, do you mean when it says:
1 2 3 4 5 6 7 8 9 10 11
if (mapArr[x][y] == 2)
{
cout << ".";
mapTracker++;
}
if (mapArr[x][y] == 1)
{
cout << "@";
mapTracker++;
}
No problem :) So essentially you're drawing a 20, 20 grid, right? Well, rather than have it start a newline at the 20th spot, you had it do the newline at the 19th spot.
I know you were meaning to set it to 19 since an array starts at 0 but since your mapTracker was updating before checking for a newline, it was moving on a 1 - 20 slot rather than 0 - 19. So what was supposed to be [0][1] turned into [19][1], visually speaking.
You got it on the flipped x and y part.
1 2 3 4 5 6 7 8 9 10 11
//Your original code
if (mapArr[y][x] == 2)
{
cout << ".";
mapTracker++;
}
if (mapArr[y][x] == 1)
{
cout << "@";
mapTracker++;
}
So it kind of rotated everything. Did that kind of clear it up? :)
I'm just going to say this before anyone else does: You're going to get shot for the system ("cls");
Heh.
Don't use system(), it's non-portable and a security risk, among other things. You also should avoid conio.h; it's old and non-standard, and thus also non-portable. Here are a couple links if you are interested:
Oh, I see now. I thought something was off with [x][y], but I didn't know about it messing up when drawing it. That explains why the @ would randomly move to the side when going down.
About using "system("cls")", I heard about how bad it is. I made an attempt at using ncurses, but my lack of anything remotely like understanding foiled it. So I figured "Eh, it's my first real project. Whatever." I'll try using SFML soon, but for now I just need to feel like I'm making progress on anything.