Commit 7b5a6fc36c4bffa70c8e8d525968bc18b5cf4a31
1 parent
658175b4
Cleanup
Showing
1 changed file
with
27 additions
and
42 deletions
06-multiple_targets/cannon.c
1 | - | |
2 | 1 | #include "primlib.h" |
3 | 2 | #include <math.h> |
4 | 3 | #include <stdlib.h> |
... | ... | @@ -8,57 +7,46 @@ struct target { |
8 | 7 | int y; |
9 | 8 | int speed_x; |
10 | 9 | int is_explosion; |
11 | - int explosion_counter; // ile razy wyswietlalismy eksplozje | |
10 | + int explosion_counter; // how many times we have already displayed an | |
11 | + // explosion animation | |
12 | 12 | int color; |
13 | 13 | }; |
14 | 14 | |
15 | 15 | // Draw explosion with the center at coordinates (x,y) |
16 | 16 | void drawExplosion(int x, int y, float scaling) { |
17 | 17 | |
18 | - gfx_filledTriangle(x - 7 * scaling, y - 7 * scaling, | |
19 | - x + 7 * scaling, y + 7 * scaling, | |
20 | - x + 22 * scaling, y - 22 * scaling, RED); | |
21 | - gfx_filledTriangle(x - 7 * scaling, y - 7 * scaling, | |
22 | - x + 7 * scaling, y + 7 * scaling, | |
23 | - x - 22 * scaling, y + 22 * scaling, RED); | |
24 | - gfx_filledTriangle(x - 7 * scaling, y + 7 * scaling, | |
25 | - x + 7 * scaling, y - 7 * scaling, | |
26 | - x - 22 * scaling, y - 22 * scaling, RED); | |
27 | - gfx_filledTriangle(x - 7 * scaling, y + 7 * scaling, | |
28 | - x + 7 * scaling, y - 7 * scaling, | |
29 | - x + 22 * scaling, y + 22 * scaling, RED); | |
30 | - | |
31 | - gfx_filledTriangle(x - 13 * scaling, y, | |
32 | - x + 13 * scaling, y, x, | |
18 | + gfx_filledTriangle(x - 7 * scaling, y - 7 * scaling, x + 7 * scaling, | |
19 | + y + 7 * scaling, x + 22 * scaling, y - 22 * scaling, RED); | |
20 | + gfx_filledTriangle(x - 7 * scaling, y - 7 * scaling, x + 7 * scaling, | |
21 | + y + 7 * scaling, x - 22 * scaling, y + 22 * scaling, RED); | |
22 | + gfx_filledTriangle(x - 7 * scaling, y + 7 * scaling, x + 7 * scaling, | |
23 | + y - 7 * scaling, x - 22 * scaling, y - 22 * scaling, RED); | |
24 | + gfx_filledTriangle(x - 7 * scaling, y + 7 * scaling, x + 7 * scaling, | |
25 | + y - 7 * scaling, x + 22 * scaling, y + 22 * scaling, RED); | |
26 | + | |
27 | + gfx_filledTriangle(x - 13 * scaling, y, x + 13 * scaling, y, x, | |
33 | 28 | y - 35 * scaling, RED); |
34 | - gfx_filledTriangle(x - 13 * scaling, y, | |
35 | - x + 13 * scaling, y, x, | |
29 | + gfx_filledTriangle(x - 13 * scaling, y, x + 13 * scaling, y, x, | |
36 | 30 | y + 35 * scaling, RED); |
37 | - gfx_filledTriangle(x, y - 13 * scaling, x, | |
38 | - y + 13 * scaling, x - 35 * scaling, | |
31 | + gfx_filledTriangle(x, y - 13 * scaling, x, y + 13 * scaling, x - 35 * scaling, | |
39 | 32 | y, RED); |
40 | - gfx_filledTriangle(x, y - 13 * scaling, x, | |
41 | - y + 13 * scaling, x + 35 * scaling, | |
33 | + gfx_filledTriangle(x, y - 13 * scaling, x, y + 13 * scaling, x + 35 * scaling, | |
42 | 34 | y, RED); |
43 | 35 | } |
44 | 36 | |
45 | - | |
46 | 37 | void draw_target(struct target *t) { |
47 | 38 | if (!t->is_explosion) |
48 | - gfx_filledRect(t->x - 10, t->y - 10, t->x + 10, t->y + 10, | |
49 | - t->color); // cel | |
39 | + gfx_filledRect(t->x - 10, t->y - 10, t->x + 10, t->y + 10, | |
40 | + t->color); // target | |
50 | 41 | |
51 | 42 | if (t->is_explosion) { |
52 | 43 | drawExplosion(t->x, t->y + t->explosion_counter * 10, |
53 | 44 | t->explosion_counter / 5.0); |
54 | 45 | } |
55 | - | |
56 | 46 | } |
57 | 47 | |
58 | - | |
59 | 48 | void move_target(struct target *t) { |
60 | - if(!t->is_explosion) | |
61 | - { | |
49 | + if (!t->is_explosion) { | |
62 | 50 | t->x += t->speed_x; |
63 | 51 | |
64 | 52 | if (t->x > gfx_screenWidth()) |
... | ... | @@ -128,26 +116,23 @@ int main() { |
128 | 116 | for (int i = 0; i < num_targets; ++i) |
129 | 117 | draw_target(&(t[i])); |
130 | 118 | |
131 | - | |
119 | + // bullet position wrt the center of the bottom of the screen | |
132 | 120 | int x_bullet = bullet_distance * cos(fire_angle); |
133 | 121 | int y_bullet = bullet_distance * sin(fire_angle); |
134 | - int x_bullet_pot = gfx_screenWidth() / 2 + x_bullet; | |
135 | - int y_bullet_pot = gfx_screenHeight() - y_bullet; | |
136 | - | |
137 | - if (is_shooting) { | |
138 | - // if(!is_explosion) | |
139 | - gfx_filledCircle(gfx_screenWidth() / 2 + x_bullet, | |
140 | - gfx_screenHeight() - y_bullet, 10, RED); // pocisk | |
141 | 122 | |
123 | + // bullet position wrt the top-left corner | |
124 | + int x_bullet_tlc = gfx_screenWidth() / 2 + x_bullet; | |
125 | + int y_bullet_tlc = gfx_screenHeight() - y_bullet; | |
142 | 126 | |
127 | + if (is_shooting) { | |
128 | + gfx_filledCircle(x_bullet_tlc, y_bullet_tlc, 10, RED); // bullet | |
143 | 129 | } |
144 | 130 | |
145 | 131 | gfx_updateScreen(); |
146 | 132 | |
147 | - if (is_shooting) | |
148 | - { | |
133 | + if (is_shooting) { | |
149 | 134 | for (int i = 0; i < num_targets; ++i) { |
150 | - if (hypot(x_bullet_pot - t[i].x, y_bullet_pot - t[i].y) < 50) { | |
135 | + if (hypot(x_bullet_tlc - t[i].x, y_bullet_tlc - t[i].y) < 50) { | |
151 | 136 | t[i].is_explosion = 1; |
152 | 137 | t[i].explosion_counter = 0; |
153 | 138 | } |
... | ... | @@ -156,7 +141,7 @@ int main() { |
156 | 141 | } |
157 | 142 | |
158 | 143 | for (int i = 0; i < num_targets; ++i) |
159 | - move_target(&(t[i])); | |
144 | + move_target(&(t[i])); | |
160 | 145 | |
161 | 146 | if (gfx_isKeyDown(SDLK_RIGHT)) |
162 | 147 | angle -= 1.0 * (M_PI / 180.0); | ... | ... |