/
bullet1.c
200 lines (192 loc) · 4.4 KB
/
bullet1.c
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
/////////////////////////////////////////////////////////////////////
// Game Programming All In One, Third Edition
// Tank War Enhancement 5 - bullet.c
/////////////////////////////////////////////////////////////////////
#include "tankwar.h"
//new function added in Chapter 11
void updateexplosion(int num)
{
int x, y,i;
for(i=0;i<MAXBULLET;i++){
if (!explosions[num]->alive) continue ;
//draw explosion (maxframe) times
if (explosions[num]->curframe++ < explosions[num]->maxframe)
{
x = explosions[num]->x;
y = explosions[num]->y;
//draw explosion in enemy window
rotate_sprite(buffer, explode_bmp,
x + rand()%10 - 20, y + rand()%10 - 20,
itofix(rand()%255));
//draw explosion in "my" window if enemy is visible
x = scrollx[!num] + SCROLLW/2;
y = scrolly[!num] + SCROLLH/2;
if (inside(x, y,
scrollx[num], scrolly[num],
scrollx[num] + SCROLLW, scrolly[num] + SCROLLH))
{
//but only draw if explosion is active
if (explosions[num]->alive)
rotate_sprite(buffer, explode_bmp,
startx[num]+x-scrollx[num] + rand()%10 - 20,
starty[num]+y-scrolly[num] + rand()%10 - 20,
itofix(rand()%255));
}
}
else
{
explosions[num]->alive = 0;
explosions[num]->curframe = 0;
}
}
}
void explode(int num, int x, int y)
{
//initialize the explosion sprite
explosions[num]->alive = 1;
explosions[num]->x = x;
explosions[num]->y = y;
explosions[num]->curframe = 0;
explosions[num]->maxframe = 20;
}
void drawbullet(int num)
{
int n;
int x, y,i;
for(i=0;i<MAXBULLET;i++){
x = bullets[num][i]->x;
y = bullets[num][i]->y;
//is the bullet active?
if (!bullets[num][i]->alive) continue;
//draw bullet sprite
for (n=0; n<2; n++)
{
if (inside(x, y, scrollx[n], scrolly[n],
scrollx[n] + SCROLLW - bullet_bmp[n]->w,
scrolly[n] + SCROLLH - bullet_bmp[n]->h))
//draw bullet, adjust for scroll
draw_sprite(buffer, bullet_bmp[num], startx[n] + x-scrollx[n],
starty[n] + y-scrolly[n]);
}
//draw bullet on radar
putpixel(buffer, radarx + x/10, radary + y/12, WHITE);
}
}
void movebullet(int num)
{
int x, y, tx, ty,i;
for(i=0;i<MAXBULLET;i++){
x = bullets[num][i]->x;
y = bullets[num][i]->y;
//is the bullet active?
if (!bullets[num][i]->alive) continue ;
//move bullet
bullets[num][i]->x += bullets[num][i]->xspeed;
bullets[num][i]->y += bullets[num][i]->yspeed;
x = bullets[num][i]->x;
y = bullets[num][i]->y;
//stay within the virtual screen
if (x < 0 || x > MAPW-6 || y < 0 || y > MAPH-6)
{
bullets[num][i]->alive = 0;
continue;
}
//look for a direct hit using basic collision
tx = scrollx[!num] + SCROLLW/2;
ty = scrolly[!num] + SCROLLH/2;
//if (collided(bullets[num], tanks[!num]))
if (inside(x,y,tx-15,ty-15,tx+15,ty+15))
{
//kill the bullet
bullets[num][i]->alive = 0;
//blow up the tank
x = scrollx[!num] + SCROLLW/2;
y = scrolly[!num] + SCROLLH/2;
if (inside(x, y,
scrollx[num], scrolly[num],
scrollx[num] + SCROLLW, scrolly[num] + SCROLLH))
{
//draw explosion in my window
explode(num,startx[num]+x-scrollx[num],
starty[num]+y-scrolly[num]);
}
//draw explosion in enemy window
explode(num, tanks[!num]->x, tanks[!num]->y);
scores[num]++;
}
}
}
void fireweapon(int num)
{
int x = scrollx[num] + SCROLLW/2;
int y = scrolly[num] + SCROLLH/2;
//ready to fire again?
int i;
for(i=0;i<MAXBULLET;i++){
if (!bullets[num][i]->alive)
{
bullets[num][i]->alive = 1;
//fire bullet in direction tank is facing
switch (tanks[num]->dir)
{
//north
case 0:
bullets[num][i]->x = x-2;
bullets[num][i]->y = y-22;
bullets[num][i]->xspeed = 0;
bullets[num][i]->yspeed = -BULLETSPEED;
break;
//NE
case 1:
bullets[num][i]->x = x+18;
bullets[num][i]->y = y-18;
bullets[num][i]->xspeed = BULLETSPEED;
bullets[num][i]->yspeed = -BULLETSPEED;
break;
//east
case 2:
bullets[num][i]->x = x+22;
bullets[num][i]->y = y-2;
bullets[num][i]->xspeed = BULLETSPEED;
bullets[num][i]->yspeed = 0;
break;
//SE
case 3:
bullets[num][i]->x = x+18;
bullets[num][i]->y = y+18;
bullets[num][i]->xspeed = BULLETSPEED;
bullets[num][i]->yspeed = BULLETSPEED;
break;
//south
case 4:
bullets[num][i]->x = x-2;
bullets[num][i]->y = y+22;
bullets[num][i]->xspeed = 0;
bullets[num][i]->yspeed = BULLETSPEED;
break;
//SW
case 5:
bullets[num][i]->x = x-18;
bullets[num][i]->y = y+18;
bullets[num][i]->xspeed = -BULLETSPEED;
bullets[num][i]->yspeed = BULLETSPEED;
break;
//west
case 6:
bullets[num][i]->x = x-22;
bullets[num][i]->y = y-2;
bullets[num][i]->xspeed = -BULLETSPEED;
bullets[num][i]->yspeed = 0;
break;
//NW
case 7:
bullets[num][i]->x = x-18;
bullets[num][i]->y = y-18;
bullets[num][i]->xspeed = -BULLETSPEED;
bullets[num][i]->yspeed = -BULLETSPEED;
break;
}
break;
}
}
}