From 467490b7653fb3bcd94104f2cdc66a8f0d8bdd04 Mon Sep 17 00:00:00 2001 From: davidovski Date: Tue, 20 Apr 2021 21:04:08 +0100 Subject: added stars and torus like screen behaviour --- Makefile | 3 ++- asteroids.c | 56 +++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index b06d04f..4cc1460 100644 --- a/Makefile +++ b/Makefile @@ -8,4 +8,5 @@ ${PROG}: ${PROG}.c ${CC} ${PROG}.c -o ${PROG} ${FLAGS} run: ${PROG} - ./${PROG} + ./${PROG} + diff --git a/asteroids.c b/asteroids.c index d1c3b27..58b4ae6 100644 --- a/asteroids.c +++ b/asteroids.c @@ -3,8 +3,8 @@ #include #include -const int height = 800; const int width = 800; +const int height = 800; const int fps = 60; const Color bg = BLACK; @@ -13,24 +13,23 @@ const Color fg = GREEN; const float NOSE = PI/8; const int LENGTH = 36; const float THRUST = 0.1; -const int SKY_SIZE = 256; + + +typedef struct Asteroid{ + Vector2 pos; + Vector2 velocity; + int size +}; + +Vector2 sky[256]; +Vector2 asteroids[64]; 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; @@ -42,42 +41,57 @@ Vector2 wingpos(int w) { 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), + wingpos(1), fg ); } + int draw() { BeginDrawing(); ClearBackground(BLACK); + for (int i = 0; i < 256; i++) { + DrawPixelV(sky[i], fg); + } drawPlayer(); EndDrawing(); } +int clamp(Vector2 *vec) { + if (vec->x > width+LENGTH) vec->x -= width+LENGTH; + if (vec->x < -LENGTH) vec->x += width; + if (vec->y > height+LENGTH) vec->y -= height+LENGTH; + if (vec->y < -LENGTH) vec->y += height; +} + int update() { if (IsKeyDown(KEY_RIGHT)) angle -= 0.1f; if (IsKeyDown(KEY_LEFT)) angle += 0.1f; - if (IsKeyDown(KEY_UP)) accelerate(THRUST); - + if (IsKeyDown(KEY_UP)) { + vel.x += sin(angle) * THRUST; + vel.y += cos(angle) * THRUST; + } pos.x += vel.x; pos.y += vel.y; + clamp(&pos); } int main() { - sky = generateSky(); + for (int i = 0; i < 256; i++) { + sky[i] = (Vector2){rand() % width, rand() % height}; + } + InitWindow(width, height, "game"); + + Vector2 m = GetMonitorPosition(1); + SetWindowPosition(m.x, m.y); SetTargetFPS(60); while (!WindowShouldClose()) { -- cgit v1.2.1