-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
212 lines (176 loc) · 5.01 KB
/
main.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
#include"acllib.h"
#include"sprite_base.h"
#include"sprite_player.h"
#include"sprite_rat.h"
#include"sprite_duck.h"
#include"sprite_dog.h"
#include<cstdio>
#include<ctime>
#define HEIGHT 800
#define WIDTH 1200
const int baseSpeed = 10;
const int spriteRatNum = 9;
const int spriteDuckNum = 6;
const int spriteDogNum = 3;
const int spriteNum = 4; //sprit种类
static SpritePlayer player;
static SpriteRat rats[spriteRatNum];
static SpriteDuck ducks[spriteDuckNum];
static SpriteDog dogs[spriteDogNum];
static ACL_Image images[spriteNum];
void timerEvent(int id);
void keyEvent(int key, int event);
void paint();
void spriteMove(int key);
void createSprite();
void paintSprite();
int Setup() {
srand((unsigned)time(NULL));
initWindow("sprite game", DEFAULT, DEFAULT, WIDTH, HEIGHT);
loadImage("tom.bmp", images);
loadImage("jerry.bmp", images + 1);
loadImage("duck.jpg", images + 2);
loadImage("dog.bmp", images + 3);
createSprite();
registerTimerEvent(timerEvent);
registerKeyboardEvent(keyEvent);
startTimer(0, 20);
startTimer(1, 100);
return 0;
}
void keyEvent(int key, int event) {
if (event != KEY_DOWN) { return; }
spriteMove(key);
paint();
}
void timerEvent(int id) {
switch (id) {
case 0: {
spriteMove(id);
} break;
case 1: {
createSprite();
} break;
}
paint();
}
void paint() {
beginPaint();
clearDevice();
//sprite
paintSprite();
//score
char txt[20];
sprintf_s(txt, "Score: %d", player.getScore());
setTextSize(20);
paintText(10, 10, txt);
endPaint();
}
//---change
void spriteMove(int key) {
if (key == 0) {
//meet rat
for (int i = 0; i < spriteRatNum; ++i) {
if (rats[i].survive && player.collision(rats[i])) {
rats[i].survive = false;
player.addScore(rats[i].getScore());
}
}
//meet duck
for (int i = 0; i < spriteDuckNum; ++i) {
if (ducks[i].survive && player.collision(ducks[i])) {
ducks[i].survive = false;
player.addScore(ducks[i].getScore());
}
}
//meet dog
for (int i = 0; i < spriteDogNum; ++i) {
if (player.survive && player.collision(dogs[i])) {
player.survive = false;
player.setScore(player.getScore() / 2);
}
}
//rat
for (int i = 0; i < spriteRatNum; ++i) {
rats[i].move(key, HEIGHT, WIDTH);
}
//duck
for (int i = 0; i < spriteDuckNum; ++i) {
ducks[i].move(key, HEIGHT, WIDTH, &player);
}
//dog
for (int i = 0; i < spriteDogNum - 1; ++i) {
dogs[i].move(key, HEIGHT, WIDTH, &player);
for (int j = i + 1; j < spriteDogNum; ++j) {
dogs[i].escMove(key, HEIGHT, WIDTH, &dogs[j]);
}
} dogs[spriteDogNum - 1].move(key, HEIGHT, WIDTH, &player);
}
else {
//player
player.move(key, HEIGHT, WIDTH);
}
}
void createSprite() {
//player
if (!player.survive) {
int nSpeed = player.speed / 2 + rand() % baseSpeed + baseSpeed;
player.initilize(Pos(rand() % WIDTH, rand() % HEIGHT), HEIGHT / 10, WIDTH / 10, nSpeed, images);
}
//rat
for (int i = 0; i < spriteRatNum; ++i) {
if (rats[i].survive) {
continue;
}
else {
int nSpeed = (rats[i].speed / 2 + rand() % baseSpeed) / 3;
if (nSpeed < 0) {
nSpeed -= baseSpeed;
}
else {
nSpeed += baseSpeed;
}
int h = HEIGHT / 10, w = WIDTH / 10;
rats[i].initilize(Pos(rand() % (WIDTH - w - 10) + w / 2, rand() % (HEIGHT - h - 10) + h / 2), h / 1.5, w / 1.5, nSpeed, images + 1);
}
}
//duck
for (int i = 0; i < spriteDuckNum; ++i) {
if (ducks[i].survive) {
continue;
}
else {
ducks[i].escape = true;
int nSpeed = rand() % baseSpeed / 3 + baseSpeed / 1.5;
int h = HEIGHT / 10, w = WIDTH / 10;
ducks[i].initilize(Pos(rand() % (WIDTH - w - 10) + w / 2, rand() % (HEIGHT - h - 10) + h / 2), h / 1.5, w / 1.5, nSpeed, images + 2);
}
}
//dog
for (int i = 0; i < spriteDogNum; ++i) {
if (dogs[i].survive) {
continue;
}
else {
int nSpeed = rand() % baseSpeed / 2 + baseSpeed / 4;
int h = HEIGHT / 10, w = WIDTH / 10;
dogs[i].initilize(Pos(rand() % (WIDTH - w - 10) + w / 2, rand() % (HEIGHT - h - 10) + h / 2), h, w, nSpeed, images + 3);
}
}
}
void paintSprite() {
//player
player.drawSprite();
//rat
for (int i = 0; i < spriteRatNum; ++i) {
rats[i].drawSprite();
}
//duck
for (int i = 0; i < spriteDuckNum; ++i) {
ducks[i].drawSprite();
}
//dog
for (int i = 0; i < spriteDogNum; ++i) {
dogs[i].drawSprite();
}
}