Question

Game Description: Most of you have played a very interesting game “Snake” on your old Nokia...

Game Description:

Most of you have played a very interesting game “Snake” on your old Nokia phones (Black & White). Now it is your time to create it with more interesting colors and features. When the game is started a snake is controlled by up, down, left and right keys to eat food which appears on random locations. By eating food snake’s length increases one unit and player’s score increases by 5 points. Food disappears after 15 seconds and appears again on some other random location. The game also has hurdles placed at the random locations. The snake dies if it collides with itself or the hurdle. 1. Create board (hint: a 2D array) and snake. Snake first block (mouth) must be a circle and have at least 2 squares as the tail. 2. Move snake 3. Show player score on progress bar at top of the game. 4. Display five foods at a particular time at random locations on a board by following the following rules a. Not two food items are on same row. b. Not two food items are on the same columns. c. Not two food are along the same diagonal (primary and secondary). d. When the snake eats the food, the food item again appear by following the same conditions described above. e. The length of snake increases when it eats food. f. Each food disappears after 15 seconds of its placement. 5. Introduce the hurdles on board. a. You can have at least one hurdle and at most three. b. The snake should avoid the hurdles while navigating to eat food item. c. The hurdles disappear after 30 seconds and reappear again at different locations on the board. d. New hurdles should be placed such as they do not collide with already placed food items and snake. e. Hurdles can take “L-shape”, “U-shape”,”Z-shape”, and horizontal or vertical lines of variable lengths. f. You can increase or decrease the difficulty level using hurdles. 6. Game must terminate when snake collide with itself or with hurdles by showing a message game over and your score is “XYZ”. If score is highest show a message “you have broken all previous records and now highest score is xyz”. After this, start the game once again. 7. Display power food after each minute and it should disappear after 15 seconds. If player eats a power food its score is increased by 20. 8. Save highest score and players history in files and display corresponding information when user selects an option from them

: Snake game with snake and food. BONUS MARKS: When we open snake game we see a screen l which shows different options: Start Game, Resume Game, Change Level, See High Score and See Previous History of Game. When user select “Start Game” option first level of game started. You can also see high score and history of game from given options. Figure 2: Shows different options to start snake game How to Start: In this project you will be provided code of some functions to draw circle, rectangle, print string and code to control keys (up, down, left and right). Your main task will be to understand the main design of game and implement it. However, before proceeding with code writing you will need to install some required libraries. 1.1 Installing libraries on Linux (Ubuntu) You can install libraries either from the Ubuntu software center or from command line. We recommend command line and provide the file “install-libraries.sh” to automate the complete installation procedure.

Here is the code on which i have to work ! For previous question i posted

//============================================================================

// Name : Tetris.cpp

// Author : Sibt ul Hussain

// Version :

// Copyright : (c) Reserved

// Description : Basic 2D game of Tetris...

//============================================================================

#ifndef TETRIS_CPP_

#define TETRIS_CPP_

#include "util.h"

#include <iostream>

#include<vector>

#include<algorithm>

//#include<cstdlib>

#include<ctime>

#include<string>

//#include<sys/wait.h>

//#include<stdlib.h>

//#include<stdio.h>

#include<unistd.h>

#include<sstream>

#include<cmath> // for basic math functions such as cos, sin, sqrt

using namespace std;

/* Function sets canvas size (drawing area) in pixels...

* that is what dimensions (x and y) your game will have

* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)

* */

void SetCanvasSize(int width, int height) {

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.

glMatrixMode( GL_MODELVIEW);

glLoadIdentity();

}

/*

* Main Canvas drawing function.

* */

double startx=320,starty=400;

void Display(){

// set the background color using function glClearColotgr.

// to change the background play with the red, green and blue values below.

// Note that r, g and b values must be in the range [0,1] where 0 means dim rid and 1 means pure red and so on.

glClearColor(0/*Red Component*/, 0.0/*Green Component*/,

0.0/*Blue Component*/, 0 /*Alpha component*/);// Red==Green==Blue==1 --> White Colour

glClear(GL_COLOR_BUFFER_BIT); //Update the colors

  

DrawSquare( 300 , 300 ,100,colors[10]); //This will draw a square shape of size 100x100 at x=300 and y=300

DrawString( 50, 600, "Here are some are basic shapes", colors[MISTY_ROSE]); // this will print given string at x=50 , y=600

DrawString( 50, 570, "You will use these to make your game", colors[MISTY_ROSE]); // this will print given string at x=50 , y=570

DrawCircle( 200 , 200 , 50 , colors[5]); // This will draw a circle at x=200,y=200 of radius 50

// v1( x,y ) v2( x,y ) , v3( x,y )

DrawTriangle( 300, 50 , 500, 50 , 400 , 250, colors[MISTY_ROSE] ); // Trianlge Vertices v1(300,50) , v2(500,50) , v3(400,250)

  

//DrawLine(int x1, int y1, int x2, int y2, int lwidth, float *color)

DrawLine( 550 , 50 , 550 , 600 , 20 , colors[MISTY_ROSE] );

glutSwapBuffers(); // do not modify this line..

}

/*This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)

* is pressed from the keyboard

*

* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...

*

* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the

* program coordinates of mouse pointer when key was pressed.

*

* */

void NonPrintableKeys(int key, int x, int y) {

if (key == GLUT_KEY_LEFT /*GLUT_KEY_LEFT is constant and contains ASCII for left arrow key*/) {

// what to do when left key is pressed...

  

  

} else if (key == GLUT_KEY_RIGHT /*GLUT_KEY_RIGHT is constant and contains ASCII for right arrow key*/) {

} else if (key == GLUT_KEY_UP) /*GLUT_KEY_UP is constant and contains ASCII for up arrow key*/ {

  

}

else if (key == GLUT_KEY_DOWN) /*GLUT_KEY_DOWN is constant and contains ASCII for down arrow key*/ {

}

  

/* This function calls the Display function to redo the drawing. Whenever you need to redraw just call

* this function*/

glutPostRedisplay();

}

/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)

* is pressed from the keyboard

* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the

* program coordinates of mouse pointer when key was pressed.

* */

void PrintableKeys(unsigned char key, int x, int y) {

if (key == KEY_ESC/* Escape key ASCII*/) {

exit(1); // exit the program when escape key is pressed.

}

if (key == 'R' || key=='r'/* Escape key ASCII*/) {

//exit(1); // exit the program when escape key is pressed.

//aswangle+=90;

}

  

else if (int(key) == 13)

{  

}

  

glutPostRedisplay();

}

/*

* This function is called after every 1000.0/FPS milliseconds

* (FPS is defined on in the beginning).

* You can use this function to animate objects and control the

* speed of different moving objects by varying the constant FPS.

*

* */

void Timer(int m) {

// implement your functionality here

glutPostRedisplay();

// once again we tell the library to call our Timer function after next 1000/FPS

glutTimerFunc(1000.0 / FPS, Timer, 0);

}

/*

* our gateway main function

* */

int main(int argc, char*argv[]) {

int width = 650, height = 650; // i have set my window size to be 800 x 600

InitRandomizer(); // seed the random number generator...

glutInit(&argc, argv); // initialize the graphics library...

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode

glutInitWindowPosition(50, 50); // set the initial position of our window

glutInitWindowSize(width, height); // set the size of our window

glutCreateWindow("PF's Snake Game"); // set the title of our game window

SetCanvasSize(width, height); // set the number of pixels...

// Register your functions to the library,

// you are telling the library names of function to call for different tasks.

//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.

glutDisplayFunc(Display); // tell library which function to call for drawing Canvas.

glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters

glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters

// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...

glutTimerFunc(5.0 / FPS, Timer, 0);

// now handle the control to library and it will call our registered functions when

// it deems necessary...

glutMainLoop();

return 1;

}

#endif /* Snake Game */

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<ncurses.h>
#include<unistd.h>
#include<time.h>
#include<stdlib.h>

class snake //to store the x and y coordinates of each snake part
{
int x, y;
char ch;
public:
snake(){ x= y= 0; ch='O';}
snake(int a, int b)
{
x= a; y= b; ch= 'O';
}
snake(const snake &ekans)
{
ch= ekans.ch;
x= ekans.x;
y= ekans.y;
}
void setCh(char x)
{
ch= x;
}
char getCh()
{
return ch;
}
int getX()
{
return x;
}
void setX(int no)
{
x= no;
}
int getY()
{
return y;
}
void setY(int no)
{
y= no;
}
};

class list //to store each snake body
{
node* head;
int length;
public:
list()
{
head= NULL;
length= 0;
}
void add(snake s) //adds at the end
{
node *n= new node;
n->setSnake(s);
n->setNext(head);
head= n;
length++;
}
int listLength()
{
return length;
}
snake get(int n)
{
node *temp =head;
int count= 1;
while(count!= n && temp!= NULL)
{
count++;
temp= temp->getNext();
}
return temp->getSnake();
}
void remove() //removes the first element
{
node *temp= head->getNext();
node *t2= head;
while(temp->getNext()!=NULL)
{
temp=temp->getNext();
t2= t2->getNext();
}
t2->setNext(NULL);
delete temp;
length--;
}
void display()
{
int i= 0;
node *temp= head;
while(temp!= NULL)
{
mvaddch(10,10+i,temp->getSnake().getCh());
mvprintw(20,5,"THIS LOVE");
refresh();
temp= temp->getNext();
}
}
~list()
{
while(head!= NULL)
{
node* n= head;
head= head->getNext();
delete n;
}
length= 0;
}
};

class game
{   
int score, max_y, max_x, food_x, food_y, direction;
list l;
public:
game()
{
score= max_y= max_x= food_x= food_y= 0;
direction= 2;
}
void launch();
void play();
void map();
void genFood();
void setSnake();
void moveSnake(int dir);
bool check();
};

void game:: launch()
{
initscr(); //initialize ncurses
cbreak(); //no line buffering
curs_set(FALSE);
keypad(stdscr, TRUE);
noecho();
refresh();
attron(A_BOLD);
mvprintw(LINES/2-2, COLS/2-10, "SNAKES WELCOMES YOU");
mvprintw(LINES/2, COLS/2-12, "PRESS ENTER TO CONTINUE");
int ch;
if((ch= getch())==10)
{
getmaxyx(stdscr, max_y, max_x);
play();
}
else
{
clear();
mvprintw(LINES/2, COLS/2- 16, "YOU DID NOT EVEN GIVE ME A CHANCE......GOODBYE");
refresh();
sleep(2);
}
attroff(A_BOLD);
endwin();
}

void game:: genFood()
{
srand(time(NULL));
food_y= random()%(max_y-6)+4;
food_x= random()%(max_x-4)+2;
}

void game:: map()
{
box(stdscr, 0, 0);
mvprintw(1,1, "SCORE: ");
mvprintw(1,8, "%d",score);
mvprintw(2,1, "Press q to quit");
refresh();
}

void game:: setSnake()
{
clear();
map();
for(int i= 0; i< 8; i++)
{
snake s((COLS/2)-8+i, LINES/2);
l.add(s);
mvprintw(s.getY(),s.getX(),"%c",s.getCh());
}
mvprintw(food_y, food_x, "F");
refresh();
}

void game::play()
{
bool b;
genFood();
setSnake();
mvprintw(8, COLS/2-20, "??????????READY??????????????");
refresh();
sleep(1);
int ch= 0;
timeout(50); //adjust speed of the game
while((ch=getch())!= 'q')
{
switch(ch)
{
case KEY_UP: direction= 1;
break;
case KEY_DOWN: direction= 3;
break;
case KEY_RIGHT: direction= 2;
break;
case KEY_LEFT: direction= 4;
break;
}
clear();
map();
mvprintw(food_y, food_x,"F");
moveSnake(direction);
refresh();
b= check();
if(!b)
{
clear();
mvprintw(max_y/2-2, max_x/2-8, "GAME OVER");
mvprintw(max_y/2, max_x/2-10, "YOUR SCORE %d",score);
refresh();
sleep(2);
break;
}
}
}

bool game:: check()
{
bool b= false;
snake s= l.get(1);
int head_x= s.getX();
int head_y= s.getY();
if(head_x== max_x-1 || head_y== 1 || head_x== 1 || head_y== max_y-1)
return false;
else
{
int len= l.listLength();
for(int i= 4; i< len; i++)
{
s= l.get(i);
if(head_x== s.getX() && head_y== s.getY())
{
b= true;
break;
}
}
if(b)
return false;
else
return true;
}
}

void game:: moveSnake(int dir)
{
snake k;
k= l.get(1);
int x= k.getX(), y= k.getY();
//mvprintw(6,1, "BEFORE x= %d, y= %d, dir=%d",x,y,dir);
if(dir== 1) y--;
else if(dir== 2) x++;
else if(dir== 3) y++;
else x--;
//mvprintw(7,1, "AFTER x=%d, y= %d", x, y);
snake s(x,y);
l.add(s);
if(x==food_x && y==food_y)
{
score++;
genFood();
mvprintw(food_y, food_x, "F");
}
else
l.remove();
int len= l.listLength();
for(int i= 1; i<= len; i++)
{
k= l.get(i);
mvaddch(k.getY(), k.getX(), k.getCh());
refresh();
}
}

int main()
{
game g;
g.launch();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Game Description: Most of you have played a very interesting game “Snake” on your old Nokia...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Your will write a class named Gasket that can be used to build Gasket objects (that...

    Your will write a class named Gasket that can be used to build Gasket objects (that represent Sierpinski gaskets) that you can display graphically.  Your Gasket class is defined in the provided file Gasket.h.   DO NOT CHANGE the file Gasket.h.  The attributes and methods defined for class Gasket are described below.     ·sideLength            an int that holds the length of each side of the Gasket.  The length of the side is measured as a number of pixels ·xLocation              an int that holds the x coordinate (in pixels)...

  • I am doing an Arduino Uno project where I made a "Simon says" memory game with 3 neopixel LED str...

    I am doing an Arduino Uno project where I made a "Simon says" memory game with 3 neopixel LED strips and 3 - ultrasonics. I have them working independently but I need to combine the code so they work together. Here is what I have: Memory Game #define PLAYER_WAIT_TIME 2000 // The time allowed between button presses - 2s byte sequence[100]; // Storage for the light sequence byte curLen = 0; // Current length of the sequence byte inputCount =...

  • given below are the project description and their Html and css files i need javascript according...

    given below are the project description and their Html and css files i need javascript according to the project and other files! WEB230 - JavaScript 1 Assignment 6b - Event Delegation Before starting, study the HTML and open it in a browser so that you understand the structure of the document. You will add functionality to perform several tasks in our shopping list app. Clicking the red "X" at the right of an item will delete that item. Clicking on...

  • so i have my c++ code and ive been working on this for hours but i...

    so i have my c++ code and ive been working on this for hours but i cant get it to run im not allowed to use arrays. im not sure how to fix it thank you for the help our job is to write a menu driven program that can convert to display Morse Code ere is the menu the program should display Menu Alphabet Initials N-Numbers - Punctuations S = User Sentence Q- Quit Enter command the user chooses...

  • The following are screen grabs of the provided files Thanks so much for your help, and have a n...

    The following are screen grabs of the provided files Thanks so much for your help, and have a nice day! My Java Programming Teacher Gave me this for practice before the exam, butI can't get it to work, and I need a working version to discuss with my teacher ASAP, and I would like to sleep at some point before the exam. Please Help TEST QUESTION 5: Tamagotchi For this question, you will write a number of classes that you...

  • I'm not getting out put what should I do I have 3 text files which is...

    I'm not getting out put what should I do I have 3 text files which is in same folder with main program. I have attached program with it too. please help me with this. ------------------------------------------------------------------------------------ This program will read a group of positive numbers from three files ( not all necessarily the same size), and then calculate the average and median values for each file. Each file should have at least 10 scores. The program will then print all the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT