Commit 313be3d4e2571d8c578f16333c0492bac90a9c6d
1 parent
4e849b52
Added intermediate step
Showing
8 changed files
with
124 additions
and
0 deletions
06-multiple-targets/cannon.c
0 → 100644
| 1 | +#include "primlib.h" | |
| 2 | +#include <stdlib.h> | |
| 3 | + | |
| 4 | +int main() { | |
| 5 | + if (gfx_init()) | |
| 6 | + exit(3); | |
| 7 | + | |
| 8 | + double angle = 90.0 * (M_PI / 180.0); | |
| 9 | + | |
| 10 | + double bullet_distance; | |
| 11 | + int is_shooting = 0; | |
| 12 | + double fire_angle; | |
| 13 | + | |
| 14 | + struct target_data { | |
| 15 | + int x; | |
| 16 | + int y; | |
| 17 | + int dir; | |
| 18 | + enum color c; | |
| 19 | + int speed; | |
| 20 | + int size; | |
| 21 | + }; | |
| 22 | + | |
| 23 | + struct target_data td[3]; | |
| 24 | + | |
| 25 | + td[0].x = 300; | |
| 26 | + td[0].y = 40; | |
| 27 | + td[0].c = MAGENTA; | |
| 28 | + td[0].speed = 3; | |
| 29 | + td[0].size = 8; | |
| 30 | + td[0].dir = -1; | |
| 31 | + | |
| 32 | + td[1].x = 0; | |
| 33 | + td[1].y = 70; | |
| 34 | + td[1].c = YELLOW; | |
| 35 | + td[1].speed = 2; | |
| 36 | + td[1].size = 10; | |
| 37 | + td[1].dir = +1; | |
| 38 | + | |
| 39 | + td[2].x = 0; | |
| 40 | + td[2].y = 100; | |
| 41 | + td[2].c = BLACK; | |
| 42 | + td[2].speed = 4; | |
| 43 | + td[2].size = 15; | |
| 44 | + td[2].dir = +1; | |
| 45 | + | |
| 46 | + while (1) { | |
| 47 | + double delta_angle = 2.0 * (M_PI / 180.0); | |
| 48 | + int x1_barrel = 150 * cos(angle - delta_angle); | |
| 49 | + int y1_barrel = 150 * sin(angle - delta_angle); | |
| 50 | + int x2_barrel = 150 * cos(angle + delta_angle); | |
| 51 | + int y2_barrel = 150 * sin(angle + delta_angle); | |
| 52 | + gfx_filledRect(0, 0, gfx_screenWidth() - 1, gfx_screenHeight() - 1, BLUE); | |
| 53 | + gfx_filledCircle(gfx_screenWidth() / 2, gfx_screenHeight(), 100, YELLOW); | |
| 54 | + gfx_filledTriangle(gfx_screenWidth() / 2, gfx_screenHeight(), | |
| 55 | + gfx_screenWidth() / 2 + x1_barrel, | |
| 56 | + gfx_screenHeight() - y1_barrel, | |
| 57 | + gfx_screenWidth() / 2 + x2_barrel, | |
| 58 | + gfx_screenHeight() - y2_barrel, YELLOW); | |
| 59 | + | |
| 60 | + { // display targets | |
| 61 | + int i; | |
| 62 | + for (i = 0; i < sizeof(td) / sizeof(td[0]); ++i) { | |
| 63 | + gfx_filledRect(td[i].x - td[i].size, td[i].y - td[i].size, | |
| 64 | + td[i].x + td[i].size, td[i].y + td[i].size, td[i].c); | |
| 65 | + } | |
| 66 | + } | |
| 67 | + | |
| 68 | + if (is_shooting) { | |
| 69 | + int x_bullet = bullet_distance * cos(fire_angle); | |
| 70 | + int y_bullet = bullet_distance * sin(fire_angle); | |
| 71 | + | |
| 72 | + gfx_filledCircle(gfx_screenWidth() / 2 + x_bullet, | |
| 73 | + gfx_screenHeight() - y_bullet, 10, RED); | |
| 74 | + } | |
| 75 | + | |
| 76 | + gfx_updateScreen(); | |
| 77 | + | |
| 78 | + if (is_shooting) | |
| 79 | + bullet_distance += 5.0; | |
| 80 | + | |
| 81 | + { // move targets | |
| 82 | + int i; | |
| 83 | + for (i = 0; i < sizeof(td) / sizeof(td[0]); ++i) { | |
| 84 | + td[i].x += td[i].speed * td[i].dir; | |
| 85 | + | |
| 86 | + if (td[i].x > gfx_screenWidth()) { | |
| 87 | + td[i].dir = -1; | |
| 88 | + } | |
| 89 | + | |
| 90 | + if (td[i].x < 0) { | |
| 91 | + td[i].dir = +1; | |
| 92 | + } | |
| 93 | + } | |
| 94 | + } | |
| 95 | + | |
| 96 | + if (gfx_isKeyDown(SDLK_RIGHT)) | |
| 97 | + angle -= 1.0 * (M_PI / 180.0); | |
| 98 | + if (gfx_isKeyDown(SDLK_LEFT)) | |
| 99 | + angle += 1.0 * (M_PI / 180.0); | |
| 100 | + if (gfx_isKeyDown(SDLK_SPACE)) { | |
| 101 | + is_shooting = 1; | |
| 102 | + bullet_distance = 170.0; | |
| 103 | + fire_angle = angle; | |
| 104 | + } | |
| 105 | + SDL_Delay(10); | |
| 106 | + }; | |
| 107 | + return 0; | |
| 108 | +} | ... | ... |
06-multiple_targets/makefile renamed to 06-multiple-targets/makefile
06-multiple_targets/cannon.c renamed to 07-multiple_targets_functions/cannon.c
07-tapered_bullet/makefile renamed to 07-multiple_targets_functions/makefile
07-tapered_bullet/bullet.png renamed to 08-tapered_bullet/bullet.png
88.4 KB
07-tapered_bullet/bullet.svg renamed to 08-tapered_bullet/bullet.svg
07-tapered_bullet/cannon.c renamed to 08-tapered_bullet/cannon.c
08-tapered_bullet/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 | +$(TARGET): $(TARGET).o ../primlib.o | |
| 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).o: $(TARGET).c ../primlib.h | |
| 14 | + | |
| 15 | +clean: | |
| 16 | + -rm $(TARGET) $(TARGET).o ../primlib.o | |
| 0 | 17 | \ No newline at end of file | ... | ... |