Updated: fixed some issues

This commit is contained in:
Lian Drake 2023-12-08 05:15:13 -04:00
parent 62a2ee9db3
commit 2559d5dfb8
2 changed files with 15 additions and 22 deletions

BIN
serpent

Binary file not shown.

View file

@ -30,16 +30,17 @@
#include "serpent.h" #include "serpent.h"
/* */ /* */
/* global variables */ /* Global variables */
bool isAlive = true; /* variable to track if snake is alive */ bool isAlive = true; /* variable to track if snake is alive */
bool isRunning = true; bool isRunning = true; /* variable to track if the game is running */
unsigned int SPEED = 100; /* speed of the game */ const unsigned int minSpeed = 100; /* default speed of the game (lower is faster) */
unsigned int MAX_SPEED = 60; /* max speed of the game */ const unsigned int maxSpeed = 70; /* max speed of the game (lower is faster) */
unsigned int terminalRows; unsigned int speed = minSpeed; /* variable speed of the game (lower is faster) */
unsigned int terminalCols; unsigned int terminalRows; /* variable for storing the terminal rows */
int startY; unsigned int terminalCols; /* variable for storing the terminal columns */
int startX; int startY; /* initial Y position of the window */
unsigned int score; int startX; /* initial X position of the window */
unsigned int score; /* game score */
/* */ /* */
/* Initialize structs */ /* Initialize structs */
@ -48,27 +49,22 @@ Apple *apple;
/* */ /* */
/* Function prototypes */ /* Function prototypes */
// structure functions
Snake *startSnake(); Snake *startSnake();
Apple *startApple(); Apple *startApple();
void appendSnakeNode(Snake *new_snake); void appendSnakeNode(Snake *new_snake);
void freeSnake(); void freeSnake();
int snakeSize(); int snakeSize();
// game logic
void updateSnake(); void updateSnake();
void updateApple(); void updateApple();
bool snakeCollision(int x, int y, bool excludeHead); bool snakeCollision(int x, int y, bool excludeHead);
bool appleCollision(int x, int y); bool appleCollision(int x, int y);
// game i-o
void handleInput(int key); void handleInput(int key);
void drawGame(); void drawGame();
// game functions
void initializeGame(); void initializeGame();
void gameLoop(); void gameLoop();
void run(); void run();
void mainMenu(WINDOW *menuScreen, int menuType); void mainMenu(WINDOW *menuScreen, int menuType);
void cleanup(); void cleanup();
// command line functions
void argControls(); void argControls();
void argHelp(); void argHelp();
void argVersion(); void argVersion();
@ -263,8 +259,8 @@ void updateSnake() {
updateApple(); updateApple();
/* Increase the speed if it hasn't reached the maximum */ /* Increase the speed if it hasn't reached the maximum */
if (SPEED >= MAX_SPEED) { if (speed >= maxSpeed) {
SPEED -= 1; speed -= 1;
} }
/* Create a new head node and update its position */ /* Create a new head node and update its position */
@ -392,10 +388,6 @@ void drawGame() {
/* Clear the terminal screen */ /* Clear the terminal screen */
erase(); erase();
// Calculate the center coordinates
int startY = (terminalRows - SCREEN_HEIGHT) / 2;
int startX = (terminalCols - SCREEN_WIDTH) / 2;
WINDOW *gameBoard = newwin(SCREEN_HEIGHT, SCREEN_WIDTH, startY, startX); WINDOW *gameBoard = newwin(SCREEN_HEIGHT, SCREEN_WIDTH, startY, startX);
box(gameBoard, 0, 0); box(gameBoard, 0, 0);
refresh(); refresh();
@ -453,7 +445,7 @@ void gameLoop() {
refresh(); refresh();
/* Introduce a delay for the game loop */ /* Introduce a delay for the game loop */
usleep(SPEED * 800L); usleep(speed * 800L);
} }
/* Function responsible of initializing the game */ /* Function responsible of initializing the game */
@ -503,6 +495,7 @@ void run() {
if (!isAlive) { if (!isAlive) {
snake = startSnake(); snake = startSnake();
isAlive = true; isAlive = true;
speed = minSpeed;
} }
/* Start the game loop */ /* Start the game loop */
while (isAlive) { while (isAlive) {