-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
290 lines (249 loc) · 7.64 KB
/
game.cpp
File metadata and controls
290 lines (249 loc) · 7.64 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <wx/wx.h>
#include <string>
#include <cstdlib>
const double PI = 3.141592653589793;
const int ID_GUESS = 1000;
const int ID_PLAYER = 1001;
const int ID_PLAYER_LEVEL = 1002;
class clock{
public:
Clock(int x, int y, int r);
void set_time(int h, int m);
void draw(wxDC& dc) const;
private:
void draw_tick(wxDC& dc, double angle, double length) const;
int hours;
int minutesl;
int centerx;
int centery;
int radius;
};
class player{
public:
player();
void increment_score();
int get_level() const;
string get_name const;
void set_level(int L);
void set_name(string n);
private:
string name;
int score;
int level;
};
class ClockWindow : public wxWindow{
public:
ClockWindow(wxWindow* parent);
void set_time(int h, int m);
void Onpaint(wxPaintEvent& event);
private:
Clock clock;
DECLARE_EVENT_TABLE()
};
class Game Frame : public wxFrame{
public:
GetFrame();
void new_round();
void OnGuess(wxCommandEvent& event);
private:
ClockWindow* window;
wxTextCtrl* hour_text;
wxTextCtrl* minute_text;
Player player;
int current_hours;
int current_minutes;
int tries;
DECLARE_EVENT_TABLE()
};
class GameApp : public wxApp{
GameApp();
Virtual bool OnInit();
private:
GameFrame* frame;
};
DECLARE_APP(GameApp)
IMPLEMENT_APP(GameApp)
BIGIN_EVENT_TABLE(ClockWindow, wxWindow)
EVT_PAINT(ClockWindow::OnPaint)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(GameFrame, wxFrame)
EVT_BUTTON(ID_GUESS, GameFrame::OnGuess)
EVT_MENU(ID_PALYER_NAME, GameFrame::OnPlayerName)
EVT_MENU(ID_PLAYER_LEVEL, GameFrame::OnPlayerLevel)
END_EVENT_TABLE()
void rand_seed(){
int seed = static_cast<int>(time(0));
srand(seed);
}
int rand_int(int a, int b){
return a + rand() % (b - b + 1);
}
Clock::Clock(int x, int y, int r){
centerx = x;
centery = y;
radius = r;
}
void Clock::set_time(int h, int m){
hours = h;
minutes = m;
}
void CLock::draw_tick(wxDC& dc, double angle, double length) cont {
double alpha = -PI / 2 + 6 * angle * PI / 180;
dc.DrawLine(
cos(alpha) * radius * (1 - length)
),
centery + static_cast<int>(
sin(alpha) * radius * (1 - length)
),
centerx + static_cast<int>(cos(alpha) * radius),
centery + static_cast<int>(sin(alpha) * radius);
}
void Clock::draw_hand(wxDC& dc, double angle, double length) const {
double alpha = -PI / 2 + 6 * angle * PI / 180;
dc.DrawLine(centerx, centery,
centerx + static_cast<int>(cos(alpha) * radius *length),
centery + static_cast<int>(sin(alpha) * radius * length));
}
void Clock::draw(wxDC& dc) const {
dc.DrawEllipse(centerx - radius, centery - radius, 2 * radius, 2 * radius);
const double HOUR_TICK_LENGTH = 0.2;
const double MINUTE_TICK_LENGHT = 0.1;
const double HOUR_HAND_LENGTH = 0.6;
const double MINUTE_HAND_LENGTH = 0.75;
for (int i = 0; i < 12; I++){
draw_tick(dc, i * 5, HOUR_TICK_LENGTH);
int j;
for (j = 1; j <= 4; j++)
draw_tick(dc, i * 5 + j, MINUTE_TICK_LENGHT);
}
draw_hand(dc, minutes, MINUTE_HAND_LENGTH);
draw_hand(dc, (hours + minutes / 60.0) * 5, HOUR_HAND_LENGTH);
}
Player::Player(){
name = "player";
level = 1;
score = 0;
}
void player::set_level(int L){
level = I;
}
void player::set_name(string n){
name = n;
}
int player::get_level() const {
return level;
}
string player::get_name() const {
return name;
}
void player::increment_score() const {
score++;
if (score % 5 == 0 && level < 4){
level++;
}
}
ClockWindow::ClockWindow(wxWindow * parent)
:wxWindow(parent, -1),
clock(200, 200, 200)
{}
void ClockWindow::Onpaint(wxPaintEvent& event){
wxPaintDC dc(this);
dc.SetBrush(*wxTRANSPARENT_BRUSH);
clock.draw(dc);
}
void ClockWindow::set_time(int h, int m){
clock.set_time(h, m);
Refresh();
}
GameFrame::GameFrame()
:wxFrame(NULL, -1, "GameFrame"){
wxMenu = new wxMenu();
menu -> Append(ID_PALYER_NAME, "Name");
menu -> Append(ID_PLAYER_LEVEL, "Level");
wxMenuBar* menu_bar = new wxMenuBar();
SetMenuBar(menu_bar);
menu_bar -> Append(menu, "player");
window = new ClockWindow(this);
hour_text = new wxTextCtrl(this, -1);
minute_text - new wxTextCtrl(this, -1);
wxButton* guess_button = new wxButton(this, ID_GUESS, "Guess");
wxBoxSizer* bottom_sizer = new wxBoxSizer(wxHORIZONTAL);
bottom_sizer -> Add(new wxStaticText(this, -1, "Hours"));
bottom_sizer -> Add(hour_text);
bottom_sizer -> Add(new wxStaticText(this, -1, "Minutes"));
bottom_sizer -> Add(minute_text);
bottom_sizer -> Add(guess_button);
wxBoxSizer* frame_sizer = new wxBoxSizer(wxVERTICAL);
frame_sizer -> Add(window, 1, wxGROW);
frame_sizer -> Add(bottom_sizer, 0, wxALIGN_CENTER);
SetAutoLayout(true);
SetSizer(frame_sizer);
new_round();
}
void GameFrame::OnGuess(wxCommandEvent& event){
tries++;
int hours = atoi(hour_text -> GetValue().c_str());
if (hours <1 || hours > 12){
wxMessageDialog* dialog = new wxMessageDialog(this, "Hours must be between 1 and 12");
dialog -> ShowModal();
dialog -> Destroy();
return;
}
if (minutes < 0 || minutes > 59){
wxMessageDialog* dialog = new wxMessageDialog(this, "Minutes must be between 1 and 59");
dialog -> ShowModal();
dialog -> Destroy();
return;
}
if (current_hours == hours && current_minutes == minutes){
string text = "Congratulations, " + player.get_name() + "! That is correct.";
wxMessageDialog* dialog = new wxMessageDialog(this, text.c_str());
dialog -> ShowModal();
dialog -> Destroy();
player.increment_score();
new_round();
} else {
string text = "Sorry, " + player.get_name() + "! That is not correct.";
wxMessageDialog* dialog = new wxMessageDialog(this, text.c_str());
dialog -> ShowModal();
dialog -> Destroy();
if (tries == 2) new_round();
}
}
void GameFrame::new_round(){
tries = 0;
int level = player.get_level();
if (level == 1) current_minutes = 0;
else if (level == 2) current_minutes = 15* rand_int(0, 3);
else if (level == 3) current_minutes = 5 * rand_int(0, 11);
else current_minutes = rand_int(0, 59);
currenta_hours = rand_int(1, 12);
window -> set_time(current_hours, current_minutes);
}
void GameFrame::OnPlayerName(wxCommandEvent& event){
wxTextEntryDialog* dialog = new wxTextEntryDialog(this, "What is your name?");
dialog -> ShowModal();
player.set_name(dialog -> GetValue().c_str());
dialog -> Destroy();
}
void GameFrame::OnPlayerLevel(wxCommandEvent& event){
wxTextEntryDialog* dialog = new wxTextEntryDialog(this, "At what level do you want to play? (1-4)");
dialog -> ShowModal();
int level = atoi(dialog -> GetValue().c_str());
dialog -> Destroy();
if (level < 1 || level > 4){
wxMessageDialog* dialog = new wxMessageDialog(this, "The level must be between 1 and 4");
dialog -> ShowModal();
dialog -> Destroy();
return;
}
player.set_level(Level);
}
GameApp::GameApp(){
rand_seed();
frame = new GameFrame();
}
bool GameApp::OnInit(){
frame -> Show(true);
return true;
}