Answered step by step
Verified Expert Solution
Link Copied!

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 80000
#define TIMEOUT 10
typedef enum {
LEFT,
RIGHT,
UP,
DOWN
} direction_type;
typedef struct point {
int x;
int y;
} point;
int x =0,
y =0,
maxY =0,
maxX =0,
nextX =0,
nextY =0,
tailLength =5;
bool gameOver = false;
direction_type currentDir = RIGHT;
point snakeParts[255]={};
// Function to draw a part of the snake on the screen
void drawPart(point drawPoint){
mvprintw(drawPoint.y, drawPoint.x,"o");
}
// Function to initialize the curses library for drawing the game screen
void cursesInit(){
initscr();
noecho();
keypad(stdscr, TRUE);
cbreak();
timeout(TIMEOUT);
curs_set(FALSE);
//Global var stdscr is created by the call to initscr()
getmaxyx(stdscr, maxY, maxX);
}
// Function to initialize the game state
void init(){
srand(time(NULL));
currentDir = RIGHT;
tailLength =5;
gameOver = false;
clear();
// Set the initial snake coordinates
int j =0;
for (int i = tailLength; i >=0; i--){
point currPoint;
currPoint.x = i;
currPoint.y = maxY /2; // Start mid screen on the y axis
snakeParts[j]= currPoint;
j++;
}
refresh();
}
// Function to shift the snake's position by updating its parts
void shiftSnake(){
point tmp = snakeParts[tailLength -1];
for (int i = tailLength -1; i >0; i--){
snakeParts[i]= snakeParts[i -1];
}
snakeParts[0]= tmp;
}
// Function to draw the game screen, including borders and the snake
void drawScreen(){
clear();
// Draws the border (Nathan)
for (int i =0; i maxY; i++){
mvprintw(i,0,"#");
mvprintw(i, maxX -1,"#");
}
for (int i =0; i maxX; i++){
mvprintw(0, i,"#");
mvprintw(maxY -1, i,"#");
}
if (gameOver)
mvprintw(maxY /2, maxX /2-5, "Game Over!");
// Draws snake to screen
for (int i =0; i tailLength; i++){
drawPart(snakeParts[i]);
}
refresh();
usleep(DELAY);
}
int main(int argc, char *argv[]){
cursesInit();
init();
int ch;
while (1){
getmaxyx(stdscr, maxY, maxX);
if (gameOver){
sleep(2);
init();
}
// Inputs
ch = getch();
if ((ch =='l'|| ch =='L'|| ch == KEY_RIGHT) && (currentDir != RIGHT && currentDir != LEFT)){
currentDir = RIGHT;
} else if ((ch =='h'|| ch =='H'|| ch == KEY_LEFT) && (currentDir != RIGHT && currentDir != LEFT)){
currentDir = LEFT;
} else if ((ch =='j'|| ch =='J'|| ch == KEY_DOWN) && (currentDir != UP && currentDir != DOWN)){
currentDir = DOWN;
} else if ((ch =='k'|| ch =='K'|| ch == KEY_UP) && (currentDir != UP && currentDir != DOWN)){
currentDir = UP;
}
// Game over if snake tries to reverse
if (((ch =='l'|| ch =='L'|| ch == KEY_RIGHT) && (currentDir == LEFT))||((ch =='h'|| ch =='H'|| ch == KEY_LEFT) && (currentDir == RIGHT))||((ch =='j'|| ch =='J'|| ch == KEY_DOWN) && (currentDir == UP))||((ch =='k'|| ch =='K'|| ch == KEY_UP) && (currentDir == DOWN))){
gameOver = true;
}
// Movement
nextX = snakeParts[0].x;
nextY = snakeParts[0].y;
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-1|| nextX =0)||(nextY >= maxY-1|| nextY =0)){
gameOver = true;
}
// Shift all the snake parts (Brandon)
shiftSnake();
snakeParts[0].x = nextX;
snakeParts[0].y = nextY;
// Game Over if the player collides with itself
for (int i =1; i tailLength; i++){
if (nextX == snakeParts[i].x && nextY == snakeParts[i].y){
gameOver = true;
break;
}
}
drawScreen();
}
endwin();
nocbreak();
return 0;
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

Students also viewed these Databases questions

Question

Describe how a leader can avoid conducting nonproductive meetings.

Answered: 1 week ago

Question

2. Ask several students to explain the directions.

Answered: 1 week ago