-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroom-game.cpp
183 lines (151 loc) · 4.46 KB
/
room-game.cpp
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
#include "engine.h"
#include <fstream>
class RoomGame : public Engine
{
protected:
int player_x;
int player_y;
int num_treasures_found;
string ICON_PLAYER = "🧍";
string ICON_EXIT = " ";
string ICON_WALL = "▓▓";
string ICON_DOOR = "🚪";
string ICON_TREASURE = "💰";
string ICON_MONSTER = "🦁";
const int MAP_HEIGHT = 16;
const int MAP_WIDTH = 48;
const int SCREEN_HEIGHT = 12;
const int SCREEN_WIDTH = 12;
// a vector of strings to store the map data
// map_data[i][j] gives us access to
// i'th row, j'th column
// or coordinate (j, i) i.e., x = column, y = row
vector<string> map_data;
void setup()
{
// read the map data from file
string line;
ifstream file("room-data.txt");
if (file.is_open())
{
while (getline(file,line))
{
map_data.push_back(line);
}
file.close();
}
// find the player's starting position
for (int i = 0; i < MAP_HEIGHT; i++)
{
for (int j = 0; j < MAP_WIDTH; j++)
{
char c = map_data[i][j];
switch (c)
{
case 'S':
player_x = j;
player_y = i;
}
}
}
// initalize the number of treasures found
num_treasures_found = 0;
createCanvas(SCREEN_WIDTH, SCREEN_HEIGHT);
}
void keyPressed(int keyCode)
{
int x1 = player_x;
int y1 = player_y;
switch (keyCode)
{
case 'd':
x1 = player_x + 1;
break;
case 'a':
x1 = player_x - 1;
break;
case 'w':
y1 = player_y - 1;
break;
case 's':
y1 = player_y + 1;
break;
}
// check if this move is not within the bound and also not a wall
if (x1 < MAP_WIDTH && y1 < MAP_HEIGHT && x1 >= 0 && y1 >= 0 && map_data[y1][x1] != 'W')
{
player_x = x1;
player_y = y1;
}
}
void draw()
{
clear();
// translate by half the screen dimensions with respect to the
// player's location in order to center the screen on the player
translate(SCREEN_WIDTH/2-player_x, SCREEN_HEIGHT/2-player_y);
for (int i = 0 ; i < MAP_HEIGHT; i++)
{
for (int j = 0 ; j < MAP_WIDTH ; j++)
{
char d = map_data[i][j];
switch (d)
{
case 'W':
stroke(ICON_WALL);
point(j, i);
break;
case 'D':
stroke(ICON_DOOR);
point(j, i);
break;
case 'T':
stroke(ICON_TREASURE);
point(j, i);
break;
case 'M':
stroke(ICON_MONSTER);
point(j, i);
break;
case 'E':
stroke(ICON_EXIT);
point(j, i);
break;
}
}
}
stroke(ICON_PLAYER);
point(player_x, player_y);
}
void console()
{
cout << "Number of treasures found: " << num_treasures_found << endl;
char d = map_data[player_y][player_x];
if (d == 'E')
{
cout << "You left this room." << endl;
quit();
}
else if (d == 'T')
{
cout << "You found a treasure!" << endl;
// increment the number of treasures found by one
num_treasures_found++;
// remove the treasure by setting the map data
// at this location to blank
map_data[player_y][player_x] = ' ';
pause();
}
else if (d == 'M')
{
cout << "You got killed by a monster!" << endl;
cout << "Game Over" << endl;
quit();
}
}
};
int main()
{
RoomGame game;
game.play();
}