refactored the whole game

This commit is contained in:
daniel committed 2026-10-13 19:24:06 +02:00
1 parent 317450833e
commit ea01f5a78b
31 files changed
+750 -48

No files matched your search

+61
View File
@@ -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
```
+49 -24
View File
@@ -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 OBJS := $(SRCS:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
# -Iinclude: Sagt dem Compiler, dass er im Ordner 'include' nach raylib.h suchen soll
CFLAGS = -Wall -std=c99 -Iinclude
# 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 LDFLAGS := -Llib -lraylib
# -lraylib bindet die lokale libraylib.a ein LDFLAGS += -lopengl32 -lgdi32 -lwinmm
# Die restlichen sind Windows-Systembibliotheken
LIBS = -lraylib -lopengl32 -lgdi32 -lwinmm
# Standard-Ziel zum Kompilieren # -------------------------------------------------
all: $(TARGET) # Targets
# -------------------------------------------------
.PHONY: all clean run
$(TARGET): $(SRC) all: $(BINDIR)/stars.exe
$(CC) $(CFLAGS) $(SRC) -o $(TARGET) $(LDFLAGS) $(LIBS)
# Programm ausführen $(BINDIR)/stars.exe: $(OBJS)
run: all $(CC) $(OBJS) $(LDFLAGS) -o $@
$(TARGET)
$(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: clean:
del /q $(TARGET) rm -rf $(OBJDIR) $(BINDIR)/stars.exe
run: $(BINDIR)/stars.exe
$(BINDIR)/stars.exe
+68
View File
@@ -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.
+43 -24
View File
@@ -81,6 +81,7 @@ typedef struct {
typedef enum { typedef enum {
INIT,
NEW_GAME, NEW_GAME,
LVL1, LVL1,
LVL2, LVL2,
@@ -133,7 +134,7 @@ int main() {
// Inits // Inits
const char* title = "StarStruck"; const char* title = "StarStruck";
// Window öffnen // Window �ffnen
InitWindow(WIDTH, HEIGHT, title); InitWindow(WIDTH, HEIGHT, title);
SetTargetFPS(60); SetTargetFPS(60);
@@ -146,7 +147,7 @@ int main() {
int runningTime = 0; 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)); Star* star = MemAlloc(STARS * sizeof(Star));
if (star == NULL) { if (star == NULL) {
@@ -162,26 +163,18 @@ int main() {
Player player; Player player;
initPlayer(&player); initPlayer(&player);
// GAMESTATE // GAMESTATE
Gamestate game; Gamestate game;
game = NEW_GAME; game = INIT;
int startGame = 0; int startGame = 0;
int gameOver = 0; int gameOver = 0;
Timer gameOverTimer = (Timer){ 0 }; Timer gameOverTimer = (Timer){ 0 };
// Level 1 // Level
Level lvl1; Level lvl1;
initLvl(&lvl1, LVL1_ENEMIES, GREEN);
Level lvl2; Level lvl2;
initLvl(&lvl2, LVL2_ENEMIES, BLUE);
Level lvl3; Level lvl3;
initLvl(&lvl3, 15, YELLOW);
// Gameloop // Gameloop
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
@@ -199,6 +192,17 @@ int main() {
if (player.life <= 0) game = GAME_OVER; if (player.life <= 0) game = GAME_OVER;
switch (game) { 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: case NEW_GAME:
// Erst nach Eingabe starten // Erst nach Eingabe starten
if (IsKeyPressed(KEY_SPACE)) { if (IsKeyPressed(KEY_SPACE)) {
@@ -211,19 +215,21 @@ int main() {
printf("LEVEL 1\n"); printf("LEVEL 1\n");
} }
break; break;
case LVL1: case LVL1:
updateLvl(&lvl1, LVL1_ENEMIES, &player); 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"); if (lvl1.done) printf("LEVEL 2\n");
break; break;
case LVL2: case LVL2:
updateLvl(&lvl2, LVL2_ENEMIES, &player); updateLvl(&lvl2, LVL2_ENEMIES, &player);
checkWin(&lvl2, &game, WON); checkWin(&lvl2, &game, WON);
break; break;
case WON: case WON:
break; break;
case GAME_OVER: case GAME_OVER:
if (!gameOver) { if (!gameOver) {
newTimer(&gameOverTimer, 5); newTimer(&gameOverTimer, 5);
@@ -231,11 +237,13 @@ int main() {
} }
if (gameOver && newTimer(&gameOverTimer, 5)) { if (gameOver && newTimer(&gameOverTimer, 5)) {
game = NEW_GAME;
gameOver = 0; gameOver = 0;
startGame = 0; startGame = 0;
game = INIT;
printf("CHECK!\n");
} }
break; break;
default: default:
break; break;
} }
@@ -282,7 +290,18 @@ int main() {
CloseWindow(); CloseWindow();
MemFree(star); MemFree(star);
MemFree(player.bullet); MemFree(player.bullet);
MemFree(lvl1.basicEnemy->bullet);
// Free enemy bullets for all levels
for (int i=0; i<lvl1.enemyCounter && i<100; ++i){
if (lvl1.basicEnemy[i].bullet) MemFree(lvl1.basicEnemy[i].bullet);
}
for (int i=0; i<lvl2.enemyCounter && i<100; ++i){
if (lvl2.basicEnemy[i].bullet) MemFree(lvl2.basicEnemy[i].bullet);
}
for (int i=0; i<lvl3.enemyCounter && i<100; ++i){
if (lvl3.basicEnemy[i].bullet) MemFree(lvl3.basicEnemy[i].bullet);
}
return 0; return 0;
} }
@@ -305,7 +324,7 @@ void initAudio(Audio* audio) {
// TIMER ----------------------------------------------------------------------------------------------------------- // TIMER -----------------------------------------------------------------------------------------------------------
int newTimer(Timer* timer, float elapsedTime) { int newTimer(Timer* timer, float elapsedTime) {
if (timer->activated == 0) { if (timer->activated == 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; timer->activated = 1;
} }
@@ -335,7 +354,7 @@ void initStars(Star* star) {
void starMovement(int* activeStars, Star* star) { void starMovement(int* activeStars, Star* star) {
// Sterne entfernen oder hinzufügen // Sterne entfernen oder hinzuf�gen
if (IsKeyDown(KEY_S)) { if (IsKeyDown(KEY_S)) {
if (*activeStars > 0) { if (*activeStars > 0) {
(*activeStars)--; // <- Zeiger muss zuerst dereferenziert werden, wegen der Reihenfolge der Rechenoperanden (*activeStars)--; // <- Zeiger muss zuerst dereferenziert werden, wegen der Reihenfolge der Rechenoperanden
@@ -402,7 +421,7 @@ void playerMovement(Player* player, Audio* audio) {
} }
// BULLETCREATION // 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) { if (IsKeyPressed(KEY_SPACE) && !player->shoot) {
createBullet(player); createBullet(player);
PlaySound(audio->laser); PlaySound(audio->laser);
@@ -493,7 +512,7 @@ void drawEnemy(Enemy* enemy) {
void enemyCollision(Enemy* enemy, Player* player) { void enemyCollision(Enemy* enemy, Player* player) {
if (enemy->life <= 0) return; if (enemy->life <= 0) return;
// Check für Bullets // Check f�r Bullets
for (int i = 0; i < player->bulletCount; i++) { 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 })) { 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; 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++) { 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 })) { 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; player->life -= enemy->bullet[i].damage;
@@ -540,7 +559,7 @@ void createEnemyBullet(Enemy* enemy) {
// Wenn Enemy aktiv ist, soll eine Kugel abgefeuert werden. // Wenn Enemy aktiv ist, soll eine Kugel abgefeuert werden.
if (enemy->isActive) { if (enemy->isActive) {
// Wenn Gegner schießen kann // Wenn Gegner schie�en kann
if (!enemy->shoot) { if (!enemy->shoot) {
newTimer(&enemy->timer, (float)GetRandomValue(1, 3)); newTimer(&enemy->timer, (float)GetRandomValue(1, 3));
@@ -550,7 +569,7 @@ void createEnemyBullet(Enemy* enemy) {
// Kugel erzeugen wenn der Timer abgelaufen ist // Kugel erzeugen wenn der Timer abgelaufen ist
if (enemy->shoot && newTimer(&enemy->timer, 0)) { 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 = MemRealloc(enemy->bullet, (enemy->bulletCount + 1) * sizeof(Bullet));
enemy->bullet[enemy->bulletCount].pos = (Vector2){ enemy->pos.x, enemy->pos.y }; enemy->bullet[enemy->bulletCount].pos = (Vector2){ enemy->pos.x, enemy->pos.y };
enemy->bullet[enemy->bulletCount].radius = 3.f; enemy->bullet[enemy->bulletCount].radius = 3.f;
@@ -560,7 +579,7 @@ void createEnemyBullet(Enemy* enemy) {
enemy->bulletCount++; enemy->bulletCount++;
// Schuss wieder zurücksetzen // Schuss wieder zur�cksetzen
enemy->shoot = 0; enemy->shoot = 0;
} }
} }
BIN
View File
Binary file not shown.
View File
Whitespace-only changes.
View File
Whitespace-only changes.
+14
View File
@@ -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");
}
View File
Whitespace-only changes.
+100
View File
@@ -0,0 +1,100 @@
#include "types.h"
#include <stddef.h>
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--;
}
}
}
}
}
+11
View File
@@ -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
+8
View File
@@ -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
+37
View File
@@ -0,0 +1,37 @@
#include "types.h"
#include <stdio.h>
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;
}
}
+8
View File
@@ -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
+70
View File
@@ -0,0 +1,70 @@
#include "types.h"
#include <stddef.h>
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);
}
}
+10
View File
@@ -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
+38
View File
@@ -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);
}
}
+7
View File
@@ -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
+16
View File
@@ -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;
}
+5
View File
@@ -0,0 +1,5 @@
#ifndef TIMER_H
# define TIMER_H
#include "types.h"
int newTimer(Timer* timer, float elapsedTime);
#endif // TIMER_H
+133
View File
@@ -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
+72
View File
@@ -0,0 +1,72 @@
#include <stdio.h>
#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<lvl1.enemyCounter && i<100;i++) if (lvl1.basicEnemy[i].bullet) MemFree(lvl1.basicEnemy[i].bullet);
for (int i=0;i<lvl2.enemyCounter && i<100;i++) if (lvl2.basicEnemy[i].bullet) MemFree(lvl2.basicEnemy[i].bullet);
for (int i=0;i<lvl3.enemyCounter && i<100;i++) if (lvl3.basicEnemy[i].bullet) MemFree(lvl3.basicEnemy[i].bullet);
return 0;
}
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
View File
Whitespace-only changes.