Placing Explosives (using GetAsyncKeyState)

Hey everyone. I was given an assignment to create a game where a player moves around a 'construction site' with some obstacles on it. The player will need to place three explosive charges within the construction site and the game ends when the three explosive devices have been placed or the player was caught within an
explosion. I'm unable to find a way to place the explosive devices. Please help. Thanks.
(Also, would it be possible to place an explosive device to get rid of an obstacle?)

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
 #include <iostream>
#include <windows.h>
using namespace std;


const int ROWS = 10;
const int COLS = 15;
	
	char aryGameMap[ROWS][COLS] ={
	{'.','.','.','.','.','.','.','.','.','.','.','.','.','.',},
	{'.','@' ,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','.'},
	{'.',' ' ,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','.'},
	{'.',' ' ,' ',' ',' ',' ',' ','O',' ',' ',' ',' ',' ','.'},
	{'.',' ' ,'O',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','.'},
	{'.',' ' ,' ',' ',' ',' ',' ',' ',' ',' ','O',' ',' ','.'},
	{'.','O' ,' ',' ','O',' ',' ',' ',' ',' ',' ',' ',' ','.'},
	{'.',' ' ,' ','O',' ',' ',' ',' ',' ',' ',' ',' ',' ','.'},
	{'.','.','.','.','.','.','.','.','.','.','.','.','.','.',}
	
	};
	
	
	bool blnGameIsRunning = true;
	int intRow = 1;
	int  intCol = 1;
	



int main()
{
	
	
	while(blnGameIsRunning =true )
	{
		
		system("cls");
		for(int row = 0; row < ROWS; row++)
		{
			for (int col = 0; col < COLS; col++)
			{
				cout << aryGameMap[row][col];
			}
			
			cout << endl;
		}	
		
	
	system("pause > nul");
	
	if(GetAsyncKeyState(VK_DOWN))
	{
	int intNewRow = intRow +1;
			if(aryGameMap[intNewRow][intCol] == ' ')
			{
				aryGameMap[intRow][intCol] = ' ';
				intRow++;
				aryGameMap[intRow][intCol] = '@';
			}
	}
	
	else if(GetAsyncKeyState(VK_UP))
	{
		int intNewRow = intRow - 1;
			if(aryGameMap[intNewRow][intCol] == ' ')
			{
				aryGameMap[intRow][intCol] = ' ';
				intRow--;
				aryGameMap[intRow][intCol] = '@';
			}
	}
	
	else if(GetAsyncKeyState(VK_RIGHT))
	{
		int intNewCol = intCol + 1;
			if(aryGameMap[intRow][intNewCol] == ' ')
			{
				aryGameMap[intRow][intCol] = ' ';
				intCol++;
				aryGameMap[intRow][intCol] = '@';
			}
	}
	
	else if(GetAsyncKeyState(VK_LEFT))
	{
		int intNewCol = intCol - 1;
			if(aryGameMap[intRow][intNewCol] == ' ')
			{
				aryGameMap[intRow][intCol] = ' ';
				intCol--;
				aryGameMap[intRow][intCol] = '@';
			}
	}
	
	else if(GetAsyncKeyState(VK_ESCAPE))
	{
		blnGameIsRunning = false;
		break;
	}
	
	
	
	
	}
	
	cout << "Game over!!";
	

		return 0;
	
}
Hello NeonNovel,

Based on what you have lines 6 - 25 will work just as well inside main.

Line 34 I think you meant while(blnGameIsRunning == true ). What you have is an assignment which is the same as saying while(true). Line 97 has no real use because line 98 will break out of the while loop to end the program.

The easy part is something like else if(GetAsyncKeyState(VK_ENTER)). The hard part is everything that need to happen within the braces. You will need to do things like; place the bomb, move the "@" out of the way to an empty space, keep track of where the bomb is placed, figure out how much space to put between the "@" and the bomb before it goes off, how much area the bomb will affect, if there are any obstacles in the area and if so remove them, keep track of the number of bombs places and exploded, the other "else if" statements will also have to deal with the bombs.

Just some initial thoughts. See what you can come up with.

Hope that helps,

Andy
Topic archived. No new replies allowed.