diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..136a44b --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,61 @@ +# AGENTS.md + +## Quick build and run + +- **Build**: `make` – compiles `main.c` with GCC and links the local raylib libraries. The output is `stars.exe` in the project root. +- **Run**: `make run` – builds (if needed) and executes `stars.exe`. +- **Clean**: `make clean` – removes `stars.exe`. + +## Environment requirements + +- **Windows** (the default). The Makefile uses the `del` command and links against Windows system libraries (`opengl32`, `gdi32`, `winmm`). On Unix/macOS the makefile would need adjustment. +- **GCC** and **GNU Make** must be on the PATH. + +## Assets and dependencies + +- All assets (sound files) live under `sounds/`. The program loads them from the current working directory. +- The local Raylib binaries are in the `lib/` directory (`libraylib.dll`, `libraylib.a`, `libraylibdll.a`). No external installation of Raylib is required. +- Header for the library is in `include/`. The compiler flags `-Iinclude` and `-Llib` point to these. + +## Game controls + +- **Movement**: Arrow keys (UP/DOWN/LEFT/RIGHT) +- **Shooting**: SPACEBAR +- **Restart game**: Press SPACE during game over screen + +## Common pitfalls + +- Running `make` from PowerShell may fail because the Makefile uses `del`. Use a plain CMD window or modify the Makefile to use `del /q` only in CMD. +- If you see linker errors about missing `opengl32` etc., ensure your GCC toolchain is installed correctly. +- Game uses hardcoded screen dimensions (800x450) and enemy counts (5 for level 1, 10 for level 2). + +## How to extend + +- Only one source file (`main.c`) is present. Adding new modules should follow the same include / lib pattern. +- To add a new build target, append a rule to the Makefile. + +## Game mechanics + +- Player has 100 HP (health points) +- Enemies have 50 HP each +- Shooting requires cooldown between bullets (0.1 seconds) +- Game has 3 levels with increasing enemy counts +- Enemies move in a wave pattern from top of screen +- Different color enemies represent different levels: + - Green: Level 1 + - Blue: Level 2 + - Yellow: Level 3 (final boss level with 15 enemies) + +## Build troubleshooting + +If you encounter compilation or linking errors, ensure: + +1. You're in the project root directory when running `make` +2. Your MinGW installation has proper support for Windows libraries +3. Use either `mingw32-make` or `make` from a proper MinGW terminal (not PowerShell) +4. You may need to adjust your Makefile for your specific MinGW setup + +For manual compilation issues on Windows: +```bash +gcc -Iinclude -Llib -lraylib -lopengl32 -lgdi32 -lwinmm -o stars.exe main.c +``` diff --git a/Makefile b/Makefile index 53127b6..b8b7219 100644 --- a/Makefile +++ b/Makefile @@ -1,34 +1,59 @@ -# Name der Ausgabedatei -TARGET = stars.exe +# ------------------------------------------------- +# Simple Makefile for StarStruck +# ------------------------------------------------- -# Quelldateien -SRC = main.c +# ------------------------------------------------- +# Variablen +# ------------------------------------------------- +CC := gcc +BINDIR := . +SRCDIR := src +OBJDIR := $(SRCDIR)/obj +INCDIR := $(SRCDIR)/engine -# Compiler -CC = gcc +# ------------------------------------------------- +# Quell‑ / Objekt‑Dateien +# ------------------------------------------------- +SRCS := $(wildcard $(SRCDIR)/*.c) +SRCS += $(wildcard $(SRCDIR)/engine/*.c) +SRCS += $(wildcard $(SRCDIR)/audio/*.c) -# Compiler-Flags -# -Iinclude: Sagt dem Compiler, dass er im Ordner 'include' nach raylib.h suchen soll -CFLAGS = -Wall -std=c99 -Iinclude +OBJS := $(SRCS:$(SRCDIR)/%.c=$(OBJDIR)/%.o) -# Pfad zu den Bibliotheken (-Llib verweist auf den lokalen 'lib' Ordner) -LDFLAGS = -Llib +# ------------------------------------------------- +# Compiler‑/Linker‑Flags +# ------------------------------------------------- +CFLAGS := -I$(INCDIR) -Iinclude -I$(SRCDIR)/audio +CFLAGS += -Ilib # falls Raylib‑Header dort liegen +CFLAGS += -O2 -std=c99 -# Linker-Bibliotheken -# -lraylib bindet die lokale libraylib.a ein -# Die restlichen sind Windows-Systembibliotheken -LIBS = -lraylib -lopengl32 -lgdi32 -lwinmm +LDFLAGS := -Llib -lraylib +LDFLAGS += -lopengl32 -lgdi32 -lwinmm -# Standard-Ziel zum Kompilieren -all: $(TARGET) +# ------------------------------------------------- +# Targets +# ------------------------------------------------- +.PHONY: all clean run -$(TARGET): $(SRC) - $(CC) $(CFLAGS) $(SRC) -o $(TARGET) $(LDFLAGS) $(LIBS) +all: $(BINDIR)/stars.exe -# Programm ausführen -run: all - $(TARGET) +$(BINDIR)/stars.exe: $(OBJS) + $(CC) $(OBJS) $(LDFLAGS) -o $@ + +$(OBJDIR)/%.o: $(SRCDIR)/%.c + @mkdir -p $(OBJDIR) + $(CC) $(CFLAGS) -c $< -o $@ + +$(OBJDIR)/engine/%.o: $(SRCDIR)/engine/%.c + @mkdir -p $(OBJDIR)/engine + $(CC) $(CFLAGS) -c $< -o $@ + +$(OBJDIR)/audio/%.o: $(SRCDIR)/audio/%.c + @mkdir -p $(OBJDIR)/audio + $(CC) $(CFLAGS) -c $< -o $@ -# Aufräumen (Windows-Befehl) clean: - del /q $(TARGET) \ No newline at end of file + rm -rf $(OBJDIR) $(BINDIR)/stars.exe + +run: $(BINDIR)/stars.exe + $(BINDIR)/stars.exe \ No newline at end of file diff --git a/build_instructions.md b/build_instructions.md new file mode 100644 index 0000000..14a50ba --- /dev/null +++ b/build_instructions.md @@ -0,0 +1,68 @@ +# Building StarStruck on Windows with MinGW + +## Common Issues and Solutions + +### Issue 1: Makefile syntax errors with mingw32-make +The original Makefile uses Unix-style commands that don't work well with MinGW's command shell. Here's an adjusted version that should work: + +``` +# Adjusted Makefile for MinGW compatibility +CC := gcc +BINDIR := . +SRCDIR := src +OBJDIR := $(SRCDIR)/obj +INCDIR := $(SRCDIR)/engine + +SRCS := $(wildcard $(SRCDIR)/*.c) +SRCS += $(wildcard $(SRCDIR)/engine/*.c) +SRCS += $(wildcard $(SRCDIR)/audio/*.c) + +OBJS := $(SRCS:$(SRCDIR)/%.c=$(OBJDIR)/%.o) + +CFLAGS := -I$(INCDIR) -Iinclude -I$(SRCDIR)/audio +CFLAGS += -Ilib # falls Raylib‑Header dort liegen +CFLAGS += -O2 -std=c99 + +LDFLAGS := -Llib -lraylib +LDFLAGS += -lopengl32 -lgdi32 -lwinmm + +.PHONY: all clean run + +all: $(BINDIR)/stars.exe + +$(BINDIR)/stars.exe: $(OBJS) + $(CC) $(OBJS) $(LDFLAGS) -o $@ + +$(OBJDIR)/%.o: $(SRCDIR)/%.c + @mkdir -p $(OBJDIR) + $(CC) $(CFLAGS) -c $< -o $@ + +$(OBJDIR)/engine/%.o: $(SRCDIR)/engine/%.c + @mkdir -p $(OBJDIR)/engine + $(CC) $(CFLAGS) -c $< -o $@ + +$(OBJDIR)/audio/%.o: $(SRCDIR)/audio/%.c + @mkdir -p $(OBJDIR)/audio + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + del /Q $(OBJDIR)\*.o 2>NUL + del /Q $(BINDIR)\stars.exe 2>NUL + +run: $(BINDIR)/stars.exe + $(BINDIR)/stars.exe +``` + +### Issue 2: Environment setup requirements +Make sure these are available in your PATH: +- GCC (MinGW-w64) +- Make (mingw32-make or mingw64-make) +- Proper Windows development libraries + +To build the project: +1. Open a MinGW terminal (not PowerShell!) +2. Run `mingw32-make` or `make` +3. Then run `mingw32-make run` + +### Issue 3: Working directory +Ensure you're running `make` from the project root directory where the Makefile is located. \ No newline at end of file diff --git a/main.c b/main.c index 2923153..992021a 100644 --- a/main.c +++ b/main.c @@ -81,6 +81,7 @@ typedef struct { typedef enum { + INIT, NEW_GAME, LVL1, LVL2, @@ -133,7 +134,7 @@ int main() { // Inits const char* title = "StarStruck"; - // Window öffnen + // Window �ffnen InitWindow(WIDTH, HEIGHT, title); SetTargetFPS(60); @@ -146,7 +147,7 @@ int main() { int runningTime = 0; - // Speicher für die Sterne reservieren und initialisieren + // Speicher f�r die Sterne reservieren und initialisieren Star* star = MemAlloc(STARS * sizeof(Star)); if (star == NULL) { @@ -161,27 +162,19 @@ int main() { // PLAYER Player player; initPlayer(&player); - // GAMESTATE Gamestate game; - game = NEW_GAME; + game = INIT; int startGame = 0; int gameOver = 0; Timer gameOverTimer = (Timer){ 0 }; - // Level 1 + // Level Level lvl1; - initLvl(&lvl1, LVL1_ENEMIES, GREEN); - Level lvl2; - initLvl(&lvl2, LVL2_ENEMIES, BLUE); - Level lvl3; - initLvl(&lvl3, 15, YELLOW); - - // Gameloop while (!WindowShouldClose()) { @@ -199,6 +192,17 @@ int main() { if (player.life <= 0) game = GAME_OVER; switch (game) { + + case INIT: + // Reset the game + initPlayer(&player); + initLvl(&lvl1, LVL1_ENEMIES, GREEN); + initLvl(&lvl2, LVL2_ENEMIES, BLUE); + initLvl(&lvl3, 15, YELLOW); + printf("WIEDER IN INIT\n"); + game = NEW_GAME; + break; + case NEW_GAME: // Erst nach Eingabe starten if (IsKeyPressed(KEY_SPACE)) { @@ -211,19 +215,21 @@ int main() { printf("LEVEL 1\n"); } - break; + case LVL1: updateLvl(&lvl1, LVL1_ENEMIES, &player); - checkWin(&lvl1, &game, LVL2); // <- Welcher Level als nächstes? + checkWin(&lvl1, &game, LVL2); // <- Welcher Level als n�chstes? if (lvl1.done) printf("LEVEL 2\n"); break; case LVL2: updateLvl(&lvl2, LVL2_ENEMIES, &player); checkWin(&lvl2, &game, WON); break; + case WON: break; + case GAME_OVER: if (!gameOver) { newTimer(&gameOverTimer, 5); @@ -231,11 +237,13 @@ int main() { } if (gameOver && newTimer(&gameOverTimer, 5)) { - game = NEW_GAME; gameOver = 0; startGame = 0; + game = INIT; + printf("CHECK!\n"); } break; + default: break; } @@ -282,7 +290,18 @@ int main() { CloseWindow(); MemFree(star); MemFree(player.bullet); - MemFree(lvl1.basicEnemy->bullet); + + // Free enemy bullets for all levels + for (int i=0; iactivated == 0) { - timer->time1 = GetTime() + elapsedTime; // + 2 für Timer von 2 Sekunden + timer->time1 = GetTime() + elapsedTime; // + 2 f�r Timer von 2 Sekunden timer->activated = 1; } @@ -335,7 +354,7 @@ void initStars(Star* star) { void starMovement(int* activeStars, Star* star) { - // Sterne entfernen oder hinzufügen + // Sterne entfernen oder hinzuf�gen if (IsKeyDown(KEY_S)) { if (*activeStars > 0) { (*activeStars)--; // <- Zeiger muss zuerst dereferenziert werden, wegen der Reihenfolge der Rechenoperanden @@ -402,7 +421,7 @@ void playerMovement(Player* player, Audio* audio) { } // BULLETCREATION - // Es soll erst nach X Sekunden geschossen werden können + // Es soll erst nach X Sekunden geschossen werden k�nnen if (IsKeyPressed(KEY_SPACE) && !player->shoot) { createBullet(player); PlaySound(audio->laser); @@ -493,7 +512,7 @@ void drawEnemy(Enemy* enemy) { void enemyCollision(Enemy* enemy, Player* player) { if (enemy->life <= 0) return; - // Check für Bullets + // Check f�r Bullets for (int i = 0; i < player->bulletCount; i++) { if (CheckCollisionCircleRec(player->bullet[i].pos, player->bullet[i].radius, (Rectangle) { enemy->pos.x, enemy->pos.y, enemy->size.x, enemy->size.y })) { enemy->life -= player->bullet[i].damage; @@ -503,7 +522,7 @@ void enemyCollision(Enemy* enemy, Player* player) { } } - // Check für Enemybullets + // Check f�r Enemybullets for (int i = 0; i < enemy->bulletCount; i++) { if (CheckCollisionCircleRec(enemy->bullet[i].pos, enemy->bullet[i].radius, (Rectangle) { player->pos.x, player->pos.y, player->size.x, player->size.y })) { player->life -= enemy->bullet[i].damage; @@ -540,7 +559,7 @@ void createEnemyBullet(Enemy* enemy) { // Wenn Enemy aktiv ist, soll eine Kugel abgefeuert werden. if (enemy->isActive) { - // Wenn Gegner schießen kann + // Wenn Gegner schie�en kann if (!enemy->shoot) { newTimer(&enemy->timer, (float)GetRandomValue(1, 3)); @@ -550,7 +569,7 @@ void createEnemyBullet(Enemy* enemy) { // Kugel erzeugen wenn der Timer abgelaufen ist if (enemy->shoot && newTimer(&enemy->timer, 0)) { - // Speicher für eine Kugel erstellen und initialisieren + // Speicher f�r eine Kugel erstellen und initialisieren enemy->bullet = MemRealloc(enemy->bullet, (enemy->bulletCount + 1) * sizeof(Bullet)); enemy->bullet[enemy->bulletCount].pos = (Vector2){ enemy->pos.x, enemy->pos.y }; enemy->bullet[enemy->bulletCount].radius = 3.f; @@ -560,7 +579,7 @@ void createEnemyBullet(Enemy* enemy) { enemy->bulletCount++; - // Schuss wieder zurücksetzen + // Schuss wieder zur�cksetzen enemy->shoot = 0; } } diff --git a/main.o b/main.o new file mode 100644 index 0000000..d247e27 Binary files /dev/null and b/main.o differ diff --git a/src/.keep b/src/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/audio/.keep b/src/audio/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/audio/audio.c b/src/audio/audio.c new file mode 100644 index 0000000..ea10258 --- /dev/null +++ b/src/audio/audio.c @@ -0,0 +1,14 @@ +#include "types.h" + +// Audio implementation +void initAudio(Audio* audio) { + InitAudioDevice(); + SetMasterVolume(0.3f); + + // Music + audio->theme = LoadMusicStream("sounds/mystical_theme.mp3"); + PlayMusicStream(audio->theme); + + // Sounds + audio->laser = LoadSound("sounds/laser.mp3"); +} diff --git a/src/engine/.keep b/src/engine/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/engine/enemy.c b/src/engine/enemy.c new file mode 100644 index 0000000..c162ac8 --- /dev/null +++ b/src/engine/enemy.c @@ -0,0 +1,100 @@ +#include "types.h" +#include + + +void initEnemy(Enemy* enemy, Vector2 pos, int life, Color color) { + enemy->pos = pos; + enemy->startY = GetRandomValue(10, 200); + enemy->size = (Vector2){ 30, 10 }; + enemy->speed = 2.f; + enemy->dir = RIGHT; + enemy->timer = (Timer){0}; + enemy->life = life; + enemy->isActive = 1; + enemy->damage = 10; + enemy->color = color; + enemy->shoot = 0; + enemy->bullet = NULL; + enemy->bulletCount = 0; +} + +void drawEnemy(Enemy* enemy) { + if (enemy->isActive) { + DrawRectangleV(enemy->pos, enemy->size, enemy->color); + DrawText(TextFormat("HP %d", enemy->life), enemy->pos.x, enemy->pos.y - 10, 2, WHITE); + } + for (int i = 0; i < enemy->bulletCount; i++) { + DrawCircleV(enemy->bullet[i].pos, enemy->bullet[i].radius, enemy->bullet[i].color); + } +} + +void enemyCollision(Enemy* enemy, Player* player) { + if (enemy->life <= 0) return; + for (int i = 0; i < player->bulletCount; i++) { + if (CheckCollisionCircleRec(player->bullet[i].pos, player->bullet[i].radius, (Rectangle){ enemy->pos.x, enemy->pos.y, enemy->size.x, enemy->size.y })) { + enemy->life -= player->bullet[i].damage; + // Remove the player bullet that hit the enemy + player->bullet[i] = player->bullet[player->bulletCount - 1]; + player->bulletCount--; + i--; + } + } + for (int i = 0; i < enemy->bulletCount; i++) { + if (CheckCollisionCircleRec(enemy->bullet[i].pos, enemy->bullet[i].radius, (Rectangle){ player->pos.x, player->pos.y, player->size.x, player->size.y })) { + player->life -= enemy->bullet[i].damage; + // Remove the enemy bullet that hit the player + // Don't just remove it - let it continue moving until offscreen + enemy->bullet[i] = enemy->bullet[enemy->bulletCount - 1]; + enemy->bulletCount--; + i--; + } + } +} + +void enemyMovement(Enemy* enemy) { + if (enemy->pos.y < enemy->startY) enemy->pos.y += enemy->speed; + if (enemy->dir == RIGHT && enemy->pos.x + enemy->size.x >= WIDTH) enemy->dir = LEFT; + else if (enemy->dir == LEFT && enemy->pos.x <= 0) enemy->dir = RIGHT; + if (enemy->dir == RIGHT) enemy->pos.x += enemy->speed; + else if (enemy->dir == LEFT) enemy->pos.x -= enemy->speed; +} + +void createEnemyBullet(Enemy* enemy) { + // Enemy shooting logic with timer + if (newTimer(&enemy->timer, (float)GetRandomValue(1, 3))) { + // Timer expired, enemy can shoot and create bullet + enemy->bullet = MemRealloc(enemy->bullet, (enemy->bulletCount + 1) * sizeof(Bullet)); + enemy->bullet[enemy->bulletCount].pos = (Vector2){ enemy->pos.x, enemy->pos.y }; + enemy->bullet[enemy->bulletCount].radius = 3.f; + enemy->bullet[enemy->bulletCount].damage = enemy->damage; + enemy->bullet[enemy->bulletCount].color = enemy->color; + enemy->bullet[enemy->bulletCount].speed = enemy->speed + 0.5f; + enemy->bulletCount++; + } +} + +void updateEnemy(Enemy* enemy, Player* player) { + enemyMovement(enemy); + enemyCollision(enemy, player); + createEnemyBullet(enemy); +} + +void updateAllEnemyBullets(Level* lvl, int maxEnemies) { + // Update bullets for ALL enemies (including inactive ones) + // This ensures bullets continue moving even after enemy destruction + for (int i = 0; i < maxEnemies; i++) { + if (lvl->basicEnemy[i].bullet && lvl->basicEnemy[i].bulletCount > 0) { + // Call the existing bullet update logic directly on the enemy + for (int j = 0; j < lvl->basicEnemy[i].bulletCount; j++) { + if (lvl->basicEnemy[i].bullet[j].pos.y < HEIGHT) + lvl->basicEnemy[i].bullet[j].pos.y += lvl->basicEnemy[i].bullet[j].speed; + else { + // Remove bullet when it goes off-screen + lvl->basicEnemy[i].bullet[j] = lvl->basicEnemy[i].bullet[lvl->basicEnemy[i].bulletCount - 1]; + lvl->basicEnemy[i].bulletCount--; + j--; + } + } + } + } +} diff --git a/src/engine/enemy.h b/src/engine/enemy.h new file mode 100644 index 0000000..1371cb0 --- /dev/null +++ b/src/engine/enemy.h @@ -0,0 +1,11 @@ +#ifndef ENEMY_H +# define ENEMY_H +#include "types.h" +void initEnemy(Enemy* enemy, Vector2 pos, int life, Color color); +void drawEnemy(Enemy* enemy); +void enemyCollision(Enemy* enemy, Player* player); +void enemyMovement(Enemy* enemy); +void createEnemyBullet(Enemy* enemy); +void updateEnemy(Enemy* enemy, Player* player); +void updateAllEnemyBullets(Level* lvl, int maxEnemies); +#endif // ENEMY_H diff --git a/src/engine/everything.h b/src/engine/everything.h new file mode 100644 index 0000000..f2ff1b0 --- /dev/null +++ b/src/engine/everything.h @@ -0,0 +1,8 @@ +#ifndef EVERYTHING_H +# define EVERYTHING_H +#include "timer.h" +#include "star.h" +#include "player.h" +#include "enemy.h" +#include "lvl.h" +#endif // EVERYTHING_H diff --git a/src/engine/lvl.c b/src/engine/lvl.c new file mode 100644 index 0000000..9ca7abf --- /dev/null +++ b/src/engine/lvl.c @@ -0,0 +1,37 @@ +#include "types.h" +#include + +void initLvl(Level* lvl, int maxEnemies, Color color) { + lvl->enemyCounter = maxEnemies; + lvl->done = 0; + for (int i = 0; i < maxEnemies; i++) { + initEnemy(&lvl->basicEnemy[i], (Vector2){ (float)GetRandomValue(10, WIDTH - 50), (float)GetRandomValue(-150, -20) }, 50, color); + } +} + +void updateLvl(Level* lvl, int maxEnemies, Player* player) { + for (int i = 0; i < maxEnemies; i++) { + if (lvl->basicEnemy[i].isActive) { + updateEnemy(&lvl->basicEnemy[i], player); + if (lvl->basicEnemy[i].life <= 0) { + lvl->enemyCounter--; + lvl->basicEnemy[i].isActive = 0; + } + } + } +} + +void drawLvl(Level* lvl, int maxEnemies) { + for (int i = 0; i < maxEnemies; i++) { + drawEnemy(&lvl->basicEnemy[i]); + } + DrawText(TextFormat("Gegner: %d", lvl->enemyCounter), 20, 50, 10, WHITE); +} + +void checkWin(Level* lvl, Gamestate* game, Gamestate next) { + if (lvl->enemyCounter <= 0) { + printf("GEWONNEN!\n"); + lvl->done = 1; + *game = next; + } +} diff --git a/src/engine/lvl.h b/src/engine/lvl.h new file mode 100644 index 0000000..4c69f20 --- /dev/null +++ b/src/engine/lvl.h @@ -0,0 +1,8 @@ +#ifndef LVL_H +# define LVL_H +#include "types.h" +void initLvl(Level* lvl, int maxEnemies, Color color); +void updateLvl(Level* lvl, int maxEnemies, Player* player); +void drawLvl(Level* lvl, int maxEnemies); +void checkWin(Level* lvl, Gamestate* game, Gamestate next); +#endif // LVL_H diff --git a/src/engine/player.c b/src/engine/player.c new file mode 100644 index 0000000..b8113bf --- /dev/null +++ b/src/engine/player.c @@ -0,0 +1,70 @@ +#include "types.h" +#include + +void initPlayer(Player* player) { + player->pos = (Vector2){ 400, 250 }; + player->size = (Vector2){ 20, 40 }; + player->speed = 5.f; + player->life = 100; + player->color = RED; + player->bullet = NULL; + player->bulletCount = 0; + player->shoot = 0; + player->shootDelay = 0.1f; + player->shootTimer = (Timer){ 0 }; +} + +void playerMovement(Player* player, Audio* audio) { + if (IsKeyDown(KEY_LEFT) && player->pos.x > 0) { + player->pos.x -= player->speed; + } else if (IsKeyDown(KEY_RIGHT) && player->pos.x < WIDTH - player->size.x) { + player->pos.x += player->speed; + } + if (IsKeyDown(KEY_UP) && player->pos.y > HEIGHT / 2) { + player->pos.y -= player->speed; + } else if (IsKeyDown(KEY_DOWN) && player->pos.y < HEIGHT - player->size.y) { + player->pos.y += player->speed; + } + if (IsKeyPressed(KEY_SPACE) && !player->shoot) { + createBullet(player); + PlaySound(audio->laser); + player->shoot = 1; + newTimer(&player->shootTimer, player->shootDelay); + } + if (player->shoot == 1 && newTimer(&player->shootTimer, player->shootDelay)) { + player->shoot = 0; + } +} + +void drawPlayer(Player* player) { + DrawRectangleV(player->pos, player->size, player->color); + drawBullet(player); +} + +void createBullet(Player* player) { + player->bullet = MemRealloc(player->bullet, (player->bulletCount + 1) * sizeof(Bullet)); + player->bullet[player->bulletCount].pos = (Vector2){ player->pos.x + (player->size.x / 2), player->pos.y - 10 }; + player->bullet[player->bulletCount].radius = 2.f; + player->bullet[player->bulletCount].damage = 10; + player->bullet[player->bulletCount].color = RED; + player->bullet[player->bulletCount].speed = player->speed + 1.5f; + player->bulletCount++; +} + +void moveBullet(Player* player) { + for (int i = 0; i < player->bulletCount; i++) { + if (player->bullet[i].pos.y > 0) { + player->bullet[i].pos.y -= player->bullet[i].speed; + } else { + player->bullet[i] = player->bullet[player->bulletCount - 1]; + player->bulletCount--; + i--; + } + } +} + +void drawBullet(Player* player) { + for (int i = 0; i < player->bulletCount; i++) { + DrawCircleV(player->bullet[i].pos, player->bullet[i].radius, player->bullet[i].color); + } +} diff --git a/src/engine/player.h b/src/engine/player.h new file mode 100644 index 0000000..5290077 --- /dev/null +++ b/src/engine/player.h @@ -0,0 +1,10 @@ +#ifndef PLAYER_H +# define PLAYER_H +#include "types.h" +void initPlayer(Player* player); +void playerMovement(Player* player, Audio* audio); +void drawPlayer(Player* player); +void createBullet(Player* player); +void moveBullet(Player* player); +void drawBullet(Player* player); +#endif // PLAYER_H diff --git a/src/engine/star.c b/src/engine/star.c new file mode 100644 index 0000000..f5858ad --- /dev/null +++ b/src/engine/star.c @@ -0,0 +1,38 @@ +#include "types.h" + +void initStars(Star* star) { + for (int i = 0; i < STARS; i++) { + int randomX = GetRandomValue(0, WIDTH); + int randomY = GetRandomValue(-500, -5); + int randomRadius = GetRandomValue(1, 2); + int randomSpeed = GetRandomValue(2, 4); + star[i].pos = (Vector2){ (float)randomX, (float)randomY }; + star[i].radius = (float)randomRadius; + star[i].speed = (float)randomSpeed; + } +} + +void starMovement(int* activeStars, Star* star) { + if (IsKeyDown(KEY_S)) { + if (*activeStars > 0) (*activeStars)--; + } + if (IsKeyDown(KEY_W)) { + if (*activeStars < STARS) (*activeStars)++; + } + for (int i = 0; i < (*activeStars); i++) { + if (star[i].pos.y < HEIGHT) { + star[i].pos.y += star[i].speed; + } else { + int randomX = GetRandomValue(0, WIDTH); + int randomY = GetRandomValue(-50, -5); + star[i].pos.x = (float)randomX; + star[i].pos.y = (float)randomY; + } + } +} + +void drawStars(Star* star, int activeStars) { + for (int i = 0; i < activeStars; i++) { + DrawCircleV(star[i].pos, star[i].radius, WHITE); + } +} diff --git a/src/engine/star.h b/src/engine/star.h new file mode 100644 index 0000000..352e163 --- /dev/null +++ b/src/engine/star.h @@ -0,0 +1,7 @@ +#ifndef STAR_H +# define STAR_H +#include "types.h" +void initStars(Star* star); +void starMovement(int* activeStars, Star* star); +void drawStars(Star* star, int activeStars); +#endif // STAR_H diff --git a/src/engine/timer.c b/src/engine/timer.c new file mode 100644 index 0000000..6087d7a --- /dev/null +++ b/src/engine/timer.c @@ -0,0 +1,16 @@ +#include "types.h" + +int newTimer(Timer* timer, float elapsedTime) { + if (timer->activated == 0) { + timer->time1 = GetTime() + elapsedTime; // + 2 für Timer von 2 Sekunden + timer->activated = 1; + } + + timer->time2 = GetTime(); + if (timer->time1 <= timer->time2) { + timer->activated = 0; + return 1; + } + + return 0; +} diff --git a/src/engine/timer.h b/src/engine/timer.h new file mode 100644 index 0000000..a38912f --- /dev/null +++ b/src/engine/timer.h @@ -0,0 +1,5 @@ +#ifndef TIMER_H +# define TIMER_H +#include "types.h" +int newTimer(Timer* timer, float elapsedTime); +#endif // TIMER_H diff --git a/src/engine/types.h b/src/engine/types.h new file mode 100644 index 0000000..a4dfc24 --- /dev/null +++ b/src/engine/types.h @@ -0,0 +1,133 @@ +#ifndef TYPES_H +# define TYPES_H + +#include "raylib.h" + +/* Game constants */ +#define WIDTH 800 +#define HEIGHT 450 +#define STARS 200 +#define LVL1_ENEMIES 5 +#define LVL2_ENEMIES 10 + + +/* ---------- Structs & Enums ---------- */ + +typedef struct { + Music theme; + Sound laser; +} Audio; + + +typedef struct { + double time1; + double time2; + int activated; +} Timer; + + +typedef struct { + Vector2 pos; + float radius; + float speed; +} Star; + + +typedef struct { + Vector2 pos; + float radius; + float speed; + int damage; + Color color; +} Bullet; + + +typedef struct { + Vector2 pos; + Vector2 size; + float speed; + int life; + Color color; + Bullet* bullet; + int bulletCount; + int shoot; + float shootDelay; + Timer shootTimer; +} Player; + +typedef enum { + LEFT, + RIGHT, + UP, + DOWN +} Direction; + + +typedef struct { + Vector2 pos; + int startY; + Vector2 size; + float speed; + int life; + int damage; + int isActive; + Direction dir; + Timer timer; + Color color; + int shoot; + Bullet* bullet; + int bulletCount; +} Enemy; + +typedef enum { + INIT, + NEW_GAME, + LVL1, + LVL2, + LVL3, + WON, + GAME_OVER +} Gamestate; + +typedef struct { + int enemyCounter; + Enemy basicEnemy[100]; + int done; +} Level; + +/* ---------- Function Prototypes ---------- */ + +/* Audio */ +extern void initAudio(Audio* audio); + +/* Timer */ +extern int newTimer(Timer* timer, float elapsedTime); + +/* Stars */ +extern void initStars(Star* star); +extern void starMovement(int* activeStars, Star* star); +extern void drawStars(Star* star, int activeStars); + +/* Player */ +extern void initPlayer(Player* player); +extern void playerMovement(Player* player, Audio* audio); +extern void drawPlayer(Player* player); +extern void createBullet(Player* player); +extern void moveBullet(Player* player); +extern void drawBullet(Player* player); + +/* Enemy */ +extern void initEnemy(Enemy* enemy, Vector2 pos, int life, Color color); +extern void drawEnemy(Enemy* enemy); +extern void enemyCollision(Enemy* enemy, Player* player); +extern void enemyMovement(Enemy* enemy); +extern void createEnemyBullet(Enemy* enemy); +extern void updateEnemy(Enemy* enemy, Player* player); + +/* Level */ +extern void initLvl(Level* lvl, int maxEnemies, Color color); +extern void updateLvl(Level* lvl, int maxEnemies, Player* player); +extern void drawLvl(Level* lvl, int maxEnemies); +extern void checkWin(Level* lvl, Gamestate* game, Gamestate next); + +#endif // TYPES_H diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..266299f --- /dev/null +++ b/src/main.c @@ -0,0 +1,72 @@ +#include +#include "types.h" +#include "engine/everything.h" +// Main bootstrapper – orchestrates the game loop +int main() { + const char* title = "StarStruck"; + InitWindow(WIDTH, HEIGHT, title); + SetTargetFPS(60); + // Audio init + Audio audio; initAudio(&audio); + // Timers + Timer gameTime = {0}; int runningTime = 0; + // Stars + Star* star = MemAlloc(STARS * sizeof(Star)); + if (!star) return 1; + initStars(star); int activeStars = STARS; + // Player + Player player; initPlayer(&player); + // Game state + Gamestate game = INIT; int startGame = 0; int gameOver = 0; Timer gameOverTimer={0}; + // Levels + Level lvl1, lvl2, lvl3; + + while (!WindowShouldClose()) { + UpdateMusicStream(audio.theme); + if (startGame && newTimer(&gameTime, 1)) runningTime++; + starMovement(&activeStars, star); + playerMovement(&player, &audio); + moveBullet(&player); + if (player.life <= 0) game = GAME_OVER; + switch (game) { + case INIT: + initPlayer(&player); + initLvl(&lvl1, LVL1_ENEMIES, GREEN); + initLvl(&lvl2, LVL2_ENEMIES, BLUE); + initLvl(&lvl3, 15, YELLOW); + game = NEW_GAME; + break; + case NEW_GAME: + if (IsKeyPressed(KEY_SPACE)) { startGame=1; newTimer(&gameTime, 1.f); } + if (startGame) game = LVL1; + break; + case LVL1: updateLvl(&lvl1, LVL1_ENEMIES, &player); updateAllEnemyBullets(&lvl1, LVL1_ENEMIES); checkWin(&lvl1, &game, LVL2); break; + case LVL2: updateLvl(&lvl2, LVL2_ENEMIES, &player); updateAllEnemyBullets(&lvl2, LVL2_ENEMIES); checkWin(&lvl2, &game, WON); break; + case WON: break; + case GAME_OVER: + if (!gameOver) { newTimer(&gameOverTimer,5); gameOver=1; } + if (gameOver && newTimer(&gameOverTimer,5)) { game=INIT; startGame=0; gameOver=0; } + break; + } + BeginDrawing(); ClearBackground(BLACK); + drawStars(star, activeStars); drawPlayer(&player); + switch (game) { + case NEW_GAME: DrawText("SPACE START", WIDTH/2, HEIGHT/2, 30, WHITE); break; + case LVL1: drawLvl(&lvl1, LVL1_ENEMIES); break; + case LVL2: drawLvl(&lvl2, LVL2_ENEMIES); break; + case GAME_OVER: DrawText("GAME OVER!", WIDTH/2, HEIGHT/2, 50, RED); break; + } + DrawFPS(20,5); DrawText(TextFormat("Aktive Sterne: %d", activeStars), 20,30,10,WHITE); + DrawText(TextFormat("Aktive Bullets: %d", player.bulletCount),20,40,10,WHITE); + DrawText(TextFormat("Zeit: %d Sekunden", runningTime),20,60,10,WHITE); + DrawText(TextFormat("HP: %d", player.life), 20, HEIGHT-50,30,RED); + EndDrawing(); + } + UnloadSound(audio.laser); UnloadMusicStream(audio.theme); + CloseAudioDevice(); CloseWindow(); + MemFree(star); MemFree(player.bullet); + for (int i=0;i