Interactive Console Application Runtime Bug [C++]

Hi.

I am making a "blueprint" for a survival game similar to Cataclysm:DDA.

When my character 'W' is moving vertically, I can press left or right to move horizontally instead. But when I am moving horizontally I can't counteract and move vertically.

I know this is happening because it looks for left and right buttons first. But I don't know how to fix it.

Thanks in advance. And please tell me if this post doesn't belong here, so I can remove it.

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
141
142
143
144
145
146
147
148
#include <iostream>
#include "windows.h"

using namespace std;

const int gameSquare = 20; // THIS IS GLOBAL!

char gameMap1[gameSquare][gameSquare] = { // THIS TOO!

    "###################",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#     ### ###     #",
    "#     #     #     #",
    "#     #######     #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#        W        #",
    "#                 #",
    "#                 #",
    "#                 #",
    "###################",

};

void clearScreen() // TAKEN FROM INTERNET
{
    HANDLE hOut;
    COORD Position;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    Position.X = 0;
    Position.Y = 0;
    SetConsoleCursorPosition(hOut, Position);
}

int main()
{
    int gameSpeed = 50;
    bool endGame = false;

    while(endGame == false)
    {

        Sleep(gameSpeed);

        clearScreen();

        for(int i = 0; i < gameSquare; i++)
        {
            switch(i)
            {
                default:
                    cout << gameMap1[i] << endl;
                break;
            }
        }

        for(int y = 0; y < gameSquare; y++)
        {
            for(int x = 0; x < gameSquare; x++)
            {
                switch(gameMap1[y][x])
                {
                    case 'W':

                        if(GetAsyncKeyState(VK_ESCAPE) != 0)
                        {
                            return 0;
                        }

                        if(GetAsyncKeyState(VK_LEFT) != 0)
                        {

                            int newX = x - 1;

                            switch(gameMap1[y][newX])
                            {
                                case ' ':
                                    gameMap1[y][x] = ' ';
                                    x--;
                                    gameMap1[y][newX] = 'W';
                                break;
                            }
                            break;
                        }

                        if(GetAsyncKeyState(VK_RIGHT) != 0)
                        {

                            int newX = x + 1;

                            switch(gameMap1[y][newX])
                            {
                                case ' ':
                                    gameMap1[y][x] = ' ';
                                    x++;
                                    gameMap1[y][newX] = 'W';
                                break;
                            }
                            break;
                        }

                        if(GetAsyncKeyState(VK_UP) != 0)
                        {

                            int newY = y - 1;

                            switch(gameMap1[newY][x])
                            {
                                case ' ':
                                    gameMap1[y][x] = ' ';
                                    y--;
                                    gameMap1[newY][x] = 'W';
                                break;
                            }
                            break;
                        }

                        if(GetAsyncKeyState(VK_DOWN) != 0)
                        {

                            int newY = y + 1;

                            switch(gameMap1[newY][x])
                            {
                                case ' ':
                                    gameMap1[y][x] = ' ';
                                    y++;
                                    gameMap1[newY][x] = 'W';
                                break;
                            }
                            break;
                        }
                    break;
                }
            }
        }
    }
}
Last edited on
Topic archived. No new replies allowed.