Commit f80727e98f7217f2ffd6292b0b89b02635eac832

Authored by Grzegorz Jabłoński
1 parent 657a76aa

Drawing moving bullet

03-bullet/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 = 170.0;
  17 + while(1)
  18 + {
  19 + double delta_angle= 2.0 * (M_PI/180.0);
  20 + int x1 = 150 * cos(angle-delta_angle);
  21 + int y1 = 150 * sin(angle-delta_angle);
  22 + int x2 = 150 * cos(angle+delta_angle);
  23 + int y2 = 150 * sin(angle+delta_angle);
  24 + gfx_filledRect(0, 0, gfx_screenWidth() - 1, gfx_screenHeight() - 1, BLUE);
  25 + gfx_filledCircle(gfx_screenWidth() / 2, gfx_screenHeight(), 100, YELLOW);
  26 + gfx_filledTriangle(gfx_screenWidth() / 2, gfx_screenHeight() , gfx_screenWidth() / 2 + x1, gfx_screenHeight() - y1,
  27 + gfx_screenWidth() / 2 + x2, gfx_screenHeight() - y2, YELLOW);
  28 +
  29 + int x_bullet = bullet_distance * cos(angle);
  30 + int y_bullet = bullet_distance * sin(angle);
  31 +
  32 + gfx_filledCircle(gfx_screenWidth() / 2 + x_bullet, gfx_screenHeight() - y_bullet, 10, RED);
  33 +
  34 + gfx_updateScreen();
  35 +
  36 + bullet_distance += 1.0;
  37 +
  38 + if(gfx_isKeyDown(SDLK_RIGHT))
  39 + angle -= 1.0 * (M_PI/180.0);
  40 + if(gfx_isKeyDown(SDLK_LEFT))
  41 + angle += 1.0 * (M_PI/180.0);
  42 + SDL_Delay(100);
  43 + };
  44 + return 0;
  45 +}
... ...
03-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 +%: %.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
... ...