Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Using onlinegdb.com and the following code, please add the trophies into the snake game and make sure all the requirments are complete. code: #include #include
Using onlinegdb.com and the following code, please add the trophies into the snake game and make sure all the requirments are complete.
code:
#include
#include
#include
#include
#define DELAY
#define TIMEOUT
typedef enum
LEFT,
RIGHT,
UP
DOWN
directiontype;
typedef struct point
int x;
int y;
point;
int x
y
maxY
maxX
nextX
nextY
tailLength ;
bool gameOver false;
directiontype currentDir RIGHT;
point snakeParts;
Function to draw a part of the snake on the screen
void drawPartpoint drawPoint
mvprintwdrawPointy drawPoint.xo;
Function to initialize the curses library for drawing the game screen
void cursesInit
initscr;
noecho;
keypadstdscr TRUE;
cbreak;
timeoutTIMEOUT;
curssetFALSE;
Global var stdscr is created by the call to initscr
getmaxyxstdscr maxY, maxX;
Function to initialize the game state
void init
srandtimeNULL;
currentDir RIGHT;
tailLength ;
gameOver false;
clear;
Set the initial snake coordinates
int j ;
for int i tailLength; i ; i
point currPoint;
currPoint.x i;
currPoint.y maxY ; Start mid screen on the y axis
snakePartsj currPoint;
j;
refresh;
Function to shift the snake's position by updating its parts
void shiftSnake
point tmp snakePartstailLength ;
for int i tailLength ; i ; i
snakePartsi snakePartsi ;
snakeParts tmp;
Function to draw the game screen, including borders and the snake
void drawScreen
clear;
Draws the border Nathan
for int i ; i maxY; i
mvprintwi#;
mvprintwi maxX #;
for int i ; i maxX; i
mvprintw i#;
mvprintwmaxY i#;
if gameOver
mvprintwmaxY maxX "Game Over!";
Draws snake to screen
for int i ; i tailLength; i
drawPartsnakePartsi;
refresh;
usleepDELAY;
int mainint argc, char argv
cursesInit;
init;
int ch;
while
getmaxyxstdscr maxY, maxX;
if gameOver
sleep;
init;
Inputs
ch getch;
if ch l ch L ch KEYRIGHT && currentDir RIGHT && currentDir LEFT
currentDir RIGHT;
else if ch h ch H ch KEYLEFT && currentDir RIGHT && currentDir LEFT
currentDir LEFT;
else if ch j ch J ch KEYDOWN && currentDir UP && currentDir DOWN
currentDir DOWN;
else if ch k ch K ch KEYUP && currentDir UP && currentDir DOWN
currentDir UP;
Game over if snake tries to reverse
if ch l ch L ch KEYRIGHT && currentDir LEFTch h ch H ch KEYLEFT && currentDir RIGHTch j ch J ch KEYDOWN && currentDir UPch k ch K ch KEYUP && currentDir DOWN
gameOver true;
Movement
nextX snakePartsx;
nextY snakePartsy;
if currentDir RIGHT
nextX;
else if currentDir LEFT
nextX;
else if currentDir UP
nextY;
else if currentDir DOWN
nextY;
Game Over if the player hits the screen edges
if nextX maxX nextX nextY maxY nextY
gameOver true;
Shift all the snake parts Brandon
shiftSnake;
snakePartsx nextX;
snakePartsy nextY;
Game Over if the player collides with itself
for int i ; i tailLength; i
if nextX snakePartsix && nextY snakePartsiy
gameOver true;
break;
drawScreen;
endwin;
nocbreak;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started