what's wrong with my program

hey guys, so I wrote a console game (I know, writing console game is a waste of time)...in order for the movement I use getch() so, I edit it and change it into GetAsyncKeyState()
but, when I pressed right, the game exit
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <iostream>
#include <string>
#include <windows.h>
#include <conio.h>

#define height 10
#define width 60
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80

using namespace std;

enum class condition {win, lose};

void clrscr()
{
    HANDLE                     hStdOut;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD                      count;
    DWORD                      cellCount;
    COORD                      homeCoords = { 0, 0 };

    hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
    if (hStdOut == INVALID_HANDLE_VALUE) return;

    if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
    cellCount = csbi.dwSize.X *csbi.dwSize.Y;

    if (!FillConsoleOutputCharacter(hStdOut,(TCHAR) ' ',cellCount,homeCoords,&count)) return;

    if (!FillConsoleOutputAttribute(hStdOut,csbi.wAttributes,cellCount,homeCoords,&count)) return;
    SetConsoleCursorPosition( hStdOut, homeCoords );
}

void gotoxy(short x, short y)
{
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos = {x, y};
    SetConsoleCursorPosition(h, pos);
}

void FastPrint(const CHAR_INFO* c, int X1, int Y1, int X2, int Y2)
{
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD CharBuffer = {X2, Y2};
    COORD CharPos = {X1, Y1};
    SMALL_RECT WriteArea = {X1, Y1, X2, Y2};
    WriteConsoleOutputA(h, c, CharBuffer, CharPos, &WriteArea);
}

void FastPrint (const CHAR_INFO* c, int x, int y)
{
    HANDLE h  = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD CharBuffer = {x, y};
    COORD CharPos = {0, 0};
    SMALL_RECT WriteArea = {0, 0, x, y};
    WriteConsoleOutputA(h, c, CharBuffer, CharPos, &WriteArea);
}

void FastPrint(CHAR_INFO c[], int X1, int Y1, int X2, int Y2)
{
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD CharBuffer = {X2, Y2};
    COORD CharPos = {X1, Y1};
    SMALL_RECT WriteArea = {X1, Y1, X2, Y2};
    WriteConsoleOutputA(h, c, CharBuffer, CharPos, &WriteArea);
}

void FastPrint (CHAR_INFO c[], int x, int y)
{
    HANDLE h  = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD CharBuffer = {x, y};
    COORD CharPos = {0, 0};
    SMALL_RECT WriteArea = {0, 0, x, y};
    WriteConsoleOutputA(h, c, CharBuffer, CharPos, &WriteArea);
}

namespace game
{
    bool runmenu = true;
    int difficulty = 30;
    bool running = true;
    CHAR_INFO layout[width * height];
}

namespace symbol
{
    const char ground = '.';
    const char wall = 'X';
    const char player = '@';
    const char spike = '|';
}

struct coord
{
    int x;
    int y;
}player, other, spike[width], arrow;

class primary
{
private :
    int i2 = 0;
    condition WinLose;
public :
    void menu();
    void run_menu();
    void init_layout();
    void update();
    void addstuff();
    void run();
    void display();
};

class Player
{
private :
    char key;
    int life = 6;
    int OldLife = life;
    bool wait;
public :
    int lifeA = life;
    void action_menu();
    void spawn();
    void action();
    bool check();
    void declife();
    void display();
};

class Spike
{
private :
    int wait = 0;
public :
    void spawn(int x, int i);
    void movement(int i);
};

int main()
{
    primary p;
    p.run_menu();
    clrscr();
    p.run();
    clrscr();
    p.display();
    cin.ignore();
}

void primary::menu()
{
    system("cls");
    gotoxy(35,9);
    cout << "play";
    gotoxy(35, 10);
    if (game::difficulty == 0) cout << "insane";
    else if (game::difficulty == 10) cout << "extreme hard";
    else if (game::difficulty == 20) cout << "hard";
    else if (game::difficulty == 30) cout << "normal";
    else if (game::difficulty == 40) cout << "easy";
    else if (game::difficulty == 50) cout << "extreme easy";
    else if (game::difficulty == 60) cout << "baby";
    gotoxy(35, 11);
    cout << "exit";
    gotoxy(34, arrow.y);
    cout << ">";
}

void primary::run_menu()
{
    Player p;
    arrow.y = 9;
    while (game::runmenu)
    {
        menu();
        p.action_menu();
    }
}

void primary::init_layout()
{
    for (other.y = 0; other.y < height; ++other.y)
        for (other.x = 0; other.x < width; ++other.x)
        {
            game::layout[other.x + width * other.y].Char.AsciiChar = symbol::wall;
            game::layout[other.x + width * other.y].Attributes = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
        }
    for (other.y = 1; other.y < height - 1; ++other.y)
        for (other.x = 1; other.x < width - 1; ++other.x)
        {
            game::layout[other.x + width * other.y].Char.AsciiChar = symbol::ground;
            game::layout[other.x + width * other.y].Attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
        }
}

void primary::update()
{
    FastPrint(game::layout, width, height);
}

void primary::addstuff()
{
    int i = 0, x;
    Spike s;
    Player p;
    p.spawn();
    for (x = 0; x < width; ++x)
    {
        if (x < 10)
        {
            if (x == 2) s.spawn(x, i);
            else if (x == 3) s.spawn(x, i);
            else if (x == 5) s.spawn(x, i);
            else if (x == 7) s.spawn(x, i);
            else continue;
            ++i;
        }
        else
        {
            if ((x % 1 == 0) && (x % 2 != 0) && (x % 3 != 0) && (x % 5 != 0)) s.spawn(x, i);
            else continue;
            ++i;
        }
    }
    i2 = i;
}

void primary::run()
{
    int i;
    Spike s;
    Player p;
    init_layout();
    addstuff();
    while (game::running == true)
    {
        if (kbhit())
        {
            p.action();
        }
        for (i = 0; i < i2; ++i)
        {
            if (kbhit())
            {
                p.action();
            }
            s.movement(i);
        }
        if (p.check()) p.declife();
        p.display();
        update();
        if (p.lifeA == 0)
        {
            WinLose = condition::lose;
            break;
        }
        if (player.x == width - 2)
        {
            WinLose = condition::win;
            break;
        }
        gotoxy (1, height + 1);
    }
}
here's the other half
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

void primary::display()
{
    clrscr();
    Player p;
    gotoxy(35, 10);
    if (WinLose == condition::lose)
    {
        if (game::difficulty == 0) cout << "there's no way you can win";
        else if (game::difficulty == 10) cout << "how's the extreme ?";
        else if (game::difficulty == 20) cout << "is it really that hard ?";
        else if (game::difficulty == 30) cout << "what ? how did you lose in normal ?";
        else if (game::difficulty == 40) cout << "c'mon now, this is easy";
        else if (game::difficulty == 50) cout << "...just how weak are you ?";
        else if (game::difficulty == 60) cout << "pathetic";
    }
    else if (WinLose == condition::win)
    {
        if (game::difficulty == 0) cout << "h-how did you";
        else if (game::difficulty == 10) cout << "just defeated extreme hard ? how about insane ?";
        else if (game::difficulty == 20) cout << "so, is it hard ?";
        else if (game::difficulty == 30) cout << "what an ordinary person";
        else if (game::difficulty == 40) cout << "maybe you should try something harder";
        else if (game::difficulty == 50) cout << "are you a baby";
        else if (game::difficulty == 60) cout << "...not even an achievment";
    }
    gotoxy(35, 20);
    cout << "press enter to exit";
    cin.ignore();
}

void Player::action_menu()
{
    if (GetAsyncKeyState(VK_UP))
    {
        if (arrow.y == 9);
        else --arrow.y;
    }
    else if (GetAsyncKeyState(VK_DOWN))
    {
        if (arrow.y == 11);
        else ++arrow.y;
    }
    else if (GetAsyncKeyState(VK_RETURN))
    {
        if (arrow.y == 9)
        {
            game::runmenu = false;
        }
        else if (arrow.y == 11) exit(1);
    }
    else if (GetAsyncKeyState(VK_LEFT))
    {
        if (arrow.y != 10);
        else
        {
            switch (game::difficulty)
            {
                case 0 : game::difficulty = 10; break;
                case 10 : game::difficulty = 20; break;
                case 20 : game::difficulty = 30; break;
                case 30 : game::difficulty = 40; break;
                case 40 : game::difficulty = 50; break;
                case 50 : game::difficulty = 60; break;
                case 60 : game::difficulty = 0; break;
            }
        }
    }
    else if (GetAsyncKeyState(VK_RIGHT))
    {
        if (arrow.y != 10);
        else
        {
            switch(game::difficulty)
            {
                case 0 : game::difficulty = 60; break;
                case 10 : game::difficulty = 0; break;
                case 20 : game::difficulty = 10; break;
                case 30 : game::difficulty = 20; break;
                case 40 : game::difficulty = 30; break;
                case 50 : game::difficulty = 40; break;
                case 60 : game::difficulty = 50; break;
            }
        }
    }
}

void Player::spawn()
{
    player.x = 1;
    player.y = (height / 2) - 1;
    game::layout[player.x + width * player.y].Char.AsciiChar = symbol::player;
    game::layout[player.x + width * player.y].Attributes = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
}

void Player::action()
{
    if (GetAsyncKeyState(VK_LEFT))
    {
         if (game::layout[(player.x - 1) + width * player.y].Char.AsciiChar != symbol::ground);
         else
         {
             game::layout[player.x + width * player.y].Char.AsciiChar = symbol::ground;
             game::layout[player.x + width * player.y].Attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
             --player.x;
             game::layout[player.x + width * player.y].Char.AsciiChar = symbol::player;
             game::layout[player.x + width * player.y].Attributes = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
         }
    }
    else if (GetAsyncKeyState(VK_RIGHT))
    {
        if (game::layout[(player.x + 1) + width * player.y].Char.AsciiChar != symbol::ground);
        else
        {
            game::layout[player.x + width * player.y].Char.AsciiChar = symbol::ground;
            game::layout[player.x + width * player.y].Attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
            ++player.x;
            game::layout[player.x + width * player.y].Char.AsciiChar = symbol::player;
            game::layout[player.x + width * player.y].Attributes = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
        }
    }
    wait = false;
}

bool Player::check()
{
    if (game::layout[player.x + width * player.y].Char.AsciiChar != symbol::player) return true;
    else return false;
}

void Player::declife()
{
    if (wait == false)
    {
        --life;
        wait = true;
    }
}

void Player::display()
{
    gotoxy(1, height);
    int i;
    if (life != OldLife) clrscr();
    for (i = 0; i < life; ++i) cout << "|";
    OldLife = life;
    lifeA = life;
}

void Spike::spawn(int x, int i)
{
    spike[i].x = x;
    spike[i].y = 1;
    game::layout[spike[i].x + width * spike[i].y].Char.AsciiChar = symbol::spike;
    game::layout[spike[i].x + width * spike[i].y].Attributes = FOREGROUND_RED | FOREGROUND_INTENSITY;
}

void Spike::movement(int i)
{
    if (game::layout[spike[i].x + width * (spike[i].y + 1)].Char.AsciiChar == symbol::wall)
    {
        game::layout[spike[i].x + width * spike[i].y].Char.AsciiChar = symbol::ground;
        game::layout[spike[i].x + width * spike[i].y].Attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
        spike[i].y = 1;
    }
    else
    {
        if (wait == game::difficulty)
        {
            game::layout[spike[i].x + width * spike[i].y].Char.AsciiChar = symbol::ground;
            game::layout[spike[i].x + width * spike[i].y].Attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
            ++spike[i].y;
            game::layout[spike[i].x + width * spike[i].y].Char.AsciiChar = symbol::spike;
            game::layout[spike[i].x + width * spike[i].y].Attributes = FOREGROUND_RED | FOREGROUND_INTENSITY;
            wait = 0;
        }
        else ++wait;
    }
}
nvm, solved it
Topic archived. No new replies allowed.