Commit 6819eba7591e99a6bbdf1cdc0a4cd03ad2016d9c
1 parent
9a9693c1
Shooting triggered by space
Showing
2 changed files
with
73 additions
and
0 deletions
04-shoot/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 | + while(1) | ||
| 20 | + { | ||
| 21 | + double delta_angle= 2.0 * (M_PI/180.0); | ||
| 22 | + int x1 = 150 * cos(angle-delta_angle); | ||
| 23 | + int y1 = 150 * sin(angle-delta_angle); | ||
| 24 | + int x2 = 150 * cos(angle+delta_angle); | ||
| 25 | + int y2 = 150 * sin(angle+delta_angle); | ||
| 26 | + gfx_filledRect(0, 0, gfx_screenWidth() - 1, gfx_screenHeight() - 1, BLUE); | ||
| 27 | + gfx_filledCircle(gfx_screenWidth() / 2, gfx_screenHeight(), 100, YELLOW); | ||
| 28 | + gfx_filledTriangle(gfx_screenWidth() / 2, gfx_screenHeight() , gfx_screenWidth() / 2 + x1, gfx_screenHeight() - y1, | ||
| 29 | + gfx_screenWidth() / 2 + x2, gfx_screenHeight() - y2, YELLOW); | ||
| 30 | + | ||
| 31 | + if(is_shooting) | ||
| 32 | + { | ||
| 33 | + int x_bullet = bullet_distance * cos(fire_angle); | ||
| 34 | + int y_bullet = bullet_distance * sin(fire_angle); | ||
| 35 | + | ||
| 36 | + gfx_filledCircle(gfx_screenWidth() / 2 + x_bullet, gfx_screenHeight() - y_bullet, 10, RED); | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + gfx_updateScreen(); | ||
| 40 | + | ||
| 41 | + if(is_shooting) | ||
| 42 | + bullet_distance += 1.0; | ||
| 43 | + | ||
| 44 | + if(gfx_isKeyDown(SDLK_RIGHT)) | ||
| 45 | + angle -= 1.0 * (M_PI/180.0); | ||
| 46 | + if(gfx_isKeyDown(SDLK_LEFT)) | ||
| 47 | + angle += 1.0 * (M_PI/180.0); | ||
| 48 | + if(gfx_isKeyDown(SDLK_SPACE)) | ||
| 49 | + { | ||
| 50 | + is_shooting = 1; | ||
| 51 | + bullet_distance = 170.0; | ||
| 52 | + fire_angle = angle; | ||
| 53 | + } | ||
| 54 | + SDL_Delay(100); | ||
| 55 | + }; | ||
| 56 | + return 0; | ||
| 57 | +} |
04-shoot/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 |