Problems with input

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:

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <cstdlib>
#include <conio.h>

using namespace 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?
closed account (1yvXoG1T)
Here ya go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <cstdlib>
#include <conio.h>

using namespace 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
Last edited on
HOORAY! Thanks Neurotrace!

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++;
            }


Could you please say what lines you changed?
I'm just going to say this before anyone else does: You're going to get shot for the system ("cls");
closed account (1yvXoG1T)
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.
1
2
3
4
5
if (mapTracker == 19)
{
     cout << "\n";
     mapTracker = 0;
}

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? :)
Last edited on
sargon92 wrote:
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:

http://www.cplusplus.com/forum/articles/28558/
http://www.cplusplus.com/forum/articles/11153/
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.
Topic archived. No new replies allowed.