diff --git a/main.c b/main.c index 9a1ee4da6e395bcafce76414b2a9c95726b13b9a..04e4f5c3549c631d1029fb24f11d74d6e1c82f7a 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ //Main file to run the game //Version: 1.0 - 30/4/25 - Added mapgen library and tested map generation capabilities //Version: 2.0 - 1/5/25 - Added movement library and tested player and asteroid movement in the map +//Version 3.0 - 3/5/25 - Added counter for number of moves and comments #include <stdio.h> #include <stdlib.h> @@ -9,18 +10,19 @@ #include "mapgen.h" #include "movement.h" int main(){ - + //Initialise array and seed the random generator srand(time(NULL)); char MapSize[Size][Size]; - + //Place the objects randomly in map InitialiseMap(MapSize); ObjectPlacement(MapSize, 'O', 5); ObjectPlacement(MapSize, 'P', 5); ObjectPlacement(MapSize, 'S', 5); - - Player player = {Size/2,Size/2}; + //Place the player in the middle of the map for starting position + Player player = {Size/2,Size/2,19}; + //Randomly place asteroid Asteroid asteroid = {rand()%Size,rand()%Size,1,1}; - + //Player and asteroid character MapSize[player.x][player.y] = 'U'; MapSize[asteroid.x][asteroid.y] = 'A'; @@ -29,10 +31,18 @@ int main(){ // NOTE TO SELF: Add more functionality here in regard to leaderboards // and saving high score to text file + + //Check if player has moves left + if (player.NoOfMoves <=0){ + printf("\nYou have run out of moves\n"); + break; + } + //Check if player hit an asteroid if(player.x == asteroid.x && player.y == asteroid.y){ printf("You hit asteroid game over\n"); break; } + //Move the player and asteroid based on input PlayerMove(MapSize,&player); AsteroidMove(MapSize,&asteroid); } diff --git a/movement.c b/movement.c index b6e1b4385c2bbb1015b9f613936c3120bf94e8f4..cb9e2fb6b5a830706ed662dd19bf1c7e0f2d2566 100644 --- a/movement.c +++ b/movement.c @@ -1,8 +1,9 @@ //Created by JAY PATEL 24028598 on 1/5/25 -//Library module for movement +//Library module for movement and resource counting //Version: 1.0 -//Version: 2.0 - Small bug in the asteroid movement where it got stuck after meeting an object because +//Version: 2.0 - 3/5/25 - Small bug in the asteroid movement where it got stuck after meeting an object because // the asteroid position was cleared before it knew if it was moving or not. Added store to hold old position. +//Version 3.0 - 3/5/25 - Added resource counter and a countdown for number of moves #include <stdio.h> #include <stdlib.h> #include "movement.h" @@ -12,6 +13,10 @@ void PlayerMove(char MapSize[Size][Size], Player *p) { int Tempx = (*p).x; int Tempy = (*p).y; char Input; + printf("\n|Resources Collected|\n"); + printf("Oxygen: %d || Petrol: %d || Scrap: %d\n",(*p).OxygenCollected,(*p).PetrolCollected,(*p).ScrapCollected); + printf("You have %d moves left!!\n", (*p).NoOfMoves); + printf("Please enter direction you want to go (wasd)\n"); scanf(" %c", &Input); while (getchar() != '\n');//Clear input buffer @@ -31,13 +36,25 @@ void PlayerMove(char MapSize[Size][Size], Player *p) { if (Input == 'd' && (*p).y < Size - 1) { Tempy++; } + //Check if player hit an object to collect + if (MapSize[Tempx][Tempy] == 'O'){ + (*p).OxygenCollected++; + } + if (MapSize[Tempx][Tempy] == 'P'){ + (*p).PetrolCollected++; + } + if (MapSize[Tempx][Tempy] == 'S'){ + (*p).ScrapCollected++; + } //Update the players position based on their input MapSize[(*p).x][(*p).y] = '+'; MapSize[Tempx][Tempy] = 'U'; //Update the temporary position stores (*p).x = Tempx; (*p).y = Tempy; - + if ((*p).NoOfMoves >0){ + (*p).NoOfMoves--; + } } //Asteroid movement function void AsteroidMove(char MapSize[Size][Size], Asteroid *a){ diff --git a/movement.h b/movement.h index 78fde367a6fc85e94591877699c84558df3c32fb..ad2e1cf9ab658debc4278697049f860daec0704e 100644 --- a/movement.h +++ b/movement.h @@ -1,6 +1,7 @@ //Created by JAY PATEL 24928598 on 30/04/25 //Library module for movement //Version: 1.0 +//Version 2.0 - 3/5/25 - Added more variables in player struct #ifndef MOVEMENT_H #define MOVEMENT_H #define Size 20 @@ -8,6 +9,10 @@ //Struct declarations typedef struct { int x,y; + int NoOfMoves; + int OxygenCollected; + int PetrolCollected; + int ScrapCollected; }Player; typedef struct {