Snake 2
Snake 2
Snake 2
CHAPTER 1
INTRODUCTION
1.2 OpenGL
Originally developed by Silicon Graphics in the early '90s, OpenGL® has become the
most widely-used open graphics standard in the world. OpenGL is a software interface to
graphics hardware. This interface consists of about 150 distinct commands that you use to
specify the objects and operations needed to produce interactive three-dimensional
applications. OpenGL (Open Graphics Library) is basically a cross-language, multi-platform
API for rendering 2D and 3D computer graphics. The API is typically used to interact with a
GPU, to achieve hardware-accelerated rendering.
OpenGL serves two main purposes:
To hide the complexities of interfacing with different 3D accelerators, by presenting the
programmer with a single, uniform API.
To hide the differing capabilities of hardware platforms, by requiring that all
implementations support the full OpenGL feature set (using software emulation if
necessary).
Some features of OpenGL include the following:
Geometric and raster primitives
RGBA or color index mode
Display list or immediate mode
Viewing and modelling transformations
Lighting and Shading
Hidden surface removal (Depth Buffer)
Texture Mapping
OpenGL is a low-level, procedural API, requiring the programmer to dictate the exact
steps required to render a scene. These contrasts with descriptive APIs, where a programmer
only needs to describe a scene and can let the library manage the details of rendering it.
OpenGL's low-level design requires programmers to have a good knowledge of the graphics
pipeline, but also gives a certain amount of freedom to implement novel rendering algorithms.
OpenGL has historically been influential on the development of 3D accelerators, promoting a
base level of functionality that is now common in consumer-level hardware.
1.3 GLUT
GLUT is the OpenGL Utility Toolkit, a window system independent toolkit for writing
OpenGL programs. It implements a simple windowing application programming interface
(API) for OpenGL. The GLUT library has C, C++ (same as C), FORTRAN, and Ada
programming bindings. The GLUT source code distribution is portable to nearly all OpenGL
implementations and platforms. The current version is 3.7. Additional releases of the library
are not anticipated.
The GLUT library supports the following functionality:
Multiple windows for OpenGL rendering.
Callback driven event processing.
An `idle' routine and timers.
Utility routines to generate various solid and wire frame objects.
Support for bitmap and stroke fonts.
Miscellaneous window management functions.
CHAPTER 2
SYSTEM REQUIREMENTS
To be used efficiently, all computer software needs certain hardware components or other
software components to be present on a computer. These prerequisites are known as software
requirements. Though our graphics software does not demand strict specifications, certain
basic hardware and software requirements must be met.
CHAPTER 3
IMPLEMENTATION
Food Generation: A food, represented by a green circle is generated randomly on screen. This
algorithm is reinvoked once the snake has succeeded in eating the current food.
Food Detection: There are two global variables (storing values of (x,y) ) keeping track the
location of the current food. As the snake moves, the program detects whether the food is directly in
front of the snake by comparing the location of the head with location of the food. If it is, the food is
considered to be eaten. The current food will be erased and a new food will be generated.
Game-over Condition Detection: The game ends whenever the snake hits the border or
itself. The program detects game over condition by checking whether any pixel directly in front of the
snake is blue since the both the border and the snake are blue coloured.
Sets the keyboard callback for the current window. When a user types into the window, each
key press generating an ASCII character will generate a keyboard callback. The x and y
callback parameters indicate the mouse location in window relative coordinates when the key
was pressed.
glutCreateMenu()
Creates a new pop-up menu and returns a unique small integer identifier.
glLoadIdentity()
Sets the current transformation matrix to an identity matrix
glutIdleFunc()
Registers the display callback function that is executed whenever there are no other events to
be handled.
gluPerspective()
Sets up a perspective projection matrix
glutAttachMenu()
It attaches a mouse button for the current window to the identifier of the current menu. By
attaching a menu identifier to a button, the named menu will be popped up when the user
presses the specified button. Button should be one of GLUT_LEFT_BUTTON,
GLUT_MIDDLE_BUTTON, and GLUT_RIGHT_BUTTON. Note that the menu is attached
to the button by identifier, not by reference.
glMatrixMode()
Specifies which matrix will be affected by subsequent transformations. Mode can be
GL_MODELVIEW, GL_PROJECTION or GL_TEXTURE.
glMaterialfv()
Sets the parameter for face (GL_FRONT, GL_BACK or GL_FRONT_AND_BACK).
glLightfv()
Sets scalar and vector parameters for light source.
glEnable()
Enables an OpenGL feature. Features that can be enabled include GL_DEPTH_TEST, GL_,
GL_POLYGON_STIPPLE, GL_FOG and GL_NORMALIZE.
gluLookAt()
Post multiplies the current matrix by a matrix determined by a viewer at the eye point looking
at the point with the specified up direction.
glColor()
Sets the present RGB colours. The maximum and minimum values of the floating point types
are 1.0 and 0.0 respectively
glRasterPosition()
Specifies a raster position.
glutBitmapCharacter()
Renders the character with ASCII code char at the current raster position using the raster font
given by font. Fonts include GLUT_BITMAP_TIMES_ROMAN_10. The raster position is
incremented by the width of the character
glutSwapBuffers()
Swaps the front and back buffers.
glFlush()
Forces any buffered OpenGL commands to execute
glutPostRedisplay()
Requests that the display callback be executed after the current callback returns.
glTranslatef()
Alters the current matrix by a displacement specified along x y and z directions.
gluCylinder()
Draw a cylinder with height in z direction.
glutSolidSphere()
Define a sphere using slices lines of longitude and stacks lines of latitude.
glutSolidTorus()
Draw a solid torus (circular ring).
CHAPTER 4
Figure 4.1(initially)
Figure 4.3(gameplay)
CONCLUSION
We have attempted to design and implement “2D Snake Game”. OpenGl supports
enormous flexibility in the design and the use of OpenGl graphics programs. The presence of
many built in classes methods take care of much functionality and reduce the job of coding as
well as makes the implementation simpler.. We regret any errors that may have inadvertently
crept in.The project gives us more thrilling, frustrating and also gives us more pleasure. It helps
us in many sectors like- planning, designing, developing, managing, programming skill, socket
programming and so on.
FUTURE ENHANCEMENT
The “2D Snake Game” project will be able to implement in future after making some
changes and modifications as the project is made in a very low level. So the modifications that
can be done for the project are more options can be made with the use of good computer
graphics.
APPENDIX
void display_callback()
if (game_over)
ofile.open("score.dat", std::ios::trunc);
ofile.close();
ifile.open("score.dat", std::ios::in);
char a[4];
ifile >> a;
std::cout << a;
strcat_s(text, a);
exit(0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
draw_grid();
draw_food();
draw_snake();
glutSwapBuffers();
{
switch (key) { case
GLUT_KEY_UP:
if (direction != DOWN)
direction = UP;
break;
case GLUT_KEY_DOWN:
if (direction != UP)
direction = DOWN;
break;
case GLUT_KEY_RIGHT:
if (direction != LEFT)
direction = RIGHT;
break;
case GLUT_KEY_LEFT:
if (direction != RIGHT)
direction = LEFT;
break;
columns = x;
rows = y;
void draw_grid()
{
for (int i = 0; i<columns; i++)
{ for (int j = 0; j<rows; j++)
unit(i, j);
}
}
void draw_snake()
{
for (int i = length - 1; i>0; i--)
if (i == 0)
switch (direction)
{
case UP:
posy[i]++;
break;
case
DOWN:
posy[i]--;
break;
case RIGHT:
posx[i]++;
break;
case LEFT:
posx[i]--;
break;
game_over = true;
food = false;
score++;
length_inc = true;
if (length == MAX)
game_over = true;
glBegin(GL_QUADS);
glEnd();
if (length_inc)
{
length++;
length_inc = false;
void draw_food()
if (!food)
food = true;
glBegin(GL_QUADS);
glEnd();
}
BIBLIOGRAPHY
Books referred:
Other Webpages
freeglut.sourceforge.net/
Opengl.org/documentation/
Opengl.org/discussion_boards/
Opengl-tutorial.org
Nehe.gamedev.net/
en.wikipedia.org/wiki/OpenGL