-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathengine.h
251 lines (192 loc) · 4.6 KB
/
engine.h
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
#define ENGINE_VERSION "1.4"
#define AUTHOR Tom Yeh <[email protected]>
#include <iostream> // cout, cin, streambuf, hex, endl, sgetc, sbumpc
#include <string>
#include <vector>
#define BLOCK "██"
#pragma once
using namespace std;
struct Color
{
Color(int r_, int g_, int b_)
{
r = r_;
g = g_;
b = b_;
}
Color()
{
r = 255;
g = 255;
b = 255;
}
int r;
int g;
int b;
};
class Canvas
{
public:
void createCanvas(int w, int h);
void resizeCanvas(int w, int h)
{
createCanvas(w, h);
}
string _strokeSymbol = BLOCK;
Color _strokeColor = Color(255,255,255);
void stroke(const string icon);
void stroke(Color color)
{
_strokeColor = color;
}
void clear();
void point(int x, int y);
void point(int x, int y, string symbol, Color color);
void text(string word, int x, int y);
void line(int x1, int y1, int x2, int y2);
void vertical_line(int x, int y1, int y2);
void horizontal_line(int y, int x1, int x2);
void render();
bool _isLooping = true;
void noLoop()
{
_isLooping = false;
}
bool _isFill = false;
string _fillSymbol = BLOCK;
Color _fillColor = Color(255,255,255);
void fill(string fillSymbol)
{
_fillSymbol = fillSymbol;
_isFill = true;
}
void fill(Color color)
{
_fillColor = color;
_isFill = true;
}
void noFill()
{
_isFill = false;
}
void background(string symbol)
{
for (int i = 0; i < getHeight(); i++)
{
for (int j = 0 ; j < getWidth(); j++)
{
point(j, i, symbol, _strokeColor);
}
}
}
void rect(int x, int y, int width, int height)
{
horizontal_line(y, x, x + width - 1);
horizontal_line(y+height-1, x, x + width - 1);
vertical_line(x, y, y + height - 1);
vertical_line(x+width-1, y, y + height - 1);
if (_isFill)
{
string saved = _strokeSymbol;
for (int i = 1; i < height -1; i++)
{
// stroke(_fillSymbol);
for (int j = 0; j < width-2; j++)
{
point(x+1+j, y+i, _fillSymbol, _fillColor);
}
}
// stroke(saved);
}
}
string _description;
void describe(string text)
{
_description = text;
}
int _xo = 0;
int _yo = 0;
void translate(int x, int y)
{
_xo += x;
_yo += y;
}
private:
static const int MAX_WIDTH = 100;
static const int MAX_HEIGHT = 50;
string buffer[MAX_HEIGHT][MAX_WIDTH];
Color color_map[MAX_HEIGHT][MAX_WIDTH];
int _width;
int _height;
public:
int getWidth()
{
return _width;
}
int getHeight()
{
return _height;
}
};
class Engine : public Canvas
{
bool _pausedUntilSpaceIsPressed = false;
bool _quit = false;
public:
Engine()
{
createCanvas(10, 10);
}
protected:
void quit();
void pause();
virtual void draw() {};
virtual void console() {};
virtual void keyPressed(int keyCode) {};
virtual void setup() {};
void redraw()
{
draw();
render();
}
public:
void play();
void sleep(int milliseconds);
};
// returns a random number from 0 up to (but not including) the number.
int random(int n);
// returns a random number from the first argument up to
// (but not including) the second argument.
int random(int a, int b);
string random(vector<string> choices);
class Menu : public Engine
{
vector<Engine*> games;
vector<string> game_names;
public:
void add(Engine* game, string name)
{
games.push_back(game);
game_names.push_back(name);
}
void setup()
{
for (int i = 0 ; i < games.size() ; i++)
{
cout << (i+1) << ". " << game_names[i] << endl;
}
string c;
cout << "Choose a game:";
cin >> c;
int i = stoi(c);
if (i >= 1 && i <= games.size())
{
games.at(i-1)->play();
}
else
{
cout << "Not a valid choice." << endl;
}
quit();
}
};