Game Crashes, Cannot Loop within a "GetAsyncKeyState()" function

closed account (3voN6Up4)
Hello all, I am trying to make my own version of the "Space Invaders" game. I have all of the keystrokes in my code working except for the space bar one which shoots the '|' character up the Y axis of the two dimensional array which is the map.

I can't really figure out how I should have the '|' to move up the Y axis and delete itself when moving upwards. Also, is it possible to auto detect the dimensions of which the 'X' is in the array? A for loop that looks for it and names the two array elements as variables in the x and y integer variables up top. The loop would find the position if I changed where the 'X' was in the code it would still know where it was and can work from there.

Any advice, tips, on further game development would be awesome! My code snippet is below:

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
#include <iostream>
#include <stdio.h>
#include "windows.h"
using namespace std;
const int mapx = 40;
const int mapy = 20;
char map[mapy][mapx] ={"#######################################",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                  X                  #",
                       "#                                     #",
                       "#######################################"};
int x = 19; //starting x position of the character
int y = 16; //starting y position of the character
bool game_running = true;
char KeyCommandsFunction()
{
    if (GetAsyncKeyState(VK_DOWN))
        {
            char y2 = y + 1;
            if (map[y2][x] == ' ') //If the space is empty in the array
            {
                map[y][x] = ' ';
                y++;
                map [y][x] = 'X';
            }
        }
        if (GetAsyncKeyState(VK_UP))
        {
            int y2 = y - 1;
            if (map[y2][x] ==  ' ') //If the space is empty in the array
            {
                map[y][x] = ' ';
                y--;
                map [y][x] = 'X';
            }
        }
        if (GetAsyncKeyState(VK_LEFT))
        {
            int x2 = x - 1;
            if (map[y][x2] ==  ' ') //If the space is empty in the array
            {
                map[y][x] = ' ';
                x--;
                map [y][x] = 'X';
            }
        }
        if (GetAsyncKeyState(VK_RIGHT))
        {
            int x2 = x + 1;
            if (map[y][x2] ==  ' ') //If the space is empty in the array
            {
                map[y][x] = ' ';
                x++;
                map [y][x] = 'X';
            }
        }
        if (GetAsyncKeyState(VK_SPACE))
            {
                for (int i = 0; i < 20; i++)
                {
                if (map[i][x] == ' ') //If the space is empty in the array
                    {
                        map[y][x] = ' ';
                        y--;
                        map [y][x] = '|';
                    }
                }
            }
        if (GetAsyncKeyState(VK_ESCAPE))
        {
            game_running = false;
        }
}
int GameFunction()
{
    while (game_running == true) //This loop is the game.
    {
        system("cls");
        for(int display = 0; display < 20; display++)
        {
            cout << map[display] << endl;
        }
        system("pause > null"); //display nothing when it's paused.
        KeyCommandsFunction();
    }
}
int main()
{
GameFunction();
KeyCommandsFunction();
system("cls");
cout << "Game Over." << endl;
}
What you need is a second x/y pair for the projectile.

Also, is it possible to auto detect the dimensions of which the 'X' is in the array?
Sure, you can have two nested loop to find the 'x', but it is actually a waste of time. Instead organize your code:
1
2
3
4
5
6
7
8
struct pos
{
  int x;
  int y;
};

pos char_pos{19, 16};
pos projectile_pos{-1, -1}; // -1 -> does not occur 


How does the projectile move. As long as you hold the space bar or once you press the space bar and after that independent?
closed account (3voN6Up4)
Thank you, so what you're saying is my code should look like this inside, and I should have a structure containing dimensions and what not. Also, could you explain to me what line 7 and 8 do in your code snippet? I'm not that great with classes/structures just yet. So my code should look more like this, right?

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
#include <iostream>
#include <stdio.h>
#include "windows.h"
using namespace std;
const int mapx = 40;
const int mapy = 20;
char map[mapy][mapx] ={"#######################################",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                                     #",
                       "#                  X                  #",
                       "#                                     #",
                       "#######################################"};
struct pos
{
    int x;
    int y;
}
pos char_pos{19,16};
pos projectile_pos{-1,-1}; // Does not occur
bool game_running = true;
char KeyCommandsFunction()
{
    if (GetAsyncKeyState(VK_DOWN))
        {
            char y2 = y + 1;
            if (map[y2][x] == ' ') //If the space is empty in the array
            {
                map[y][x] = ' ';
                y++;
                map [y][x] = 'X';
            }
        }
        if (GetAsyncKeyState(VK_UP))
        {
            int y2 = y - 1;
            if (map[y2][x] ==  ' ') //If the space is empty in the array
            {
                map[y][x] = ' ';
                y--;
                map [y][x] = 'X';
            }
        }
        if (GetAsyncKeyState(VK_LEFT))
        {
            int x2 = x - 1;
            if (map[y][x2] ==  ' ') //If the space is empty in the array
            {
                map[y][x] = ' ';
                x--;
                map [y][x] = 'X';
            }
        }
        if (GetAsyncKeyState(VK_RIGHT))
        {
            int x2 = x + 1;
            if (map[y][x2] ==  ' ') //If the space is empty in the array
            {
                map[y][x] = ' ';
                x++;
                map [y][x] = 'X';
            }
        }
        if (GetAsyncKeyState(VK_SPACE))
            {
                for (int i = 0; i < 20; i++)
                {
                if (map[i][x] == ' ') //If the space is empty in the array
                    {
                        map[y][x] = ' ';
                        y--;
                        map [y][x] = '|';
                    }
                }
            }
        if (GetAsyncKeyState(VK_ESCAPE))
        {
            game_running = false;
        }
}
int GameFunction()
{
    while (game_running == true) //This loop is the game.
    {
        system("cls");
        for(int display = 0; display < 20; display++)
        {
            cout << map[display] << endl;
        }
        system("pause > null"); //display nothing when it's paused.
        KeyCommandsFunction();
    }
}
int main()
{
GameFunction();
KeyCommandsFunction();
system("cls");
cout << "Game Over." << endl;
}


I'm unsure of how it will move based on speed, I should be able to update the speed and stuff like that with the sleep function. Basically you press the spacebar, and it will move up 1, move up 1, move up 1, and so on until it hits the border. So independent
Last edited on
Also, could you explain to me what line 7 and 8 do in your code snippet?
It creates the varibales and initialize them much like int x = 19

For the use of struct see:

http://www.cplusplus.com/doc/tutorial/structures/
http://www.learncpp.com/cpp-tutorial/47-structs/

Alternatively you may write:
1
2
3
4
5
int x = 19; //starting x position of the character
int y = 16; //starting y position of the character

projectile_x = -1; // -1 means the projectile | is not visible
projectile_y = -1; // -1 means the projectile | is not visible 

projectile_sleep = 100; // speed: 100 ms
projectile_max_speed = 5; // movement every 500 ms
projectile_speed_index = 0;

What you can do is the following:
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
        if (GetAsyncKeyState(VK_SPACE))
            {
                projectile_x = x;
                projectile_y = y - 1;
                if (map[projectile_y][projectile_x] == ' ') //If the space is empty in the array
                        map[projectile_y][projectile_x] = '|';
                else
                {
                        projectile_x = -1;
                        projectile_y = -1;
                }
            }
...
int GameFunction()
{
...
        for(int display = 0; display < 20; display++)
        {
            cout << map[display] << endl;
        }
        if((projectile_x < 0) || (projectile_y < 0)) // no projectile: no special handling
        {
                system("pause > null"); //display nothing when it's paused.
                KeyCommandsFunction();
        }
        else // control of the projectile:
        {
                Sleep(projectile_sleep); // This means that the user needs to hit the key longer than 100 ms

projectile_speed_index++;
if(projectile_speed_index >= projectile_max_speed)
{
projectile_speed_index = 0;

                if (map[projectile_y - 1][projectile_x] == ' ') //If the space is empty in the array
                {
                        map[projectile_y][projectile_x] = ' ';
                        projectile_y--;
                        map [projectile_y][projectile_x] = '|';
                }
                else
                {
                        map[projectile_y][projectile_x] == ' ';
                        projectile_x = -1;
                        projectile_y = -1;
                }
}
                KeyCommandsFunction();
        }
...
}
closed account (3voN6Up4)
Damn, well I was off.

So struct was just organization of the code,
and in the GameFunction() Function,
1
2
3
4
5
6
 
if ((projectilve_x < 0) || (Projectile_y < 0)) //No projectile: no special handling
        {
            system("pause > null"); //display nothing when it's paused.
            KeyCommandsFunction();
        }

makes it so like if the projectile is not visible it'll just keep doing what it's doing?
and else:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
else // control of the projectile:
        {
                Sleep(projectile_sleep); // This means that the user needs to hit the key longer than 100 ms
                projectile_speed_index++;
                if(projectile_speed_index >= projectile_max_speed)
                {
                    projectile_speed_index = 0;
                }
                if (map[projectile_y - 1][projectile_x] == ' ') //If the space is empty in the array
                {
                    map[projectile_y][projectile_x] = ' ';
                    projectile_y--;
                    map [projectile_y][projectile_x] = '|';
                }
                else
                {
                    map[projectile_y][projectile_x] == ' ';
                    projectile_x = -1;
                    projectile_y = -1;
                }
                KeyCommandsFunction();
        }


Anyways, I've revised what I have and looked at yours.
I have also added in two structures so it'll look different.
Here is what I have:
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
#include <iostream>
#include <stdio.h>
#include "windows.h"
using namespace std;
const int mapx = 40;
const int mapy = 20;
char map[mapy][mapx] =
{
"#######################################",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                  X                  #",
"#                                     #",
"#######################################"
};
struct Positions
{
    int x = 19;
    int y = 16;
    int projectile_x = -1; // -1 Means the projectile '|' is not visible.
    int projectile_y = -1; // -1 Means the projectile '|' is not visible.
};
struct Movements
{
    int projectile_sleep = 100;   //Speed is 100ms
    int projectile_max_speed = 5; //Movement every 500ms
    int projectile_speed_index = 0;
};
Positions P; //P variable with modifiable variables inside of it from the Positions structure.
Movements M; //M variable with modifiable variables inside of it from the Movements structure.
bool game_running = true;
char KeyCommandsFunction()
{
    if (GetAsyncKeyState(VK_DOWN))
        {
            char y2 = P.y + 1;
            if (map[y2][P.x] == ' ') //If the space is emptP.y in the arraP.y
            {
                map[P.y][P.x] = ' ';
                P.y++;
                map [P.y][P.x] = 'X';
            }
        }
        if (GetAsyncKeyState(VK_UP))
        {
            int y2 = P.y - 1;
            if (map[y2][P.x] ==  ' ') //If the space is emptP.y in the arraP.y
            {
                map[P.y][P.x] = ' ';
                P.y--;
                map [P.y][P.x] = 'X';
            }
        }
        if (GetAsyncKeyState(VK_LEFT))
        {
            int x2 = P.x - 1;
            if (map[P.y][x2] ==  ' ') //If the space is empty in the array
            {
                map[P.y][P.x] = ' ';
                P.x--;
                map [P.y][P.x] = 'X';
            }
        }
        if (GetAsyncKeyState(VK_RIGHT))
        {
            int x2 = P.x + 1;
            if (map[P.y][x2] ==  ' ') //If the space is empty in the array
            {
                map[P.y][P.x] = ' ';
                P.x++;
                map [P.y][P.x] = 'X';
            }
        }
        if (GetAsyncKeyState(VK_SPACE))
            {
                P.projectile_x = P.x;
                P.projectile_y = P.y;
                if (map[P.projectile_y][P.projectile_x] == ' ') //If the space is empty in the array
                {
                    map[P.projectile_y][P.projectile_x] = '|';
                }
                else
                {
                    P.projectile_x = -1;
                    P.projectile_y = -1;
                }
            }
        if (GetAsyncKeyState(VK_ESCAPE))
        {
            game_running = false;
        }
}
int GameFunction()
{
    while (game_running == true) //This loop is the game.
    {
        system("cls");
        for(int display = 0; display < 20; display++)
        {
            cout << map[display] << endl;
        }
        if ((P.projectile_x < 0) || (P.projectile_y < 0)) //No projectile: no special handling
        {
            system("pause > null"); //display nothing when it's paused.
            KeyCommandsFunction();
        }
        else // control of the projectile:
        {
                Sleep(M.projectile_sleep); // This means that the user needs to hit the key longer than 100 ms
                M.projectile_speed_index++;
                if(M.projectile_speed_index >= M.projectile_max_speed)
                {
                    M.projectile_speed_index = 0;
                }
                if (map[P.projectile_y - 1][P.projectile_x] == ' ') //If the space is empty in the array
                {
                    map[P.projectile_y][P.projectile_x] = ' ';
                    P.projectile_y--;
                    map [P.projectile_y][P.projectile_x] = '|';
                }
                else
                {
                    map[P.projectile_y][P.projectile_x] == ' ';
                    P.projectile_x = -1;
                    P.projectile_y = -1;
                }
                KeyCommandsFunction();
        }
    }
}
int main()
{
GameFunction();
KeyCommandsFunction();
system("cls");
cout << "Game Over." << endl;
}


It seems to be a lot slower than it originally was in terms of moving the X around, and the projectile doesn't show up
Last edited on
1
2
3
4
5
6
7
8
9
10
11
                P.projectile_x = P.x; // This is the position of 'X'
                P.projectile_y = P.y; // This is the position of 'X' (Note: - 1 is required)
                if (map[P.projectile_y][P.projectile_x] == ' ') // Hence this will not be true
                {
                    map[P.projectile_y][P.projectile_x] = '|';
                }
                else // Therefore the projectile is 'removed'
                {
                    P.projectile_x = -1;
                    P.projectile_y = -1;
                }


It seems to be a lot slower than it originally was
I don't see why since the projetile is not involved. Maybe this is a good time to use the debugger in order to find out what is going on.
closed account (3voN6Up4)
The projectile doesn't appear, and it was my computer that was slow not the program. It runs fine now. How do you use the debugger? I've never used it before. I have CodeBlocks
The projectile doesn't appear
Again: It does not appear because you try to set it to the 'X' position. Line 91 prevents that.


How do you use the debugger?
There is a debug menu. See:
http://wiki.codeblocks.org/index.php?title=Debugging_with_Code::Blocks
Topic archived. No new replies allowed.