SNAKEGAME
SNAKEGAME
SNAKEGAME
CHAPTER-1
INTRODUCTION
Also in 1961 another student at MIT, Steve Russell, created the first video game, Space
war. E. E. Zajac, a scientist at Bell Telephone Laboratory (BTL), created a film called
“Simulation of a two-giro gravity attitude control system” in 1963. In this computer generated
film, Zajac showed how the attitude of a satellite could be altered as it orbits the Earth. Many of
the most important early breakthroughs in computer graphics research occurred at the University
of Utah in the 1970s.
The first major advance in 3D computer graphics was created at UU by these early
pioneers, the hidden-surface algorithm. In order to draw a representation of a 3D object on the
screen, the computer must determine which surfaces are “behind” the object from the viewer’s
perspective, and thus should be “hidden” when the computer creates (or renders) the image.
Graphics and application processing were increasingly migrated to the intelligence in the
workstation, rather than continuing to rely on central mainframe and mini-computers. 3D
graphics became more popular in the 1990s in gaming, multimedia and animation. Computer
graphics used in films and video games gradually began to be realistic to the point of entering the
uncanny valley. Examples include the later Final Fantasy games and animated films like The
Polar Express.
The OpenGL specification describes an abstract API for drawing 2D and 3D graphics.
Although it's possible for the API to be implemented entirely in software, it's designed to be
implemented mostly or entirely in hardware.
OpenGL is an evolving API. New versions of the OpenGL specification are regularly
released by the Khronos Group, each of which extends the API to support various new features.
In addition to the features required by the core API, GPU vendors may provide additional
functionality in the form of extensions.
Extensions may introduce new functions and new constants, and may relax or remove
restrictions on existing OpenGL functions. Vendors can use extensions to expose custom APIs
without needing support from other vendors or the Khronos Group as a whole, which greatly
increases the flexibility of OpenGL. All extensions are collected in, and defined by, the OpenGL
Registry.
DISPLAY OF INFORMATION: More than 4000 years ago, the Babylonians developed
floor. plans of buildings on stones. Today, the same type of information is generated by
architects using computers. Over the past 150 years, workers in the field of statistics have
explored techniques for generating plots. Now, we have computer plotting packages.
Supercomputers now allow researchers in many areas to solve previously intractable
problems. Thus, Computer Graphics has innumerable applications.
DESIGN: Professions such as engineering and architecture are concerned with design.
Today, the use of interactive graphical tools in CAD, in VLSI circuits, characters for
animation have developed in a great way.
SIMULATION AND ANIMATION: One of the most important uses has been in pilots‟
training. Graphical flight simulators have proved to increase safety and reduce expenses.
Simulators can be used for designing robots, plan it’s path, etc. Video games and
animated movies can now be made with low expenses.
USER INTERFACES: Our interaction with computers has become dominated by a visual
paradigm. The users’ access to internet is through graphical network browsers. Thus
Computer Graphics plays a major role in all fields.
The API is typically used to interact with a GPU, to achieve hardware accelerated
rendering.
i. Geometric Primitives.
ii. Image(Raster) Primitives.
Geometric primitives are specified in the problem domain and include points, line segments,
polygons, curves and surfaces.
Raster primitives, such as arrays of pixels pass through a separate parallel pipeline on their
way to the frame buffer.
CHAPTER-2
REQUIREMENT SPECIFICATION
The graphics editor has been programmed in C. It makes use of Turbo C Graphics library
package for creating the user interface. This is a subroutine library for terminal independent
screen painting and input event handling.
CHAPTER-3
ABOUT THE PROJECT
INTRODUCTION TO THE PROJECT:
“INTERACTIVE SNAKE GAME” is a single-player puzzle video game. The objective
of the game is to clear a rectangular board containing hidden “MINES” or bombs without
detonating any of them, with help from clues about the number of neighboring mines in each
field. The game originates from the 1960’s, and has been written for many computing platforms
in use today. It has many variations abd offshoots.
The Interactive Snake Game is a mini project that aims to recreate the classic game of
Snake using the OpenGL graphics library. The project focuses on providing an immersive and
visually appealing gaming experience while incorporating user interaction and game mechanics
to enhance the gameplay.
DESCRIPTION:
The Interactive Snake Game project utilizes the power of OpenGL to create a dynamic
and engaging gaming environment. The game screen is rendered using OpenGL's graphics
functions, which allows for smooth animations and vibrant visuals. The game consists of a
snake, represented by a series of connected spheres, and food items that appear at random
locations on the screen.
It has a very basic gameplay style. In its original form, mines are scattered throughout a
board. This board is divided into cells, which have two stages: uncovered and covered. A
covered cell is blank and clickable, while an uncovered cell is exposed, either containing a
number or a mine. When a cell is uncovered by a player click, and it bears a bomb a mine, the
game ends.
The player controls the snake's movement using arrow keys. As the snake moves, it
continuously grows in length, and the player's objective is to guide the snake to eat the food
items, increasing its score. The game incorporates collision detection mechanisms to ensure that
the snake does not collide with itself, which would result in the game ending.
CHAPTER-4
DESIGN
INITIALIZATION:
System design is the process of defining the architecture, components, modules,
interfaces, and data for a system to satisfy specified requirements. Systems design could see it as
the application of systems theory to product development. There is some overlap with the
disciplines of analysis, system and systems engineering.
The system design of a system pertains to an abstract representation of the data flows,
inputs and outputs of the system. This is often conducted via modeling, using an over abstract
(and sometimes graphical) model of the actual system. It relates to the actual input and output
processes of the system. This is laid down in terms of how data is input into a system, how it is
verified/authenticated, how it is processed, and how it is displayed as output. Systems design can
generally be broken down into three sub-tasks;
User Interface Design is concerned with how users add information to the system and with
how the system presents information back to them. Data Design is concerned with how the data
is represented and stored within the system. Finally, Process Design is concerned with how data
moves through the system, and with how and where it is validated, secured and/or transformed as
it flows into, through and out of the system. At the end of the systems design phase,
documentation describing the three sub-tasks is produced and made available for use in the next
phase.
This project aims to demonstrate the user interface by using keyboard and mouse I/O
devices. When a predefined key is pressed, the corresponding action takes place. In an OpenGL
project we would like to create a scene through which we are demonstration of Minesweeper.
FLOWCHART:
CHAPTER-5
IMPLEMENTATION
5.1 BUILT-IN FUNCTIONS:
1) glColor3f() - Used to display color.
11) glutCreateWindow() - Creates a top level window with the window name as specified.
12) glutDisplayFunc(display) - Sets the display call back for the current window.
13) glutReshapeFunc(reshape) - Sets the reshape call back for the current window.
14) glutMainLoop() - Enters the GLUT event processing loop. This routine should be called
atmost once in a GLUT program. Once called, this routine will never return. It will calls
necessary any callbacks that have been registered.
#define UP 1
#define Down 2
#define LEFT 3
#define RIGHT 4
// Status Variables
GLint lvl = 1;
GLint points = 0;
GLint size = 0;
GLbyte gameOver = true;
GLbyte EnableLight = true;
// Snake Variables
GLint bodyPos[2][100] = {{}};
GLint _x = 5;
GLint _z = 10;
GLint _oldX[2] = {};
GLint _oldZ[2] = {};
GLbyte direction = 0;
// Food Variables
GLint _bx = 0;
GLint _bz = 0;
// Screen variables
GLint _w = 800;
GLint _h = 550;
GLint _Giw = 0;
GLint _Gih = 0;
GLint _Gfw = 150;
GLint _Gfh = 150;
glRasterPos2f(-60, 20);
Write("WELCOME");
glRasterPos2f(-60, 10);
Write("Simulation of Snake Game");
glRasterPos2f(-60, 0);
Write("To Start Playing please press 'S' or 's' ");
}
void DrawSnake(){
int i;
//Drawing the head & the plane
glPushMatrix();
ManipulateViewAngle();
//This will draw the plane that the snake will run on.
glPushMatrix();
//glColor3f(1, 0.7, 0.2);
glColor3f(0.0, 0.6, 0.2);
glTranslatef(75.0, -16.0, 75.0);
glScalef(155,5.0,155);
glutSolidCube(1);
glPopMatrix();
//Here we will draw the Head of the snake
glColor3f(1,0,0);//Color it red
glTranslatef(_x,-10.0,_z);//Give it the location according to _x & _z
glScalef(0.5,0.5,0.5);
glutSolidSphere(10,20,20);//Draw the head as a sphere a litle bit bigger than the body spheres
}
//Generates Random Numbers for the location of the food that the snake will eat
int RandomNumber(int high, int low)
{
return (rand() % (high-low))+low;
}
//Generate the New Food that the snake will eat
void newFood(){
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
_bx = RandomNumber(_Gfw-_Giw, _Giw+10);
_bz = RandomNumber(_Gfh-_Gih, _Gih+10);
}
//This Function Will Check for Collision
bool collision(){
int i;
for(i=0; i<size; i++){
if((bodyPos[0][i] == _x && bodyPos[1][i] == _z) ||
//((bodyPos[0][i] >= _x) && (bodyPos[0][i] <= _x + 5) && (bodyPos[1][i] >= _z) &&
(bodyPos[1][i] <= _z + 5)) ||
//((bodyPos[0][i] <= _x) && (bodyPos[0][i] >= _x - 5) && (bodyPos[1][i] <= _z) &&
(bodyPos[1][i] >= _z - 5)) ||
//((bodyPos[0][i] <= _x) && (bodyPos[0][i] >= _x - 5) && (bodyPos[1][i] >= _z) &&
(bodyPos[1][i] <= _z + 5)) ||
((bodyPos[0][i] >= _x) && (bodyPos[0][i] <= _x + 5) && (bodyPos[1][i] <= _z) &&
(bodyPos[1][i] >= _z - 5)))
return true;
}
return false;
}
//This Function will move the snake according to the directions
//Taken from the Keyboard keys
void Run(int value){
int i;
_oldX[1] = _x;
_oldZ[1] = _z;
switch(direction){
case RIGHT:
headRotation =90;
_x += 6;
if(_x > _Gfw-2) _x = _Giw-1;//This will check if the snake is going into the border so it
will appear on the other side
break;
case LEFT:
headRotation =-90;
_x -= 6;
if(_x < 0) _x = _Gfw-2;//This will check if the snake is going into the border so it will
appear on the other side
break;
case UP:
headRotation =0;
_z += 6;
if(_z > _Gfh-2) _z = _Gih-1;//This will check if the snake is going into the border so it will
appear on the other side
break;
case Down:
headRotation =180;
_z -= 6;
if(_z < 2) _z = _Gfh-2;//This will check if the snake is going into the border so it will
appear on the other side
break;
}
//Checks for Collisoin if yes Game Over
if(collision()) gameOver = true;
//Checks if the snake ate the food (check the X and Y)
// If yes it will increase the points & the size of the snake & create a new food
if((_x == _bx && _z == _bz) ||
((_x >= _bx) && (_x <= _bx + 4) && (_z >= _bz) && (_z <= _bz + 4)) ||
((_x <= _bx) && (_x >= _bx - 4) && (_z <= _bz) && (_z >= _bz - 4)) ||
((_x <= _bx) && (_x >= _bx - 4) && (_z >= _bz) && (_z <= _bz + 4)) ||
((_x >= _bx) && (_x <= _bx + 4) && (_z <= _bz) && (_z >= _bz - 4))){
points++;
if(points < 100) size++;
if(points%5 == 0 && lvl < 15) lvl++;
newFood();
}
// Clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(EnableLight) initLight();
glutPostRedisplay();
break;
//ESC to Exit
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(_w,_h);
glutInitWindowPosition(80,80);
glutCreateWindow("Snake Game");
Initialize();
glutSpecialFunc(Special);
glutKeyboardFunc(keyboard);
glutDisplayFunc(Display);
glutReshapeFunc(resize);
newFood();
Run(0);
glutMainLoop();
}
CHAPTER-6
RESULT
SNAPSHOTS:
Fig. 6.1: Snapshot Showing the Instructions before Starting the Game.
Fig. 6.5: Snapshot of the Snake after eating several Food pieces.
Fig. 6.6: Game Ends after the Snake Collides with itself.
CONCLUSION
This project is an effort in the development of a Graphical Software package which is the
building block of graphical application. During the development of this package effort lead to
understanding the display of geometric primitives like rectangles, lines, line loops, polygon,
modes of display, features like translation were also designed. Various functions and operations
of the graphical library provide the learning platform to get the maximum performance of the
OpenGL functions.
The project “INTERACTIVE SNAKE GAME” has been successfully implemented using
OpenGL. The illustration of graphical principles and OpenGL features are included and
application program is efficiently developed.
This project enlightens the basic idea of scaling, rotation and translation of the objects.
Since it uses interaction with both keyboard and mouse it is sufficiently easy for any kind of end
user to run it.
However user requirement changes and provision for changes in design has been
allowed. If no major requirements are demanded from the user the current design holds. Thus
this project meets the basic requirements successfully and is flexible in all respects to one and
all. On conclusion this mini project is implemented with the standard OpenGL functions.
FUTURE ENHANCEMENTS
An attempt has been made to develop a Graphical Software package, which meets the
necessary requirement of the user successfully. The objects in the project can be flagged so that
we get a chance to be saved from a mine, we can also change the color and provide more effects.
Implementation of the other graphical effects will definitely enhance the representation
and view of the project. The mini project can further be enhanced to demonstrate the use of other
OpenGL functions of higher degree.
Since it is user friendly it enables the user to interact efficiently and easily. Thus the
package designed demonstrates the Basic Structures Orientation of some objects to the user.
REFERENCES
[1] Computer Graphics–Principals And Practice (Foley, VanDam, Fenier and Hughes) helped me
to understand graphics generation algorithms, user interface and dialogue design.
[3] www.cplusplus.com:-provided references regarding all C++ functions and their uses.
[4] www.stackoverflow.com:-help to get rid of all types of error occurred regarding uses of
OpenGL functions.