Commit 7165abda29a8911f202caa3e0a246d6baa4bb7aa
1 parent
75b87b36
Multiple targets
Showing
2 changed files
with
191 additions
and
0 deletions
06-multiple_targets/cannon.c
0 → 100644
| 1 | + | ||
| 2 | +#include "primlib.h" | ||
| 3 | +#include <math.h> | ||
| 4 | +#include <stdlib.h> | ||
| 5 | + | ||
| 6 | +struct target { | ||
| 7 | + int x; | ||
| 8 | + int y; | ||
| 9 | + int speed_x; | ||
| 10 | + int is_explosion; | ||
| 11 | + int explosion_counter; // ile razy wyswietlalismy eksplozje | ||
| 12 | + int color; | ||
| 13 | +}; | ||
| 14 | + | ||
| 15 | +// Draw explosion with the center at coordinates (x,y) | ||
| 16 | +void drawExplosion(int x, int y, float scaling) { | ||
| 17 | + int x_triangle = x; | ||
| 18 | + int y_triangle = y; | ||
| 19 | + | ||
| 20 | + gfx_filledTriangle(x_triangle - 7 * scaling, y_triangle - 7 * scaling, | ||
| 21 | + x_triangle + 7 * scaling, y_triangle + 7 * scaling, | ||
| 22 | + x_triangle + 22 * scaling, y_triangle - 22 * scaling, RED); | ||
| 23 | + gfx_filledTriangle(x_triangle - 7 * scaling, y_triangle - 7 * scaling, | ||
| 24 | + x_triangle + 7 * scaling, y_triangle + 7 * scaling, | ||
| 25 | + x_triangle - 22 * scaling, y_triangle + 22 * scaling, RED); | ||
| 26 | + gfx_filledTriangle(x_triangle - 7 * scaling, y_triangle + 7 * scaling, | ||
| 27 | + x_triangle + 7 * scaling, y_triangle - 7 * scaling, | ||
| 28 | + x_triangle - 22 * scaling, y_triangle - 22 * scaling, RED); | ||
| 29 | + gfx_filledTriangle(x_triangle - 7 * scaling, y_triangle + 7 * scaling, | ||
| 30 | + x_triangle + 7 * scaling, y_triangle - 7 * scaling, | ||
| 31 | + x_triangle + 22 * scaling, y_triangle + 22 * scaling, RED); | ||
| 32 | + | ||
| 33 | + gfx_filledTriangle(x_triangle - 13 * scaling, y_triangle, | ||
| 34 | + x_triangle + 13 * scaling, y_triangle, x_triangle, | ||
| 35 | + y_triangle - 35 * scaling, RED); | ||
| 36 | + gfx_filledTriangle(x_triangle - 13 * scaling, y_triangle, | ||
| 37 | + x_triangle + 13 * scaling, y_triangle, x_triangle, | ||
| 38 | + y_triangle + 35 * scaling, RED); | ||
| 39 | + gfx_filledTriangle(x_triangle, y_triangle - 13 * scaling, x_triangle, | ||
| 40 | + y_triangle + 13 * scaling, x_triangle - 35 * scaling, | ||
| 41 | + y_triangle, RED); | ||
| 42 | + gfx_filledTriangle(x_triangle, y_triangle - 13 * scaling, x_triangle, | ||
| 43 | + y_triangle + 13 * scaling, x_triangle + 35 * scaling, | ||
| 44 | + y_triangle, RED); | ||
| 45 | +} | ||
| 46 | + | ||
| 47 | + | ||
| 48 | +void draw_target(struct target *t) { | ||
| 49 | + if (!t->is_explosion) | ||
| 50 | + gfx_filledRect(t->x - 10, t->y - 10, t->x + 10, t->y + 10, | ||
| 51 | + t->color); // cel | ||
| 52 | + | ||
| 53 | + if (t->is_explosion) { | ||
| 54 | + drawExplosion(t->x, t->y + t->explosion_counter * 10, | ||
| 55 | + t->explosion_counter / 5.0); | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | + | ||
| 61 | +void move_target(struct target *t) { | ||
| 62 | + if(!t->is_explosion) | ||
| 63 | + { | ||
| 64 | + t->x += t->speed_x; | ||
| 65 | + | ||
| 66 | + if (t->x > gfx_screenWidth()) | ||
| 67 | + t->x = 0; | ||
| 68 | + | ||
| 69 | + if (t->x < 0) | ||
| 70 | + t->x = gfx_screenWidth() - 1; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + if (t->is_explosion) { | ||
| 74 | + t->explosion_counter += 1; | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + if (t->is_explosion && (t->explosion_counter == 10)) { | ||
| 78 | + t->is_explosion = 0; | ||
| 79 | + t->x = 0; | ||
| 80 | + } | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +int main() { | ||
| 84 | + if (gfx_init()) | ||
| 85 | + exit(3); | ||
| 86 | + | ||
| 87 | + double angle = 90.0 * (M_PI / 180.0); | ||
| 88 | + | ||
| 89 | + double bullet_distance; | ||
| 90 | + int is_shooting = 0; | ||
| 91 | + double fire_angle; | ||
| 92 | + | ||
| 93 | + const int num_targets = 3; | ||
| 94 | + | ||
| 95 | + struct target t[num_targets]; | ||
| 96 | + | ||
| 97 | + t[0].x = 0; | ||
| 98 | + t[0].y = 40; | ||
| 99 | + t[0].speed_x = 3; | ||
| 100 | + t[0].is_explosion = 0; | ||
| 101 | + t[0].color = MAGENTA; | ||
| 102 | + | ||
| 103 | + t[1].x = 1000; | ||
| 104 | + t[1].y = 80; | ||
| 105 | + t[1].speed_x = -5; | ||
| 106 | + t[1].is_explosion = 0; | ||
| 107 | + t[1].color = WHITE; | ||
| 108 | + | ||
| 109 | + t[2].x = 100; | ||
| 110 | + t[2].y = 120; | ||
| 111 | + t[2].speed_x = 7; | ||
| 112 | + t[2].is_explosion = 0; | ||
| 113 | + t[2].color = GREEN; | ||
| 114 | + | ||
| 115 | + while (1) { | ||
| 116 | + double delta_angle = 2.0 * (M_PI / 180.0); | ||
| 117 | + int x1_barrel = 150 * cos(angle - delta_angle); | ||
| 118 | + int y1_barrel = 150 * sin(angle - delta_angle); | ||
| 119 | + int x2_barrel = 150 * cos(angle + delta_angle); | ||
| 120 | + int y2_barrel = 150 * sin(angle + delta_angle); | ||
| 121 | + gfx_filledRect(0, 0, gfx_screenWidth() - 1, gfx_screenHeight() - 1, BLUE); | ||
| 122 | + | ||
| 123 | + gfx_filledCircle(gfx_screenWidth() / 2, gfx_screenHeight(), 100, YELLOW); | ||
| 124 | + gfx_filledTriangle(gfx_screenWidth() / 2, gfx_screenHeight(), | ||
| 125 | + gfx_screenWidth() / 2 + x1_barrel, | ||
| 126 | + gfx_screenHeight() - y1_barrel, | ||
| 127 | + gfx_screenWidth() / 2 + x2_barrel, | ||
| 128 | + gfx_screenHeight() - y2_barrel, YELLOW); | ||
| 129 | + | ||
| 130 | + for (int i = 0; i < num_targets; ++i) | ||
| 131 | + draw_target(&(t[i])); | ||
| 132 | + | ||
| 133 | + | ||
| 134 | + int x_bullet = bullet_distance * cos(fire_angle); | ||
| 135 | + int y_bullet = bullet_distance * sin(fire_angle); | ||
| 136 | + int x_bullet_pot = gfx_screenWidth() / 2 + x_bullet; | ||
| 137 | + int y_bullet_pot = gfx_screenHeight() - y_bullet; | ||
| 138 | + | ||
| 139 | + if (is_shooting) { | ||
| 140 | + // if(!is_explosion) | ||
| 141 | + gfx_filledCircle(gfx_screenWidth() / 2 + x_bullet, | ||
| 142 | + gfx_screenHeight() - y_bullet, 10, RED); // pocisk | ||
| 143 | + | ||
| 144 | + | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + gfx_updateScreen(); | ||
| 148 | + | ||
| 149 | + if (is_shooting) | ||
| 150 | + { | ||
| 151 | + for (int i = 0; i < num_targets; ++i) { | ||
| 152 | + if (hypot(x_bullet_pot - t[i].x, y_bullet_pot - t[i].y) < 50) { | ||
| 153 | + t[i].is_explosion = 1; | ||
| 154 | + t[i].explosion_counter = 0; | ||
| 155 | + } | ||
| 156 | + } | ||
| 157 | + bullet_distance += 10.0; | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + for (int i = 0; i < num_targets; ++i) | ||
| 161 | + move_target(&(t[i])); | ||
| 162 | + | ||
| 163 | + if (gfx_isKeyDown(SDLK_RIGHT)) | ||
| 164 | + angle -= 1.0 * (M_PI / 180.0); | ||
| 165 | + if (gfx_isKeyDown(SDLK_LEFT)) | ||
| 166 | + angle += 1.0 * (M_PI / 180.0); | ||
| 167 | + if (gfx_isKeyDown(SDLK_SPACE)) { | ||
| 168 | + is_shooting = 1; | ||
| 169 | + bullet_distance = 170.0; | ||
| 170 | + fire_angle = angle; | ||
| 171 | + } | ||
| 172 | + SDL_Delay(10); | ||
| 173 | + }; | ||
| 174 | + return 0; | ||
| 175 | +} |
06-multiple_targets/makefile
0 → 100755
| 1 | +TARGET=cannon | ||
| 2 | + | ||
| 3 | +all: $(TARGET) | ||
| 4 | + | ||
| 5 | +%.o:%.c | ||
| 6 | + gcc -g -I.. -Wall -pedantic `sdl2-config --cflags` -c $< -o $@ | ||
| 7 | + | ||
| 8 | +%: %.c ../primlib.o %.c | ||
| 9 | + gcc -g -I.. -Wall -pedantic `sdl2-config --cflags` ../primlib.o $< -o $@ -lSDL2_gfx `sdl2-config --libs` -lm | ||
| 10 | + | ||
| 11 | +../primlib.o: ../primlib.c ../primlib.h | ||
| 12 | + | ||
| 13 | +$(TARGET): $(TARGET).c ../primlib.h | ||
| 14 | + | ||
| 15 | +clean: | ||
| 16 | + -rm $(TARGET) | ||
| 0 | \ No newline at end of file | 17 | \ No newline at end of file |