-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
334 lines (317 loc) · 7.61 KB
/
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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "acllib.h"
#include "AutoSprite.h"
#include "UsrSprite.h"
#include "AvoidSprite.h"
#include "EnemySprite.h"
#include "HistoryBase.h"
#include "PrizeSprite.h"
#include "PrizeSprite1.h"
#include "Ball.h"
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <chrono>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
const int maxNum= 1<<8;
const int winWidth= 800, winHeight= 600;
CAutoSprite *autosprite[maxNum]= {0};
CUsrSprite *usr= NULL;
HistoryBase *record[10+3];
int autoWidth= 100, autoHeight= 100;
int usrWidth= 100, usrHeight= 100;
int ballWidth= 50, ballHeight= 50;
int nowNum= 0, rk= 0, gdt= 1578239999;
ACL_Image img, imgUsr, imgHeart, imgEnm, imgGirl, imgButiGirl;
ACL_Image wpnimg, shieldimg;
ACL_Sound BGMsound;
rect winRect;
void CreateData(CAutoSprite **autospt);
void CreateData(CUsrSprite **usr);
void TimerEvent(int id);
void Paint();
void KeyEvent(int key, int event);
void GameOver();
void ReadRecord();
void WriteRecord();
void MouseEvent(int mx, int my, int button, int event);
int Gcd(int a, int b);
string GetCurrentSystemTime();
int Setup()
{
winRect.x= winRect.y= DEFAULT;
winRect.width= winWidth;
winRect.height= winHeight;
initWindow("Tom.Eureka.Newton's game", DEFAULT, DEFAULT, winWidth, winHeight);
srand((unsigned)time(NULL));
ReadRecord();
loadImage(".\\picture\\jerry.bmp", &img);
loadImage(".\\picture\\tom.bmp", &imgUsr);
loadImage(".\\picture\\duck.jpg", &imgHeart);
loadImage(".\\picture\\dog.bmp", &imgEnm);
loadImage(".\\picture\\girl.jpg", &imgGirl);
loadImage(".\\picture\\buti.jpg", &imgButiGirl);
loadImage(".\\picture\\weapon.jpg", &wpnimg);
loadSound(".\\music\\BGM.mp3", &BGMsound);
CreateData(autosprite);
CreateData(&usr);
playSound(BGMsound, 1);
registerTimerEvent(TimerEvent);
registerKeyboardEvent(KeyEvent);
registerMouseEvent(MouseEvent);
startTimer(0, 40);
startTimer(1, 1000);
WriteRecord();
return 0;
}
void CreateData(CAutoSprite **autospt)
{
int x= rand()%winWidth-autoWidth;
int y= rand()%winHeight-autoHeight;
int dx= rand()%5+1, dy= rand()%5+1;
if (x< 0){
x= 0;
}
if (y< 0){
y= 0;
}
;
int t= rand()%100;
if (t< 60){
autospt[nowNum++]= new CAutoSprite(x, y, autoWidth, autoHeight, dx, dy, &img, winRect, 2);
}
else if (t< 79){
autospt[nowNum++]= new CAvoidSprite(x, y, autoWidth, autoHeight, dx, dy, &imgHeart, winRect, 5);
}
else if (t< 90){
autospt[nowNum++]= new CEnemySprite(x, y, autoWidth, autoHeight, dx, dy, &imgEnm, winRect, 7);
}
else if (t< 94){
autospt[nowNum++]= new CPrizeSprite(x, y, autoWidth, autoHeight, dx, dy, &imgGirl, winRect, 2);
}
else{
autospt[nowNum++]= new CPrizeSprite1(x, y, autoWidth, autoHeight, dx, dy, &imgButiGirl, winRect, 1);
}
}
void CreateData(CUsrSprite **usr)
{
int x= rand()%winWidth-usrWidth;
int y= rand()%winHeight-usrHeight;
int dx=5, dy= 5;
if (x< 0){
x= 0;
}
if (y< 0){
y= 0;
}
*usr= new CUsrSprite(x, y, usrWidth, usrHeight, dx, dy, &imgUsr, winRect);
}
char aa[]= { 72, 52, 99, 107, 51, 114, 32, 73, 100, 33, 48, 116, 78, 51, 0};
void TimerEvent(int id)
{
int i= 0;
switch(id){
case 0:
{
for (i= 0; i< nowNum; ++i){
if (autosprite[i]){
rect ur= usr->getRect();
if (autosprite[i]->Bite(ur)){
usr->LoseHealth(1);
if (usr->ShowHealth()<= 0){
GameOver();
}
}
autosprite[i]->move(ur);
}
}
CBall* wpn= usr->ShowWeaponHead();
wpn= wpn->GetNext();
while (NULL!= wpn){
rect ur= usr->getRect();
wpn->move(ur);
for (i= 0; i< nowNum; ++i){
if (autosprite[i] && wpn->Collision(autosprite[i]->getRect())){
int s= autosprite[i]->getScore();
if (usr){
usr->addScore(s);
}
usr->GetPrize(autosprite[i]->Gift(rand()));
delete autosprite[i];
autosprite[i]= NULL;
wpn->LoseHealth();
}
}
if (1> wpn->ShowHealth()){
CBall *btmp= wpn->GetNext();
usr->UseOutWeapon(wpn);
wpn= btmp;
}
else{
wpn= wpn->GetNext();
}
}
if (0== usr->ShoeFade()){
usr->RushOff();
}
break;
}
case 1:
{
if (nowNum< maxNum){
CreateData(autosprite);
}
if (0== usr->ShoeFade()){
usr->RushOff();
}
break;
}
default:
break;
}
Paint();
}
void Paint()
{
beginPaint();
clearDevice();
int i= 0;
for (i= 0; i< nowNum; ++i){
if (autosprite[i]){
autosprite[i]->drawSprite();
}
}
if (usr){
CBall *wpn= usr->ShowWeaponHead();
wpn= wpn->GetNext();
while (NULL!= wpn){
wpn->drawSprite();
wpn= wpn->GetNext();
}
usr->drawSprite();
char txt[50];
setTextSize(16);
sprintf(txt, "Score Record: %d", record[0]->GetScore());
paintText(10, 5, txt);
sprintf(txt, "Score: %d", usr->getScore());
paintText(10, 20, txt);
if (time(0)>= gdt){
paintText(671, 575, aa);
}
sprintf(txt, "Helath: %d", usr->ShowHealth());
paintText(10, 35, txt);
sprintf(txt, "Weapon: %d", usr->Equipped());
paintText(10, 50, txt);
sprintf(txt, "SpeedUp: %d", usr->SpeedUp());
paintText(10, 65, txt);
sprintf(txt, "Time: %s", GetCurrentSystemTime().c_str());
paintText(10, 80, txt);
}
endPaint();
}
void KeyEvent(int key, int event)
{
if (KEY_DOWN!= event){
return;
}
if (usr){
usr->move(key);
}
for (int i= 0; i< nowNum; ++i){
if (autosprite[i]){
if (usr->collision(autosprite[i]->getRect())){
int s= autosprite[i]->getScore();
if (usr){
usr->addScore(s);
}
usr->GetPrize(autosprite[i]->Gift(rand()));
if (0== autosprite[i]->ShowHealth()){
delete autosprite[i];
autosprite[i]= NULL;
}
}
}
}
Paint();
}
void GameOver()
{
stopSound(BGMsound);
WriteRecord();
exit(0);
}
void ReadRecord()
{
char ttm[73];
int scr;
FILE *fp;
fp= fopen(".\\History\\rank.txt", "r");
while ('\n'!= fgetc(fp)){
continue;
}
while ('\n'!= fgetc(fp)){
continue;
}
while (EOF!= fscanf(fp, "%s %d", ttm, &scr) && rk< 10){
record[rk++]= new HistoryBase(ttm, scr);
}
fclose(fp);
}
void WriteRecord()
{
FILE *fp;
int scr= usr->getScore(), i= rk, lb= min(rk+1, 10);
char ttm[73];
string buf= GetCurrentSystemTime();
sprintf(ttm, "%s", buf.c_str());
record[rk]= new HistoryBase(ttm, scr);
while (i> 0 && *record[i-1]< *record[i]){
HistoryBase *tmp= record[i];
record[i]= record[i-1];
record[i-1]= tmp;
--i;
}
fp= fopen(".\\History\\record.txt", "a");
fputc('\n', fp);
fprintf(fp, "%s\t%d", ttm, scr);
fclose(fp);
fp= fopen(".\\History\\rank.txt", "w");
fprintf(fp, "Top 10 Rank:\n");
fprintf(fp, "Time\t\t\tScore\n");
for (i= 0; i< lb; ++i){
fprintf(fp, "%s\t%d\n", record[i]->GetTime(), record[i]->GetScore());
}
fclose(fp);
}
void MouseEvent(int mx, int my, int button, int event)
{
if (BUTTON_DOWN!= event || RIGHT_BUTTON!= button){
return;
}
rect ur= usr->getRect();
int dx= mx- ur.x, dy= my- ur.y;
if (0== dx && 0== dy){
return;
}
float tmp= sqrt(dx*dx+dy*dy);
float ratx= dx/tmp, raty= dy/tmp;
dx= 10*ratx;
dy= 10*raty;
usr->UseWeapon(&wpnimg, ballWidth, ballHeight, dx, dy);
}
inline int Gcd(int a, int b)
{
return 0== b ? a : Gcd(b, a%b);
}
string GetCurrentSystemTime()
{
auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
struct tm* ptm = localtime(&tt);
char date[60] = {0};
sprintf(date, "%d-%02d-%02d@%02d:%02d:%02d",
(int)ptm->tm_year + 1900,(int)ptm->tm_mon + 1,(int)ptm->tm_mday,
(int)ptm->tm_hour,(int)ptm->tm_min,(int)ptm->tm_sec);
return std::string(date);
}