diff options
author | davidovski <david@sendula.com> | 2021-04-20 20:34:53 +0100 |
---|---|---|
committer | davidovski <david@sendula.com> | 2021-04-20 20:34:53 +0100 |
commit | e3fc1ec1a0761a355cb6a839cba8db0200e77a0e (patch) | |
tree | b11c3268efa35ae3d2c70e2d533b02706ce210bf |
initial commit
-rw-r--r-- | Makefile | 11 | ||||
-rw-r--r-- | asteroids.c | 89 |
2 files changed, 100 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b06d04f --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +PROG=asteroids +CC=gcc +FLAGS=-lm `pkg-config -libs raylib` + +.DEFAULT_GOAL := run + +${PROG}: ${PROG}.c + ${CC} ${PROG}.c -o ${PROG} ${FLAGS} + +run: ${PROG} + ./${PROG} diff --git a/asteroids.c b/asteroids.c new file mode 100644 index 0000000..d1c3b27 --- /dev/null +++ b/asteroids.c @@ -0,0 +1,89 @@ +#include <stdio.h> +#include <stdlib.h> +#include <raylib.h> +#include <math.h> + +const int height = 800; +const int width = 800; +const int fps = 60; + +const Color bg = BLACK; +const Color fg = GREEN; + +const float NOSE = PI/8; +const int LENGTH = 36; +const float THRUST = 0.1; +const int SKY_SIZE = 256; + +Vector2 pos = {(float) height / 2, (float) width / 2}; +Vector2 vel = {0.0f, 0.0f}; +float angle = 0; + +Vector2 * sky; + +Vector2 * generateSky() { + Vector2 s[SKY_SIZE]; + for (int i = 0; i < SKY_SIZE; i++) { + s[i] = (Vector2){rand() % width, rand() % height}; + } + return s; +} + +Vector2 wingpos(int w) { + // optimise this maths + int x, y; + float a; + + if (w > 0) a = angle + (NOSE / 2.0f); + else if (w < 0) a = angle - (NOSE / 2.0f); + + x = pos.x - LENGTH * sin(a); + y = pos.y - LENGTH * cos(a); + return (Vector2){x, y}; +} + +int accelerate(float m) { + vel.x += sin(angle) * m; + vel.y += cos(angle) * m; +} + +int drawPlayer() { + DrawTriangleLines( + pos, + wingpos(-1), + wingpos(1), + fg + ); +} + +int draw() { + BeginDrawing(); + ClearBackground(BLACK); + + drawPlayer(); + EndDrawing(); +} + +int update() { + if (IsKeyDown(KEY_RIGHT)) angle -= 0.1f; + if (IsKeyDown(KEY_LEFT)) angle += 0.1f; + + if (IsKeyDown(KEY_UP)) accelerate(THRUST); + + + pos.x += vel.x; + pos.y += vel.y; +} + +int main() { + sky = generateSky(); + InitWindow(width, height, "game"); + SetTargetFPS(60); + + while (!WindowShouldClose()) { + update(); + draw(); + } + CloseWindow(); + return 0; +} |