character movement in a dungeon crawler

Hello everyone. I'm having some trouble with "identifier is undefined" in my BuildLevel function. I can get rid of that issue by copying the first four lines of my main function into BuildLevel, but then I have the issue of the player's position not changing. Could anyone help me with this?

My current goal is to have WASD move the player around the dungeon crawler.

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 <ctime>
#include <conio.h>
#include <chrono>
#include <windows.h>

using namespace std;
using namespace std::chrono;

#define WIDTH    30
#define HEIGHT   20

typedef unsigned char uchar;

uchar dungeon[HEIGHT][WIDTH] =
{
	{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
	{ 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1 },
	{ 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1 },
	{ 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1 },
	{ 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1 },
	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 0, 0, 1, 1, 1, 1 },
	{ 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
	{ 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
	{ 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
	{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};

class Player
{
public:
	uchar health = 100;
	uchar damage = rand() % 25;
	uchar posx = 3;
	uchar posy = 17;
};

class slimeOne
{
public:
	uchar health = 20;
	uchar damage = rand() % 5;
	uchar posx = 2;
	uchar posy = 2;
};

class slimeTwo
{
public:
	uchar health = 60;
	uchar damage = rand() % 15;
	uchar posx = 13;
	uchar posy = 2;
};

class slimeThree
{
public:
	uchar health = 100;
	uchar damage = rand() % 25;
	uchar posx = 16;
	uchar posy = 17;
};

void BuildLevel()
{
	system("cls");
	system("COLOR 0E");

	for (int y = 0; y < HEIGHT; y++)
	{
		cout << endl;
		for (int x = 0; x < WIDTH; x++)
		{
			if (x == hero.posx && y == hero.posy)
			{
				cout << char(2); 
			}
			else if (x == patheticSlime.posx && y == patheticSlime.posy)
			{
				cout << char(235);
			}
			else if (x == basicSlime.posx && y == basicSlime.posy)
			{
				cout << char(153);
			}
			else if (x == monstrousSlime.posx && y == monstrousSlime.posy)
			{
				cout << char(234);
			}
			else if (dungeon[y][x] == 0)
			{
				cout << ' ';
			}
			else if (dungeon[y][x] == 1)
			{
				cout << char(178);
			}
		}
	}
	cout << endl << endl;
	cout << (int)hero.posy; //test value increase
}

int main()
{
	Player     hero;
	slimeOne   patheticSlime;
	slimeTwo   basicSlime;
	slimeThree monstrousSlime;

	BuildLevel();

	while (1)
	{
		if (GetAsyncKeyState('W') || GetAsyncKeyState('w'))
		{
			hero.posy++;
			BuildLevel();
		}
		if (GetAsyncKeyState('A') || GetAsyncKeyState('a'))
		{
			hero.posx--;
			BuildLevel();
		}
		if (GetAsyncKeyState('S') || GetAsyncKeyState('s'))
		{
			hero.posy--;
			BuildLevel();
		}
		if (GetAsyncKeyState('D') || GetAsyncKeyState('d'))
		{
			hero.posx++;
			BuildLevel();
		}
	}

	system("pause");
	return 0;
}
I'm not 100% sure on this(I've been doing Java for too long now.) You may want to try removing the first four lines of your main function. Unless you use them anywhere else, make them a private variable accessible to the entire class. Like try putting them under typedef unsigned char uchar;
Your four variables in your main function are only in the scope of the main function...so when you call buildLevel()...that function knows nothing of your four main game variables.

You either need to pass them to the function itself or just make those variables global (defined outside of any function or class). Although the latter isn't technically good programming practice...it will probably work just fine for this purpose.
Topic archived. No new replies allowed.