mirror of
https://github.com/prurigro/intelligent-snake.git
synced 2024-11-21 15:42:31 -05:00
Add an option to render the game at twice the size
This commit is contained in:
parent
78163cec45
commit
1f4ad02aaf
2 changed files with 57 additions and 50 deletions
|
@ -47,6 +47,7 @@ Usage: `./isnake [options]`
|
||||||
* `./isnake -b [blocks]`: Set the number of blocks: between [0] and [40] (DEFAULT: [20])
|
* `./isnake -b [blocks]`: Set the number of blocks: between [0] and [40] (DEFAULT: [20])
|
||||||
* `./isnake -l [length]`: Set the snake's starting length: between [3] and [35] (DEFAULT: [3])
|
* `./isnake -l [length]`: Set the snake's starting length: between [3] and [35] (DEFAULT: [3])
|
||||||
* `./isnake -s [speed]`: Set the snake's starting speed: between [1] and [9] (DEFAULT: [1])
|
* `./isnake -s [speed]`: Set the snake's starting speed: between [1] and [9] (DEFAULT: [1])
|
||||||
|
* `./isnake -2`: Double the size the game renders at
|
||||||
* `./isnake -h`: Display help information
|
* `./isnake -h`: Display help information
|
||||||
|
|
||||||
## Controls ##
|
## Controls ##
|
||||||
|
|
106
isnake.c
106
isnake.c
|
@ -91,29 +91,29 @@ const int colourTextData[3] = { 135, 215, 255 }; // Blue
|
||||||
|
|
||||||
// ENUMERATIONS FOR MORE READABLE CODE
|
// ENUMERATIONS FOR MORE READABLE CODE
|
||||||
enum direction { Up, Down, Left, Right }; // Movement directions
|
enum direction { Up, Down, Left, Right }; // Movement directions
|
||||||
enum gameParams { QuitGame, TilesHigh, TilesWide, NPCCount, SnakeSpeed, SnakeLength, SnakeDirection, SnakeScore };
|
enum gameParams { QuitGame, TilesHigh, TilesWide, NPCCount, SnakeSpeed, SnakeLength, SnakeDirection, SnakeScore, RenderSizeMultiplier };
|
||||||
|
|
||||||
// GAME FUNCTIONS
|
// GAME FUNCTIONS
|
||||||
void gameLoop(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8], SDL_Event* event);
|
void gameLoop(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9], SDL_Event* event);
|
||||||
void moveSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8], enum direction newDirection);
|
void moveSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9], enum direction newDirection);
|
||||||
bool collisionDetect(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8], enum direction newDirection);
|
bool collisionDetect(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9], enum direction newDirection);
|
||||||
void collisionDetectFood(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8]);
|
void collisionDetectFood(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9]);
|
||||||
bool gameEventPoll(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8], SDL_Event* event);
|
bool gameEventPoll(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9], SDL_Event* event);
|
||||||
bool scrollSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8]);
|
bool scrollSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9]);
|
||||||
void updateSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8]);
|
void updateSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9]);
|
||||||
void loadNPCs(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8]);
|
void loadNPCs(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9]);
|
||||||
void randomLocation(int** ppSprites, int (*gameParameters)[8], int* location[2]);
|
void randomLocation(int** ppSprites, int (*gameParameters)[9], int* location[2]);
|
||||||
void drawText(SDL_Surface* screen, char* string, int size, int x, int y, SDL_Colour colour);
|
void drawText(SDL_Surface* screen, char* string, int size, int x, int y, SDL_Colour colour);
|
||||||
void updateRect(SDL_Surface* screen, SDL_Rect** ppTiles, int position[2], const int colour[3]);
|
void updateRect(SDL_Surface* screen, SDL_Rect** ppTiles, int position[2], const int colour[3]);
|
||||||
|
|
||||||
// COMMANDLINE FUNCTIONS
|
// COMMANDLINE FUNCTIONS
|
||||||
void configureGame(int argc, char** args, int (*gameParameters)[8]);
|
void configureGame(int argc, char** args, int (*gameParameters)[9]);
|
||||||
void printHelpMenu(char filename[]);
|
void printHelpMenu(char filename[]);
|
||||||
void printErrorHelp(char filename[]);
|
void printErrorHelp(char filename[]);
|
||||||
|
|
||||||
// MAIN LOOP
|
// MAIN LOOP
|
||||||
int main(int argc, char* args[]) {
|
int main(int argc, char* args[]) {
|
||||||
int x, y, gameParameters[8];
|
int x, y, gameParameters[9];
|
||||||
int** ppSprites = NULL;
|
int** ppSprites = NULL;
|
||||||
int* pSprites = NULL;
|
int* pSprites = NULL;
|
||||||
SDL_Surface* screen = NULL;
|
SDL_Surface* screen = NULL;
|
||||||
|
@ -144,7 +144,7 @@ int main(int argc, char* args[]) {
|
||||||
atexit(TTF_Quit);
|
atexit(TTF_Quit);
|
||||||
|
|
||||||
// INITIALIZE THE MAIN SURFACE
|
// INITIALIZE THE MAIN SURFACE
|
||||||
if ((screen = SDL_SetVideoMode((TILEWIDTH * gameParameters[TilesWide]), (TILEHEIGHT * gameParameters[TilesHigh]) + 35, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT)) == NULL) {
|
if ((screen = SDL_SetVideoMode((TILEWIDTH * gameParameters[TilesWide] * gameParameters[RenderSizeMultiplier]), ((TILEHEIGHT * gameParameters[TilesHigh]) + 35) * gameParameters[RenderSizeMultiplier], 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT)) == NULL) {
|
||||||
fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError());
|
fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError());
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
@ -169,10 +169,10 @@ int main(int argc, char* args[]) {
|
||||||
|
|
||||||
for (x = 0; x < gameParameters[TilesHigh]; x++) {
|
for (x = 0; x < gameParameters[TilesHigh]; x++) {
|
||||||
for (y = 0; y < gameParameters[TilesWide]; y++) {
|
for (y = 0; y < gameParameters[TilesWide]; y++) {
|
||||||
ppTiles[x][y].w = TILEWIDTH - (TILEWIDTH / 10);
|
ppTiles[x][y].w = TILEWIDTH * gameParameters[RenderSizeMultiplier] - (TILEWIDTH * gameParameters[RenderSizeMultiplier] / 10);
|
||||||
ppTiles[x][y].h = TILEHEIGHT - (TILEHEIGHT / 10);
|
ppTiles[x][y].h = TILEHEIGHT * gameParameters[RenderSizeMultiplier] - (TILEHEIGHT * gameParameters[RenderSizeMultiplier] / 10);
|
||||||
ppTiles[x][y].x = ((ppTiles[x][y].w + (TILEWIDTH / 10)) * y);
|
ppTiles[x][y].x = ((ppTiles[x][y].w + (TILEWIDTH * gameParameters[RenderSizeMultiplier] / 10)) * y);
|
||||||
ppTiles[x][y].y = ((ppTiles[x][y].h + (TILEHEIGHT / 10)) * x);
|
ppTiles[x][y].y = ((ppTiles[x][y].h + (TILEHEIGHT * gameParameters[RenderSizeMultiplier] / 10)) * x);
|
||||||
SDL_FillRect(screen, &ppTiles[x][y], SDL_MapRGB(screen->format, colourTiles[0], colourTiles[1], colourTiles[2]));
|
SDL_FillRect(screen, &ppTiles[x][y], SDL_MapRGB(screen->format, colourTiles[0], colourTiles[1], colourTiles[2]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -221,16 +221,16 @@ int main(int argc, char* args[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GAME LOOP
|
// GAME LOOP
|
||||||
void gameLoop(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8], SDL_Event* event) {
|
void gameLoop(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9], SDL_Event* event) {
|
||||||
SDL_Colour SDL_ColourBackground = { colourBackground[0], colourBackground[1], colourBackground[2] };
|
SDL_Colour SDL_ColourBackground = { colourBackground[0], colourBackground[1], colourBackground[2] };
|
||||||
SDL_Colour SDL_ColourTextLabel = { colourTextLabel[0], colourTextLabel[1], colourTextLabel[2] };
|
SDL_Colour SDL_ColourTextLabel = { colourTextLabel[0], colourTextLabel[1], colourTextLabel[2] };
|
||||||
SDL_Colour SDL_ColourTextData = { colourTextData[0], colourTextData[1], colourTextData[2] };
|
SDL_Colour SDL_ColourTextData = { colourTextData[0], colourTextData[1], colourTextData[2] };
|
||||||
SDL_Colour SDL_ColourTextGameOver = { colourTextGameOver[0], colourTextGameOver[1], colourTextGameOver[2] };
|
SDL_Colour SDL_ColourTextGameOver = { colourTextGameOver[0], colourTextGameOver[1], colourTextGameOver[2] };
|
||||||
int scoreLabelPosition[2] = { 15, (TILEHEIGHT * (*gameParameters)[TilesHigh]) + 4 };
|
int scoreLabelPosition[2] = { 15 * (*gameParameters)[RenderSizeMultiplier], ((TILEHEIGHT * (*gameParameters)[TilesHigh]) + 4) * (*gameParameters)[RenderSizeMultiplier] };
|
||||||
int scoreDataPosition[2] = { 95, (TILEHEIGHT * (*gameParameters)[TilesHigh]) + 4 };
|
int scoreDataPosition[2] = { 95 * (*gameParameters)[RenderSizeMultiplier], ((TILEHEIGHT * (*gameParameters)[TilesHigh]) + 4) * (*gameParameters)[RenderSizeMultiplier] };
|
||||||
int speedLabelPosition[2] = { (TILEWIDTH * (*gameParameters)[TilesWide]) - 100, (TILEHEIGHT * (*gameParameters)[TilesHigh]) + 4 };
|
int speedLabelPosition[2] = { ((TILEWIDTH * (*gameParameters)[TilesWide]) - 100) * (*gameParameters)[RenderSizeMultiplier], ((TILEHEIGHT * (*gameParameters)[TilesHigh]) + 4) * (*gameParameters)[RenderSizeMultiplier] };
|
||||||
int speedDataPosition[2] = { (TILEWIDTH * (*gameParameters)[TilesWide]) - 25, (TILEHEIGHT * (*gameParameters)[TilesHigh]) + 4 };
|
int speedDataPosition[2] = { ((TILEWIDTH * (*gameParameters)[TilesWide]) - 25) * (*gameParameters)[RenderSizeMultiplier], ((TILEHEIGHT * (*gameParameters)[TilesHigh]) + 4) * (*gameParameters)[RenderSizeMultiplier] };
|
||||||
int gameOverMsgPosition[5] = { (TILEWIDTH * ((*gameParameters)[TilesWide] / 2)) - 147, (TILEWIDTH * ((*gameParameters)[TilesWide] / 2)) - 50, (TILEWIDTH * ((*gameParameters)[TilesWide] / 2)) + 61, (TILEWIDTH * ((*gameParameters)[TilesWide] / 2)) + 82, (TILEHEIGHT * (*gameParameters)[TilesHigh]) + 9 };
|
int gameOverMsgPosition[5] = { ((TILEWIDTH * ((*gameParameters)[TilesWide] / 2)) - 147) * (*gameParameters)[RenderSizeMultiplier], ((TILEWIDTH * ((*gameParameters)[TilesWide] / 2)) - 50) * (*gameParameters)[RenderSizeMultiplier], ((TILEWIDTH * ((*gameParameters)[TilesWide] / 2)) + 61) * (*gameParameters)[RenderSizeMultiplier], ((TILEWIDTH * ((*gameParameters)[TilesWide] / 2)) + 82) * (*gameParameters)[RenderSizeMultiplier], ((TILEHEIGHT * (*gameParameters)[TilesHigh]) + 9) * (*gameParameters)[RenderSizeMultiplier] };
|
||||||
char* gameOverMsg[4] = { "GAME OVER", "SPACE to RESTART", " or ", "ESC to QUIT" };
|
char* gameOverMsg[4] = { "GAME OVER", "SPACE to RESTART", " or ", "ESC to QUIT" };
|
||||||
char tempString[2][3] = { "-1", "-1" };
|
char tempString[2][3] = { "-1", "-1" };
|
||||||
|
|
||||||
|
@ -238,29 +238,29 @@ void gameLoop(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*ga
|
||||||
loadNPCs(screen, ppTiles, ppSprites, gameParameters);
|
loadNPCs(screen, ppTiles, ppSprites, gameParameters);
|
||||||
|
|
||||||
// DRAW LABELS FOR SCORE AND SPEED RESPECTIVELY
|
// DRAW LABELS FOR SCORE AND SPEED RESPECTIVELY
|
||||||
drawText(screen, "SCORE", DEFAULT_FONT_SIZE, scoreLabelPosition[0], scoreLabelPosition[1], SDL_ColourTextLabel);
|
drawText(screen, "SCORE", DEFAULT_FONT_SIZE * (*gameParameters)[RenderSizeMultiplier], scoreLabelPosition[0], scoreLabelPosition[1], SDL_ColourTextLabel);
|
||||||
drawText(screen, "SPEED", DEFAULT_FONT_SIZE, speedLabelPosition[0], speedLabelPosition[1], SDL_ColourTextLabel);
|
drawText(screen, "SPEED", DEFAULT_FONT_SIZE * (*gameParameters)[RenderSizeMultiplier], speedLabelPosition[0], speedLabelPosition[1], SDL_ColourTextLabel);
|
||||||
|
|
||||||
// LOOP UNTIL GAME IS FINISHED
|
// LOOP UNTIL GAME IS FINISHED
|
||||||
while (1) {
|
while (1) {
|
||||||
// UPDATE THE SNAKE'S SCORE WHEN IT CHANGES
|
// UPDATE THE SNAKE'S SCORE WHEN IT CHANGES
|
||||||
if (atoi(tempString[0]) != (*gameParameters)[SnakeScore]) {
|
if (atoi(tempString[0]) != (*gameParameters)[SnakeScore]) {
|
||||||
if (atoi(tempString[0]) != -1) {
|
if (atoi(tempString[0]) != -1) {
|
||||||
drawText(screen, tempString[0], DEFAULT_FONT_SIZE, scoreDataPosition[0], scoreDataPosition[1], SDL_ColourBackground);
|
drawText(screen, tempString[0], DEFAULT_FONT_SIZE * (*gameParameters)[RenderSizeMultiplier], scoreDataPosition[0], scoreDataPosition[1], SDL_ColourBackground);
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf(tempString[0], "%d", (*gameParameters)[SnakeScore]);
|
sprintf(tempString[0], "%d", (*gameParameters)[SnakeScore]);
|
||||||
drawText(screen, tempString[0], DEFAULT_FONT_SIZE, scoreDataPosition[0], scoreDataPosition[1], SDL_ColourTextData);
|
drawText(screen, tempString[0], DEFAULT_FONT_SIZE * (*gameParameters)[RenderSizeMultiplier], scoreDataPosition[0], scoreDataPosition[1], SDL_ColourTextData);
|
||||||
}
|
}
|
||||||
|
|
||||||
// UPDATE THE SNAKE'S SPEED WHEN IT CHANGES
|
// UPDATE THE SNAKE'S SPEED WHEN IT CHANGES
|
||||||
if (atoi(tempString[1]) != (*gameParameters)[SnakeSpeed]) {
|
if (atoi(tempString[1]) != (*gameParameters)[SnakeSpeed]) {
|
||||||
if (atoi(tempString[1]) != -1) {
|
if (atoi(tempString[1]) != -1) {
|
||||||
drawText(screen, tempString[1], DEFAULT_FONT_SIZE, speedDataPosition[0], speedDataPosition[1], SDL_ColourBackground);
|
drawText(screen, tempString[1], DEFAULT_FONT_SIZE * (*gameParameters)[RenderSizeMultiplier], speedDataPosition[0], speedDataPosition[1], SDL_ColourBackground);
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf(tempString[1], "%d", (*gameParameters)[SnakeSpeed]);
|
sprintf(tempString[1], "%d", (*gameParameters)[SnakeSpeed]);
|
||||||
drawText(screen, tempString[1], DEFAULT_FONT_SIZE, speedDataPosition[0], speedDataPosition[1], SDL_ColourTextData);
|
drawText(screen, tempString[1], DEFAULT_FONT_SIZE * (*gameParameters)[RenderSizeMultiplier], speedDataPosition[0], speedDataPosition[1], SDL_ColourTextData);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gameEventPoll(screen, ppTiles, ppSprites, gameParameters, event) == false) {
|
if (gameEventPoll(screen, ppTiles, ppSprites, gameParameters, event) == false) {
|
||||||
|
@ -290,10 +290,10 @@ void gameLoop(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*ga
|
||||||
// DISPLAY GAME OVER MESSAGE AND WAIT FOR INPUT
|
// DISPLAY GAME OVER MESSAGE AND WAIT FOR INPUT
|
||||||
if ((*gameParameters)[QuitGame] == 0) {
|
if ((*gameParameters)[QuitGame] == 0) {
|
||||||
// DISPLAY GAMEOVER MESSAGES
|
// DISPLAY GAMEOVER MESSAGES
|
||||||
drawText(screen, gameOverMsg[0], DEFAULT_FONT_SIZE - 7, gameOverMsgPosition[0], gameOverMsgPosition[4], SDL_ColourTextGameOver);
|
drawText(screen, gameOverMsg[0], (DEFAULT_FONT_SIZE - 7) * (*gameParameters)[RenderSizeMultiplier], gameOverMsgPosition[0], gameOverMsgPosition[4], SDL_ColourTextGameOver);
|
||||||
drawText(screen, gameOverMsg[1], DEFAULT_FONT_SIZE - 9, gameOverMsgPosition[1], gameOverMsgPosition[4], SDL_ColourTextData);
|
drawText(screen, gameOverMsg[1], (DEFAULT_FONT_SIZE - 9) * (*gameParameters)[RenderSizeMultiplier], gameOverMsgPosition[1], gameOverMsgPosition[4], SDL_ColourTextData);
|
||||||
drawText(screen, gameOverMsg[2], DEFAULT_FONT_SIZE - 9, gameOverMsgPosition[2], gameOverMsgPosition[4], SDL_ColourTextGameOver);
|
drawText(screen, gameOverMsg[2], (DEFAULT_FONT_SIZE - 9) * (*gameParameters)[RenderSizeMultiplier], gameOverMsgPosition[2], gameOverMsgPosition[4], SDL_ColourTextGameOver);
|
||||||
drawText(screen, gameOverMsg[3], DEFAULT_FONT_SIZE - 9, gameOverMsgPosition[3], gameOverMsgPosition[4], SDL_ColourTextData);
|
drawText(screen, gameOverMsg[3], (DEFAULT_FONT_SIZE - 9) * (*gameParameters)[RenderSizeMultiplier], gameOverMsgPosition[3], gameOverMsgPosition[4], SDL_ColourTextData);
|
||||||
SDL_Flip(screen);
|
SDL_Flip(screen);
|
||||||
|
|
||||||
// WAIT A MOMENT TO ENSURE INPUT FROM THE GAME ISN'T CAUGHT
|
// WAIT A MOMENT TO ENSURE INPUT FROM THE GAME ISN'T CAUGHT
|
||||||
|
@ -316,27 +316,27 @@ void gameLoop(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*ga
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
drawText(screen, gameOverMsg[0], DEFAULT_FONT_SIZE - 7, gameOverMsgPosition[0], gameOverMsgPosition[4], SDL_ColourBackground);
|
drawText(screen, gameOverMsg[0], (DEFAULT_FONT_SIZE - 7) * (*gameParameters)[RenderSizeMultiplier], gameOverMsgPosition[0], gameOverMsgPosition[4], SDL_ColourBackground);
|
||||||
drawText(screen, gameOverMsg[1], DEFAULT_FONT_SIZE - 9, gameOverMsgPosition[1], gameOverMsgPosition[4], SDL_ColourBackground);
|
drawText(screen, gameOverMsg[1], (DEFAULT_FONT_SIZE - 9) * (*gameParameters)[RenderSizeMultiplier], gameOverMsgPosition[1], gameOverMsgPosition[4], SDL_ColourBackground);
|
||||||
drawText(screen, gameOverMsg[2], DEFAULT_FONT_SIZE - 9, gameOverMsgPosition[2], gameOverMsgPosition[4], SDL_ColourBackground);
|
drawText(screen, gameOverMsg[2], (DEFAULT_FONT_SIZE - 9) * (*gameParameters)[RenderSizeMultiplier], gameOverMsgPosition[2], gameOverMsgPosition[4], SDL_ColourBackground);
|
||||||
drawText(screen, gameOverMsg[3], DEFAULT_FONT_SIZE - 9, gameOverMsgPosition[3], gameOverMsgPosition[4], SDL_ColourBackground);
|
drawText(screen, gameOverMsg[3], (DEFAULT_FONT_SIZE - 9) * (*gameParameters)[RenderSizeMultiplier], gameOverMsgPosition[3], gameOverMsgPosition[4], SDL_ColourBackground);
|
||||||
SDL_Flip(screen);
|
SDL_Flip(screen);
|
||||||
}
|
}
|
||||||
|
|
||||||
drawText(screen, "SCORE", DEFAULT_FONT_SIZE, scoreLabelPosition[0], scoreLabelPosition[1], SDL_ColourBackground);
|
drawText(screen, "SCORE", DEFAULT_FONT_SIZE * (*gameParameters)[RenderSizeMultiplier], scoreLabelPosition[0], scoreLabelPosition[1], SDL_ColourBackground);
|
||||||
drawText(screen, "SPEED", DEFAULT_FONT_SIZE, speedLabelPosition[0], speedLabelPosition[1], SDL_ColourBackground);
|
drawText(screen, "SPEED", DEFAULT_FONT_SIZE * (*gameParameters)[RenderSizeMultiplier], speedLabelPosition[0], speedLabelPosition[1], SDL_ColourBackground);
|
||||||
|
|
||||||
if (atoi(tempString[0]) != -1) {
|
if (atoi(tempString[0]) != -1) {
|
||||||
drawText(screen, tempString[0], DEFAULT_FONT_SIZE, scoreDataPosition[0], scoreDataPosition[1], SDL_ColourBackground);
|
drawText(screen, tempString[0], DEFAULT_FONT_SIZE * (*gameParameters)[RenderSizeMultiplier], scoreDataPosition[0], scoreDataPosition[1], SDL_ColourBackground);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (atoi(tempString[1]) != -1) {
|
if (atoi(tempString[1]) != -1) {
|
||||||
drawText(screen, tempString[1], DEFAULT_FONT_SIZE, speedDataPosition[0], speedDataPosition[1], SDL_ColourBackground);
|
drawText(screen, tempString[1], DEFAULT_FONT_SIZE * (*gameParameters)[RenderSizeMultiplier], speedDataPosition[0], speedDataPosition[1], SDL_ColourBackground);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MOVES THE SNAKE IN THE DESIRED DIRECTION
|
// MOVES THE SNAKE IN THE DESIRED DIRECTION
|
||||||
void moveSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8], enum direction newDirection) {
|
void moveSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9], enum direction newDirection) {
|
||||||
int x;
|
int x;
|
||||||
|
|
||||||
// MOVEMENT
|
// MOVEMENT
|
||||||
|
@ -373,7 +373,7 @@ void moveSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*g
|
||||||
}
|
}
|
||||||
|
|
||||||
// DETECT+HANDLE WHEN THE SNAKE COLLIDES WITH WALLS, BLOCKS, ITSELF OR FOOD
|
// DETECT+HANDLE WHEN THE SNAKE COLLIDES WITH WALLS, BLOCKS, ITSELF OR FOOD
|
||||||
bool collisionDetect(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8], enum direction newDirection) {
|
bool collisionDetect(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9], enum direction newDirection) {
|
||||||
int x;
|
int x;
|
||||||
|
|
||||||
switch (newDirection) {
|
switch (newDirection) {
|
||||||
|
@ -454,7 +454,7 @@ bool collisionDetect(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, i
|
||||||
}
|
}
|
||||||
|
|
||||||
// HANDLES COLLISION WITH FOOD
|
// HANDLES COLLISION WITH FOOD
|
||||||
void collisionDetectFood(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8]) {
|
void collisionDetectFood(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9]) {
|
||||||
// INCREASE THE SNAKE'S SIZE IF IT'S NOT ALREADY THE MAXIMUM
|
// INCREASE THE SNAKE'S SIZE IF IT'S NOT ALREADY THE MAXIMUM
|
||||||
if ((*gameParameters)[SnakeLength] < MAX_SNAKELENGTH) {
|
if ((*gameParameters)[SnakeLength] < MAX_SNAKELENGTH) {
|
||||||
(*gameParameters)[SnakeLength]++;
|
(*gameParameters)[SnakeLength]++;
|
||||||
|
@ -476,7 +476,7 @@ void collisionDetectFood(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprite
|
||||||
}
|
}
|
||||||
|
|
||||||
// CAPTURES INPUT AND GENERATES APPROPRIATE RESPONSE
|
// CAPTURES INPUT AND GENERATES APPROPRIATE RESPONSE
|
||||||
bool gameEventPoll(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8], SDL_Event* event) {
|
bool gameEventPoll(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9], SDL_Event* event) {
|
||||||
bool playerAlive = true;
|
bool playerAlive = true;
|
||||||
|
|
||||||
if (SDL_PollEvent(event)) {
|
if (SDL_PollEvent(event)) {
|
||||||
|
@ -546,7 +546,7 @@ bool gameEventPoll(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int
|
||||||
}
|
}
|
||||||
|
|
||||||
// MOVES SNAKE AUTOMATICALLY IN WHICHEVER DIRECTION IT WENT LAST
|
// MOVES SNAKE AUTOMATICALLY IN WHICHEVER DIRECTION IT WENT LAST
|
||||||
bool scrollSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8]) {
|
bool scrollSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9]) {
|
||||||
bool playerAlive = true;
|
bool playerAlive = true;
|
||||||
|
|
||||||
switch ((*gameParameters)[SnakeDirection]) {
|
switch ((*gameParameters)[SnakeDirection]) {
|
||||||
|
@ -574,7 +574,7 @@ bool scrollSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (
|
||||||
}
|
}
|
||||||
|
|
||||||
// REDRAW THE SNAKE BASED ON CURRENT VALUES
|
// REDRAW THE SNAKE BASED ON CURRENT VALUES
|
||||||
void updateSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8]) {
|
void updateSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9]) {
|
||||||
int x;
|
int x;
|
||||||
|
|
||||||
for (x = (*gameParameters)[NPCCount]; x < (*gameParameters)[NPCCount] + (*gameParameters)[SnakeLength]; x++) {
|
for (x = (*gameParameters)[NPCCount]; x < (*gameParameters)[NPCCount] + (*gameParameters)[SnakeLength]; x++) {
|
||||||
|
@ -591,7 +591,7 @@ void updateSnake(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (
|
||||||
}
|
}
|
||||||
|
|
||||||
// DRAW NPCs BASED ON ON CURRENT VALUES
|
// DRAW NPCs BASED ON ON CURRENT VALUES
|
||||||
void loadNPCs(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[8]) {
|
void loadNPCs(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*gameParameters)[9]) {
|
||||||
int x, startNPCs = 0;
|
int x, startNPCs = 0;
|
||||||
|
|
||||||
for (x = startNPCs; x < (*gameParameters)[NPCCount]; x++) {
|
for (x = startNPCs; x < (*gameParameters)[NPCCount]; x++) {
|
||||||
|
@ -604,7 +604,7 @@ void loadNPCs(SDL_Surface* screen, SDL_Rect** ppTiles, int** ppSprites, int (*ga
|
||||||
}
|
}
|
||||||
|
|
||||||
// A HELPER FUNCTION TO RANDOMLY PLACE NPCs WITH SOME INTELLIGENCE
|
// A HELPER FUNCTION TO RANDOMLY PLACE NPCs WITH SOME INTELLIGENCE
|
||||||
void randomLocation(int** ppSprites, int (*gameParameters)[8], int* location[2]) {
|
void randomLocation(int** ppSprites, int (*gameParameters)[9], int* location[2]) {
|
||||||
int x, randLocation[2];
|
int x, randLocation[2];
|
||||||
bool isAcceptable = false;
|
bool isAcceptable = false;
|
||||||
|
|
||||||
|
@ -662,7 +662,7 @@ void updateRect(SDL_Surface* screen, SDL_Rect** ppTiles, int position[2], const
|
||||||
}
|
}
|
||||||
|
|
||||||
// PARSES COMMANDLINE OPTIONS AND GENERATES APPROPRIATE RESPONSE
|
// PARSES COMMANDLINE OPTIONS AND GENERATES APPROPRIATE RESPONSE
|
||||||
void configureGame(int argc, char** args, int (*gameParameters)[8]) {
|
void configureGame(int argc, char** args, int (*gameParameters)[9]) {
|
||||||
int parsecount = 1;
|
int parsecount = 1;
|
||||||
|
|
||||||
// SET DEFAULT GAME PARAMETERS
|
// SET DEFAULT GAME PARAMETERS
|
||||||
|
@ -674,6 +674,7 @@ void configureGame(int argc, char** args, int (*gameParameters)[8]) {
|
||||||
(*gameParameters)[SnakeLength] = DEFAULT_SNAKELENGTH;
|
(*gameParameters)[SnakeLength] = DEFAULT_SNAKELENGTH;
|
||||||
(*gameParameters)[SnakeDirection] = -1;
|
(*gameParameters)[SnakeDirection] = -1;
|
||||||
(*gameParameters)[SnakeScore] = 0;
|
(*gameParameters)[SnakeScore] = 0;
|
||||||
|
(*gameParameters)[RenderSizeMultiplier] = 1;
|
||||||
|
|
||||||
// PARSE COMMANDLINE FOR SETTINGS
|
// PARSE COMMANDLINE FOR SETTINGS
|
||||||
while (parsecount < argc) {
|
while (parsecount < argc) {
|
||||||
|
@ -738,6 +739,10 @@ void configureGame(int argc, char** args, int (*gameParameters)[8]) {
|
||||||
printErrorHelp(args[0]);
|
printErrorHelp(args[0]);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
} else if (strcmp(args[parsecount], "-2") == 0) {
|
||||||
|
// DOUBLE RESOLUTION
|
||||||
|
(*gameParameters)[RenderSizeMultiplier] = 2;
|
||||||
|
parsecount++;
|
||||||
} else {
|
} else {
|
||||||
// FAIL IF ANYTHING ELSE
|
// FAIL IF ANYTHING ELSE
|
||||||
printErrorHelp(args[0]);
|
printErrorHelp(args[0]);
|
||||||
|
@ -754,6 +759,7 @@ void printHelpMenu(char filename[]) {
|
||||||
fprintf(stdout, " -b [blocks]\t\tSet the number of blocks: between [%d] and [%d] (DEFAULT: [%d])\n", MIN_NPCCOUNT - 1, MAX_NPCCOUNT - 1, DEFAULT_NPCCOUNT - 1);
|
fprintf(stdout, " -b [blocks]\t\tSet the number of blocks: between [%d] and [%d] (DEFAULT: [%d])\n", MIN_NPCCOUNT - 1, MAX_NPCCOUNT - 1, DEFAULT_NPCCOUNT - 1);
|
||||||
fprintf(stdout, " -l [length]\t\tSet the snake's starting length: between [%d] and [%d] (DEFAULT: [%d])\n", MIN_SNAKELENGTH - 1, MAX_SNAKELENGTH - 1, DEFAULT_SNAKELENGTH - 1);
|
fprintf(stdout, " -l [length]\t\tSet the snake's starting length: between [%d] and [%d] (DEFAULT: [%d])\n", MIN_SNAKELENGTH - 1, MAX_SNAKELENGTH - 1, DEFAULT_SNAKELENGTH - 1);
|
||||||
fprintf(stdout, " -s [speed]\t\tSet the snake's starting speed: between [%d] and [%d] (DEFAULT: [%d])\n", MIN_SNAKESPEED, MAX_SNAKESPEED, DEFAULT_SNAKESPEED);
|
fprintf(stdout, " -s [speed]\t\tSet the snake's starting speed: between [%d] and [%d] (DEFAULT: [%d])\n", MIN_SNAKESPEED, MAX_SNAKESPEED, DEFAULT_SNAKESPEED);
|
||||||
|
fprintf(stdout, " -2\t\t\tDouble the size the game renders at\n");
|
||||||
fprintf(stdout, " -h\t\t\tDisplay help information\n");
|
fprintf(stdout, " -h\t\t\tDisplay help information\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue