Commit 920a05c32f0809a09e81501e545f8171e8f08aef
1 parent
6819eba7
Added moving target
Showing
2 changed files
with
83 additions
and
0 deletions
05-target/cannon.c
0 → 100644
| 1 | +#include "primlib.h" | |
| 2 | +#include <stdlib.h> | |
| 3 | +#include <unistd.h> | |
| 4 | + | |
| 5 | +int main(int argc, char *argv[]) { | |
| 6 | + if (gfx_init()) | |
| 7 | + exit(3); | |
| 8 | + | |
| 9 | + gfx_filledRect(0, 0, gfx_screenWidth() - 1, gfx_screenHeight() - 1, BLUE); | |
| 10 | + gfx_filledCircle(gfx_screenWidth() / 2, gfx_screenHeight(), 100, YELLOW); | |
| 11 | + gfx_line(gfx_screenWidth() / 2, gfx_screenHeight() , gfx_screenWidth() / 2, gfx_screenHeight() - 150, YELLOW); | |
| 12 | + gfx_updateScreen(); | |
| 13 | + | |
| 14 | + double angle = 90.0 * (M_PI/180.0); | |
| 15 | + | |
| 16 | + double bullet_distance; | |
| 17 | + int is_shooting = 0; | |
| 18 | + double fire_angle; | |
| 19 | + | |
| 20 | + int target_x = 0; | |
| 21 | + int target_y = 40; | |
| 22 | + while(1) | |
| 23 | + { | |
| 24 | + double delta_angle= 2.0 * (M_PI/180.0); | |
| 25 | + int x1 = 150 * cos(angle-delta_angle); | |
| 26 | + int y1 = 150 * sin(angle-delta_angle); | |
| 27 | + int x2 = 150 * cos(angle+delta_angle); | |
| 28 | + int y2 = 150 * sin(angle+delta_angle); | |
| 29 | + gfx_filledRect(0, 0, gfx_screenWidth() - 1, gfx_screenHeight() - 1, BLUE); | |
| 30 | + gfx_filledCircle(gfx_screenWidth() / 2, gfx_screenHeight(), 100, YELLOW); | |
| 31 | + gfx_filledTriangle(gfx_screenWidth() / 2, gfx_screenHeight() , gfx_screenWidth() / 2 + x1, gfx_screenHeight() - y1, | |
| 32 | + gfx_screenWidth() / 2 + x2, gfx_screenHeight() - y2, YELLOW); | |
| 33 | + | |
| 34 | + | |
| 35 | + gfx_filledRect(target_x - 10, target_y - 10, target_x + 10, target_y + 10, MAGENTA); | |
| 36 | + | |
| 37 | + if(is_shooting) | |
| 38 | + { | |
| 39 | + int x_bullet = bullet_distance * cos(fire_angle); | |
| 40 | + int y_bullet = bullet_distance * sin(fire_angle); | |
| 41 | + | |
| 42 | + gfx_filledCircle(gfx_screenWidth() / 2 + x_bullet, gfx_screenHeight() - y_bullet, 10, RED); | |
| 43 | + } | |
| 44 | + | |
| 45 | + gfx_updateScreen(); | |
| 46 | + | |
| 47 | + if(is_shooting) | |
| 48 | + bullet_distance += 5.0; | |
| 49 | + | |
| 50 | + target_x += 3; | |
| 51 | + if(target_x > gfx_screenWidth()) | |
| 52 | + target_x = 0; | |
| 53 | + | |
| 54 | + if(gfx_isKeyDown(SDLK_RIGHT)) | |
| 55 | + angle -= 1.0 * (M_PI/180.0); | |
| 56 | + if(gfx_isKeyDown(SDLK_LEFT)) | |
| 57 | + angle += 1.0 * (M_PI/180.0); | |
| 58 | + if(gfx_isKeyDown(SDLK_SPACE)) | |
| 59 | + { | |
| 60 | + is_shooting = 1; | |
| 61 | + bullet_distance = 170.0; | |
| 62 | + fire_angle = angle; | |
| 63 | + } | |
| 64 | + SDL_Delay(100); | |
| 65 | + }; | |
| 66 | + return 0; | |
| 67 | +} | ... | ... |
05-target/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 | 17 | \ No newline at end of file | ... | ... |