diff --git a/main.c b/main.c index 11b3d268b3545b0314d774fc2e6ac2fd1dcf697d..9a1ee4da6e395bcafce76414b2a9c95726b13b9a 100644 --- a/main.c +++ b/main.c @@ -37,6 +37,4 @@ int main(){ AsteroidMove(MapSize,&asteroid); } return 0; -} - - +} \ No newline at end of file diff --git a/mapgen.c b/mapgen.c index 2ac468642711d2104d36cdb9ca18dcb7ef312092..60fe0563671b5c4ba9ab53e822c15e5f1d267c6b 100644 --- a/mapgen.c +++ b/mapgen.c @@ -1,31 +1,32 @@ //Created by JAY PATEL 24028598 on 30/04/25 //Library Module for map generation //Version: 1.0 - -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> #include "mapgen.h" +//Create the map grid, size is determined in the .h files of the function void InitialiseMap(char MapSize[Size][Size]){ for (int i = 0; i < Size; i++){ for (int j = 0; j < Size; ++j) { - MapSize[i][j] = '.'; + MapSize[i][j] = '+'; } } } - +//Placement of intractable objects void ObjectPlacement(char MapSize[Size][Size], char Object, int Count){ int Placed = 0; while (Placed < Count){ int x = rand() % Size; int y = rand() % Size; - if (MapSize[x][y] == '.'){ + //Only place object if tile is empty + if (MapSize[x][y] == '+'){ MapSize[x][y] = Object; Placed++; } } } - +//Output of the created map void MapOutput(char MapSize[Size][Size]){ for (int i = 0; i < Size; ++i) { for (int j = 0; j < Size; ++j) { diff --git a/mapgen.h b/mapgen.h index b2598d6182cedc6f87f42e9243b6452dc2ff0d49..edec09b3c4d4ee3313bcb346ce59f28fad016a43 100644 --- a/mapgen.h +++ b/mapgen.h @@ -7,7 +7,7 @@ //Define grid size #define Size 20 -//All Functions for Map Generation +//Define all functions to be used for Map Generation void InitialiseMap(char MapSize[Size][Size]); void ObjectPlacement(char MapSize[Size][Size], char Object, int Count); void MapOutput(char MapSize[Size][Size]); diff --git a/movement.c b/movement.c index 0ccf6cfc33558c7116671b6d00d93cfe7250fa18..b6e1b4385c2bbb1015b9f613936c3120bf94e8f4 100644 --- a/movement.c +++ b/movement.c @@ -1,41 +1,50 @@ //Created by JAY PATEL 24028598 on 1/5/25 //Library module for movement //Version: 1.0 +//Version: 2.0 - 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. #include <stdio.h> #include <stdlib.h> #include "movement.h" - +//Player movement function void PlayerMove(char MapSize[Size][Size], Player *p) { int Tempx = (*p).x; int Tempy = (*p).y; char Input; printf("Please enter direction you want to go (wasd)\n"); scanf(" %c", &Input); - while (getchar() != '\n'); - if (Input == 'w' && (*p).x > 0) { - Tempx--; - } - if (Input == 'a' && (*p).y > 0) { - Tempy--; - } - if (Input == 's' && (*p).x < Size - 1) { - Tempx++; - } - if (Input == 'd' && (*p).y < Size - 1) { - Tempy++; - } - - MapSize[(*p).x][(*p).y] = '.'; - MapSize[Tempx][Tempy] = 'U'; - - (*p).x = Tempx; - (*p).y = Tempy; + while (getchar() != '\n');//Clear input buffer + //Go up + if (Input == 'w' && (*p).x > 0) { + Tempx--; + } + //Go left + if (Input == 'a' && (*p).y > 0) { + Tempy--; } + //Go down + if (Input == 's' && (*p).x < Size - 1) { + Tempx++; + } + //Go right + if (Input == 'd' && (*p).y < Size - 1) { + Tempy++; + } + //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; + } +//Asteroid movement function void AsteroidMove(char MapSize[Size][Size], Asteroid *a){ - MapSize[(*a).x][((*a).y)] = '.'; - + //Store previous position of asteroid + int PrevX = (*a).x; + int PrevY = (*a).y; + //Randomly move asteroid int AsteroidDirection = rand() %4; if (AsteroidDirection == 0){ (*a).dx = -1, (*a).dy = 0; @@ -49,19 +58,21 @@ void AsteroidMove(char MapSize[Size][Size], Asteroid *a){ else if (AsteroidDirection == 3){ (*a).dx = 0, (*a).dy = 1; } - - int temp1 = (*a).x + (*a).dx; - int temp2 = (*a).y + (*a).dy; - - if ( MapSize[temp1][temp2]=='O' || MapSize[temp1][temp2]=='P' || MapSize[temp1][temp2]=='S'){ + //Calculate the next position of the asteroid + //Only moves there if tile is empty + int Temp1 = PrevX + (*a).dx; + int Temp2 = PrevY + (*a).dy; + //Asteroid bounce back from object if it enters a tile occupied by an object + if ( MapSize[Temp1][Temp2]=='O' || MapSize[Temp1][Temp2]=='P' || MapSize[Temp1][Temp2]=='S'){ (*a).dx = (*a).dx *-1; (*a).dy = (*a).dy *-1; }else{ - (*a).x = temp1; - (*a).y= temp2; + //Clear old asteroid position + MapSize[(*a).x][((*a).y)] = '+'; + (*a).x = Temp1; + (*a).y= Temp2; } - - + //Keep the asteroid within the game cube, if it hits a wall it bounces back if((*a).x< 0){ (*a).x = 0; } @@ -74,7 +85,6 @@ void AsteroidMove(char MapSize[Size][Size], Asteroid *a){ else if((*a).y>= Size){ (*a).y = Size-1; } - - + //Set the new position of the Asteroid MapSize[(*a).x][(*a).y] = 'A'; } \ No newline at end of file