added some sounds and timer systems
This commit is contained in:
1 parent
609fa151f7
commit
19515d9f30
4 files changed
+148
-62
No files matched your search
@@ -10,6 +10,20 @@
|
|||||||
#define LVL1_ENEMIES 5
|
#define LVL1_ENEMIES 5
|
||||||
#define LVL2_ENEMIES 10
|
#define LVL2_ENEMIES 10
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Music theme;
|
||||||
|
Sound laser;
|
||||||
|
} Audio;
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
double time1;
|
||||||
|
double time2;
|
||||||
|
int activated;
|
||||||
|
} Timer;
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Vector2 pos;
|
Vector2 pos;
|
||||||
float radius;
|
float radius;
|
||||||
@@ -34,6 +48,9 @@ typedef struct {
|
|||||||
Color color;
|
Color color;
|
||||||
Bullet* bullet;
|
Bullet* bullet;
|
||||||
int bulletCount;
|
int bulletCount;
|
||||||
|
int shoot;
|
||||||
|
float shootDelay;
|
||||||
|
Timer shootTimer;
|
||||||
} Player;
|
} Player;
|
||||||
|
|
||||||
|
|
||||||
@@ -53,6 +70,7 @@ typedef struct {
|
|||||||
int life;
|
int life;
|
||||||
int isActive;
|
int isActive;
|
||||||
Direction dir;
|
Direction dir;
|
||||||
|
Timer timer;
|
||||||
Color color;
|
Color color;
|
||||||
} Enemy;
|
} Enemy;
|
||||||
|
|
||||||
@@ -62,27 +80,25 @@ typedef enum {
|
|||||||
LVL1,
|
LVL1,
|
||||||
LVL2,
|
LVL2,
|
||||||
LVL3,
|
LVL3,
|
||||||
|
WON,
|
||||||
GAME_OVER
|
GAME_OVER
|
||||||
} Gamestate;
|
} Gamestate;
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int enemyCounter;
|
int enemyCounter;
|
||||||
Enemy basicEnemy[LVL1_ENEMIES];
|
Enemy basicEnemy[100];
|
||||||
} Lvl1;
|
int done;
|
||||||
|
} Level;
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int enemyCounter;
|
|
||||||
Enemy basicEnemy[LVL2_ENEMIES];
|
|
||||||
} Lvl2;
|
|
||||||
|
|
||||||
|
|
||||||
|
int newTimer(Timer* timer, float elapsedTime);
|
||||||
|
void initAudio(Audio* audio);
|
||||||
void initStars(Star* star);
|
void initStars(Star* star);
|
||||||
void starMovement(int* activeStars, Star* star);
|
void starMovement(int* activeStars, Star* star);
|
||||||
void drawStars(Star* star, int activeStars);
|
void drawStars(Star* star, int activeStars);
|
||||||
void initPlayer(Player* player);
|
void initPlayer(Player* player);
|
||||||
void playerMovement(Player* player, Star* star, int activeStars);
|
void playerMovement(Player* player, Audio* audio, Star* star, int activeStars);
|
||||||
void drawPlayer(Player* player);
|
void drawPlayer(Player* player);
|
||||||
void createBullet(Player* player);
|
void createBullet(Player* player);
|
||||||
void moveBullet(Player* player);
|
void moveBullet(Player* player);
|
||||||
@@ -92,14 +108,15 @@ void drawEnemy(Enemy* enemy);
|
|||||||
void enemyCollision(Enemy* enemy, Player* player);
|
void enemyCollision(Enemy* enemy, Player* player);
|
||||||
void enemyMovement(Enemy* enemy);
|
void enemyMovement(Enemy* enemy);
|
||||||
|
|
||||||
void initLvl1(Lvl1* lvl1);
|
void initLvl1(Level* lvl1);
|
||||||
void updateLvl1(Lvl1* lvl1, Player* player);
|
void updateLvl(Level* lvl, int maxEnemies, Player* player);
|
||||||
void drawLvl1(Lvl1* lvl1);
|
void drawLvl1(Level* lvl1);
|
||||||
void lvl1CheckWin(Lvl1* lvl1, Gamestate* game);
|
|
||||||
|
|
||||||
void initLvl2(Lvl2* lvl2);
|
void checkWin(Level* lvl, Gamestate* game, Gamestate next);
|
||||||
void updateLvl2(Lvl2* lvl2, Player* player);
|
|
||||||
void drawLvl2(Lvl2* lvl2);
|
void initLvl2(Level* lvl2);
|
||||||
|
void drawLvl2(Level* lvl2);
|
||||||
|
void lvl2CheckWin(Level* lvl2, Gamestate* game);
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@@ -108,9 +125,17 @@ int main() {
|
|||||||
|
|
||||||
// Window öffnen
|
// Window öffnen
|
||||||
InitWindow(WIDTH, HEIGHT, title);
|
InitWindow(WIDTH, HEIGHT, title);
|
||||||
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
|
||||||
|
// SOUNDS
|
||||||
|
Audio audio;
|
||||||
|
initAudio(&audio);
|
||||||
|
|
||||||
|
// TIMER
|
||||||
|
Timer gameTime = (Timer){ 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));
|
||||||
|
|
||||||
@@ -130,30 +155,55 @@ int main() {
|
|||||||
|
|
||||||
// GAMESTATE
|
// GAMESTATE
|
||||||
Gamestate game;
|
Gamestate game;
|
||||||
game = LVL1;
|
game = NEW_GAME;
|
||||||
|
|
||||||
|
|
||||||
|
int startGame = 0;
|
||||||
// Level 1
|
// Level 1
|
||||||
Lvl1 lvl1;
|
Level lvl1;
|
||||||
initLvl1(&lvl1);
|
initLvl1(&lvl1);
|
||||||
|
|
||||||
Lvl2 lvl2;
|
Level lvl2;
|
||||||
initLvl2(&lvl2);
|
initLvl2(&lvl2);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Gameloop
|
// Gameloop
|
||||||
while (!WindowShouldClose()) {
|
while (!WindowShouldClose()) {
|
||||||
// UPDATE LOGIC
|
// UPDATE LOGIC
|
||||||
|
// Musik
|
||||||
|
UpdateMusicStream(audio.theme);
|
||||||
|
|
||||||
starMovement(&activeStars, star);
|
starMovement(&activeStars, star);
|
||||||
playerMovement(&player, star, activeStars);
|
playerMovement(&player, &audio, star, activeStars);
|
||||||
moveBullet(&player);
|
moveBullet(&player);
|
||||||
|
|
||||||
|
if (startGame && newTimer(&gameTime, 1)) runningTime++;
|
||||||
|
|
||||||
switch (game) {
|
switch (game) {
|
||||||
|
case NEW_GAME:
|
||||||
|
// Erst nach Eingabe starten
|
||||||
|
if (IsKeyPressed(KEY_SPACE)) {
|
||||||
|
startGame = 1;
|
||||||
|
newTimer(&gameTime, 1.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (startGame) {
|
||||||
|
game = LVL1;
|
||||||
|
|
||||||
|
printf("LEVEL 1\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
case LVL1:
|
case LVL1:
|
||||||
updateLvl1(&lvl1, &player);
|
updateLvl(&lvl1, LVL1_ENEMIES, &player);
|
||||||
lvl1CheckWin(&lvl1, &game);
|
checkWin(&lvl1, &game, LVL2); // <- Welcher Level als nächstes?
|
||||||
|
if (lvl1.done) printf("LEVEL 2\n");
|
||||||
break;
|
break;
|
||||||
case LVL2:
|
case LVL2:
|
||||||
updateLvl2(&lvl2, &player);
|
updateLvl(&lvl2, LVL2_ENEMIES, &player);
|
||||||
|
checkWin(&lvl2, &game, WON);
|
||||||
|
break;
|
||||||
|
case WON:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -168,6 +218,9 @@ int main() {
|
|||||||
drawBullet(&player);
|
drawBullet(&player);
|
||||||
|
|
||||||
switch (game) {
|
switch (game) {
|
||||||
|
case NEW_GAME:
|
||||||
|
DrawText("SPACE START", WIDTH / 2, HEIGHT / 2, 30, WHITE);
|
||||||
|
break;
|
||||||
case LVL1:
|
case LVL1:
|
||||||
drawLvl1(&lvl1);
|
drawLvl1(&lvl1);
|
||||||
break;
|
break;
|
||||||
@@ -181,12 +234,16 @@ int main() {
|
|||||||
DrawFPS(20, 5);
|
DrawFPS(20, 5);
|
||||||
DrawText(TextFormat("Aktive Sterne: %d", activeStars), 20, 30, 10, WHITE);
|
DrawText(TextFormat("Aktive Sterne: %d", activeStars), 20, 30, 10, WHITE);
|
||||||
DrawText(TextFormat("Aktive Bullets: %d", player.bulletCount), 20, 40, 10, WHITE);
|
DrawText(TextFormat("Aktive Bullets: %d", player.bulletCount), 20, 40, 10, WHITE);
|
||||||
|
DrawText(TextFormat("Zeit: %d Sekunden", runningTime), 20, 60, 10, WHITE);
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alles wieder freigeben
|
// Alles wieder freigeben
|
||||||
|
UnloadSound(audio.laser);
|
||||||
|
UnloadMusicStream(audio.theme);
|
||||||
|
CloseAudioDevice();
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
MemFree(star);
|
MemFree(star);
|
||||||
MemFree(player.bullet);
|
MemFree(player.bullet);
|
||||||
@@ -195,6 +252,39 @@ int main() {
|
|||||||
|
|
||||||
|
|
||||||
// FUNKTIONEN ------------------------------------------------------------------------------------------------------
|
// FUNKTIONEN ------------------------------------------------------------------------------------------------------
|
||||||
|
// AUDIO -----------------------------------------------------------------------------------------------------------
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TIMER -----------------------------------------------------------------------------------------------------------
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// STARS -----------------------------------------------------------------------------------------------------------
|
||||||
void initStars(Star* star) {
|
void initStars(Star* star) {
|
||||||
for (int i = 0; i < STARS; i++) {
|
for (int i = 0; i < STARS; i++) {
|
||||||
int randomX = GetRandomValue(0, WIDTH);
|
int randomX = GetRandomValue(0, WIDTH);
|
||||||
@@ -253,10 +343,13 @@ void initPlayer(Player* player) {
|
|||||||
player->color = RED;
|
player->color = RED;
|
||||||
player->bullet = NULL;
|
player->bullet = NULL;
|
||||||
player->bulletCount = 0;
|
player->bulletCount = 0;
|
||||||
|
player->shoot = 0;
|
||||||
|
player->shootDelay = 0.1f;
|
||||||
|
player->shootTimer = (Timer){ 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void playerMovement(Player* player, Star* star, int activeStars) {
|
void playerMovement(Player* player, Audio* audio, Star* star, int activeStars) {
|
||||||
// MOVEMENT
|
// MOVEMENT
|
||||||
if (IsKeyDown(KEY_LEFT) && player->pos.x > 0) {
|
if (IsKeyDown(KEY_LEFT) && player->pos.x > 0) {
|
||||||
player->pos.x -= player->speed;
|
player->pos.x -= player->speed;
|
||||||
@@ -273,10 +366,17 @@ void playerMovement(Player* player, Star* star, int activeStars) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BULLETCREATION
|
// BULLETCREATION
|
||||||
if (IsKeyPressed(KEY_SPACE)) {
|
// Es soll erst nach X Sekunden geschossen werden können
|
||||||
|
if (IsKeyPressed(KEY_SPACE) && !player->shoot) {
|
||||||
createBullet(player);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -328,6 +428,7 @@ void initEnemy(Enemy* enemy, Vector2 pos, int life, Color color) {
|
|||||||
enemy->size = (Vector2){ 30, 10 };
|
enemy->size = (Vector2){ 30, 10 };
|
||||||
enemy->speed = 2.f;
|
enemy->speed = 2.f;
|
||||||
enemy->dir = RIGHT;
|
enemy->dir = RIGHT;
|
||||||
|
enemy->timer = (Timer){0};
|
||||||
enemy->life = life;
|
enemy->life = life;
|
||||||
enemy->isActive = 1;
|
enemy->isActive = 1;
|
||||||
enemy->color = color;
|
enemy->color = color;
|
||||||
@@ -377,8 +478,9 @@ void enemyMovement(Enemy* enemy) {
|
|||||||
|
|
||||||
// LEVEL 1 ------------------------------------------------------------------------------------------------------
|
// LEVEL 1 ------------------------------------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
void initLvl1(Lvl1* lvl1) {
|
void initLvl1(Level* lvl1) {
|
||||||
lvl1->enemyCounter = LVL1_ENEMIES; // <- 5
|
lvl1->enemyCounter = LVL1_ENEMIES; // <- 5
|
||||||
|
lvl1->done = 0;
|
||||||
|
|
||||||
for (int i = 0; i < LVL1_ENEMIES; i++) {
|
for (int i = 0; i < LVL1_ENEMIES; i++) {
|
||||||
initEnemy(&lvl1->basicEnemy[i], (Vector2){ (float)GetRandomValue(10, WIDTH - 50), (float)GetRandomValue(-150, -20) }, 50, GREEN);
|
initEnemy(&lvl1->basicEnemy[i], (Vector2){ (float)GetRandomValue(10, WIDTH - 50), (float)GetRandomValue(-150, -20) }, 50, GREEN);
|
||||||
@@ -386,25 +488,25 @@ void initLvl1(Lvl1* lvl1) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void updateLvl1(Lvl1* lvl1, Player* player) {
|
void updateLvl(Level* lvl, int maxEnemies, Player* player) {
|
||||||
// Enemies bewegen & Collision
|
// Enemies bewegen & Collision
|
||||||
for (int i = 0; i < LVL1_ENEMIES; i++) {
|
for (int i = 0; i < maxEnemies; i++) {
|
||||||
// Wenn der Gegner aktiv ist.
|
// Wenn der Gegner aktiv ist.
|
||||||
if (lvl1->basicEnemy[i].isActive) {
|
if (lvl->basicEnemy[i].isActive) {
|
||||||
enemyMovement(&lvl1->basicEnemy[i]);
|
enemyMovement(&lvl->basicEnemy[i]);
|
||||||
enemyCollision(&lvl1->basicEnemy[i], player);
|
enemyCollision(&lvl->basicEnemy[i], player);
|
||||||
|
|
||||||
// Gegner abziehen, sobald Leben bei 0 ist und deaktivieren
|
// Gegner abziehen, sobald Leben bei 0 ist und deaktivieren
|
||||||
if (lvl1->basicEnemy[i].life <= 0) {
|
if (lvl->basicEnemy[i].life <= 0) {
|
||||||
lvl1->enemyCounter--;
|
lvl->enemyCounter--;
|
||||||
lvl1->basicEnemy[i].isActive = 0;
|
lvl->basicEnemy[i].isActive = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void drawLvl1(Lvl1* lvl1) {
|
void drawLvl1(Level* lvl1) {
|
||||||
for (int i = 0; i < LVL1_ENEMIES; i++) {
|
for (int i = 0; i < LVL1_ENEMIES; i++) {
|
||||||
if (lvl1->basicEnemy[i].life > 0)
|
if (lvl1->basicEnemy[i].life > 0)
|
||||||
drawEnemy(&lvl1->basicEnemy[i]);
|
drawEnemy(&lvl1->basicEnemy[i]);
|
||||||
@@ -413,18 +515,20 @@ void drawLvl1(Lvl1* lvl1) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void lvl1CheckWin(Lvl1* lvl1, Gamestate* game) {
|
void checkWin(Level* lvl, Gamestate* game, Gamestate next) {
|
||||||
if (lvl1->enemyCounter <= 0) {
|
if (lvl->enemyCounter <= 0) {
|
||||||
printf("GEWONNEN");
|
printf("GEWONNEN!\n");
|
||||||
*game = LVL2;
|
lvl->done = 1;
|
||||||
|
*game = next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// LEVEL 2 ------------------------------------------------------------------------------------------------------
|
// LEVEL 2 ------------------------------------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
void initLvl2(Lvl2* lvl2) {
|
void initLvl2(Level* lvl2) {
|
||||||
lvl2->enemyCounter = LVL2_ENEMIES;
|
lvl2->enemyCounter = LVL2_ENEMIES;
|
||||||
|
lvl2->done = 0;
|
||||||
|
|
||||||
for (int i = 0; i < LVL2_ENEMIES; i++) {
|
for (int i = 0; i < LVL2_ENEMIES; i++) {
|
||||||
initEnemy(&lvl2->basicEnemy[i], (Vector2){ (float)GetRandomValue(10, WIDTH - 50), (float)GetRandomValue(-150, -20) }, 50, BLUE);
|
initEnemy(&lvl2->basicEnemy[i], (Vector2){ (float)GetRandomValue(10, WIDTH - 50), (float)GetRandomValue(-150, -20) }, 50, BLUE);
|
||||||
@@ -432,25 +536,7 @@ void initLvl2(Lvl2* lvl2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void updateLvl2(Lvl2* lvl2, Player* player) {
|
void drawLvl2(Level* lvl2) {
|
||||||
// Enemies bewegen & Collision
|
|
||||||
for (int i = 0; i < LVL2_ENEMIES; i++) {
|
|
||||||
// Wenn der Gegner aktiv ist.
|
|
||||||
if (lvl2->basicEnemy[i].isActive) {
|
|
||||||
enemyMovement(&lvl2->basicEnemy[i]);
|
|
||||||
enemyCollision(&lvl2->basicEnemy[i], player);
|
|
||||||
|
|
||||||
// Gegner abziehen, sobald Leben bei 0 ist und deaktivieren
|
|
||||||
if (lvl2->basicEnemy[i].life <= 0) {
|
|
||||||
lvl2->enemyCounter--;
|
|
||||||
lvl2->basicEnemy[i].isActive = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void drawLvl2(Lvl2* lvl2) {
|
|
||||||
for (int i = 0; i < LVL2_ENEMIES; i++) {
|
for (int i = 0; i < LVL2_ENEMIES; i++) {
|
||||||
if (lvl2->basicEnemy[i].life > 0)
|
if (lvl2->basicEnemy[i].life > 0)
|
||||||
drawEnemy(&lvl2->basicEnemy[i]);
|
drawEnemy(&lvl2->basicEnemy[i]);
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user