Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3K views21 pages

Snake 2

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 21

Snake 2D-Game 2017-2018

CHAPTER 1

INTRODUCTION

1.1 COMPUTER GRAPHICS


The term computer graphics includes almost everything on computers that is not text or
sound. Today almost every computer can do some graphics, and people have even come to
expect to control their computer through icons and pictures rather than just by typing.
Computer graphics are created using computers and the representation of image data by a
computer specifically with help from specialized graphic hardware and software. The
interaction and understanding of computers and interpretation of data has been made easier
because of computer graphics. A computer graphic development has had a significant impact
on many types of media and has revolutionized animation, movies and the video game
industry.
Typically, the term computer graphics refers to several different things:
 The representation and manipulation of image data by a computer
 The various technologies used to create and manipulate images
 The sub-field of computer science which studies methods for digitally synthesizing and
manipulating visual content.
Computer generated imagery can be categorized into several different types: two
dimensional (2D), three dimensional (3D), and animated graphics. As technology has
improved, 3D computer graphics have become more common, but 2D computer graphics are
still widely used. Computer graphics has emerged as a sub-field of computer science which
studies methods for digitally synthesizing and manipulating visual content. Over the past
decade, other specialized fields have been developed like information visualization, and
scientific visualization more concerned with "the visualization of three dimensional
phenomena (architectural, meteorological, medical, biological, etc.), where the emphasis is on
realistic renderings of volumes, surfaces, illumination sources, and so forth, perhaps with a
dynamic (time) component".

Dept. of CSE, Vemana IT Page 1


Snake 2D-Game 2017-2018

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.

Dept. of CSE, Vemana IT Page 2


Snake 2D-Game 2017-2018

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.

Dept. of CSE, Vemana IT Page 3


Snake 2D-Game 2017-2018

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.

2.1 HARDWARE REQUIREMENTS


 CPU: Intel/AMD CPU
 RAM (Main memory): 512 MB
 Hard disk: 10MB of free space
 Hard disk speed (in RPM): 5400 RPM
 Mouse: 2 button mouse
 Keyboard: Standard keyboard with arrow keys
 Monitor: 1366*768 display resolution

2.2 SOFTWARE REQUIREMENTS


 Operating System: Windows based operating system
 Code::Blocks with OpenGL library
 Mouse driver
 Graphic driver

Dept. of CSE, Vemana IT Page 4


Snake 2D-Game 2017-2018

CHAPTER 3

IMPLEMENTATION

3.1 Introduction to Visual Studio


Microsoft visual studio is an integrated development environment from Microsoft. It is
used to develop computer programs, as well as web sites, web apps, web services and mobile
apps. Visual studio uses Microsoft software development platforms such as window API,
windows form, windows presentation foundation, windows store and Microsoft Silverlight. It
can produce both native code and managed code. Visual studio includes a code editor
supporting intelligence as well as code refactoring. The integrated debugger works both as a
source-level debugger and a machine-level debugger. Other built-in tools include a code
profiler, forms designer for building GUI applications, web designer, class designer, and
database schema designer. It accepts plug-instant enhance the functionality at almost every
level-including adding support for source control systems and adding new toolsets like editors
and visual designers for domain specific languages or toolsets for other aspects of the software
development lifecycle .Visual studio supports 36 different programming languages and allows
the code editor and debugger to support nearly any programming language provide a language-
specific service exists.

3.2 Project description


The following is an example game written in C based on the game called 'snake' which has been
around since the earliest days of home computing and has re-emerged in recent years on mobile
phones.It isn't the world's greatest game, but it does give you an idea of what you can achieve with a
relatively simple C program, and perhaps the basis by which to extend the principles and create more
interesting games of your own.

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.

Dept. of CSE, Vemana IT Page 5


Snake 2D-Game 2017-2018

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.

Dept. of CSE, Vemana IT Page 6


Snake 2D-Game 2017-2018

3.3 OpenGL Functions


 glutInit()
Initializes GLUT. The arguments from main are passed in and can be used by the application.
 glutInitDisplayMode()
Requests a display with the properties in mode. The value of mode is determined by the logical
OR of options including the colour model (GLUT_RGB, GLUT_INDEX) and buffering
(GLUT_SINGLE, GLUT_DOUBLE).
 glutInitWindowSize()
Specifies the initial height and width of the window in pixels.
 glutCreateWindow()
Creates a window on the display. The string can be used to label the window. The return value
provides a reference to the window that can be used when there are multiple windows.
 glutDisplayFunc()
Registers the display function that is executed when the window needs to be redrawn.
 glutIdleFunc()
Registers the display callback function that is executed whenever there are no other events to
be handled.
 glutMouseFunc()
Registers the mouse callback function. The callback function returns the button
(GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON, GLUT_MIDDLE_BUTTON), the
state of the button after the event (GLUT_UP, GLUT_DOWN), and the position of the mouse
relative to the top-left corner of the window.
 glutMainLoop()
Cause the program to enter an event-processing loop. It should be the last statement in main.
 glClearColor()
Sets the present RGBA clear colour used when clearing the colour buffer.
 glutReshapeFunc()
Sets the reshape callback for the current window. The reshape callback is triggered when a
window is reshaped. A reshape callback is also triggered immediately before a window's first
display callback after a window is created or whenever an overlay for the window is
established.
 glutKeyboardFunc()

Dept. of CSE, Vemana IT Page 7


Snake 2D-Game 2017-2018

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.

Dept. of CSE, Vemana IT Page 8


Snake 2D-Game 2017-2018

 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).

Dept. of CSE, Vemana IT Page 9


Snake 2D-Game 2017-2018

CHAPTER 4

SAMPLE OUTPUT SNAPSHOTS)

Figure 4.1(initially)

Dept. of CSE, Vemana IT Page 10


Snake 2D-Game 2017-2018

Figure 4.2(collision condition)

Dept. of CSE, Vemana IT Page 11


Snake 2D-Game 2017-2018

Figure 4.3(gameplay)

Dept. of CSE, Vemana IT Page 12


Snake 2D-Game 2017-2018

Figure 4.4 (gameplay)

Dept. of CSE, Vemana IT Page 13


Snake 2D-Game 2017-2018

Figure 4.5(gameover condition)

Dept. of CSE, Vemana IT Page 14


Snake 2D-Game 2017-2018

CONCLUSION AND FUTURE ENHANCEMENTS

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.

Dept. of CSE, Vemana IT Page 15


Snake 2D-Game 2017-2018

APPENDIX

void display_callback()

if (game_over)

ofile.open("score.dat", std::ios::trunc);

ofile << score << std::endl;

ofile.close();

ifile.open("score.dat", std::ios::in);

char a[4];

ifile >> a;

std::cout << a;

char text[50] = "Your score : ";

strcat_s(text, a);

MessageBox(NULL, text, "Game Over", 0);

exit(0);

glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();

draw_grid();

draw_food();

draw_snake();

glutSwapBuffers();

Dept. of CSE, Vemana IT Page 16


Snake 2D-Game 2017-2018

void input_callback(int key, int x, int y)

{
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;

void initGrid(int x, int y)

columns = x;

rows = y;

Dept. of CSE, Vemana IT Page 17


Snake 2D-Game 2017-2018

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--)

posx[i] = posx[i - 1];

posy[i] = posy[i - 1];

for (int i = 0; i<length; i++)

glColor3f(0.8, 0.6, 0.4);

if (i == 0)

glColor3f(0.0, 0.0, 0.3);

switch (direction)

{
case UP:

posy[i]++;

break;
case
DOWN:

Dept. of CSE, Vemana IT Page 18


Snake 2D-Game 2017-2018

posy[i]--;

break;

case RIGHT:
posx[i]++;

break;

case LEFT:

posx[i]--;

break;

if (posx[i] == 0 || posx[i] == columns - 1 || posy[i] == 0 || posy[i] == rows - 1)

game_over = true;

else if (posx[i] == foodx && posy[i] == foody)

food = false;

score++;

if (length <= MAX)

length_inc = true;

if (length == MAX)

MessageBox(NULL, "You Win", "Awesome", 0);

for (int j = 1; j<length; j++)

if (posx[j] == posx[0] && posy[j] == posy[0])

game_over = true;

glBegin(GL_QUADS);

Dept. of CSE, Vemana IT Page 19


Snake 2D-Game 2017-2018

glVertex2d(posx[i], posy[i]); glVertex2d(posx[i] + 1, posy[i]); glVertex2d(posx[i] + 1, posy[i] +


1); glVertex2d(posx[i], posy[i] + 1);

glEnd();

if (length_inc)

{
length++;

length_inc = false;

void draw_food()

if (!food)

foodx = random(2, columns - 2);

foody = random(2, rows - 2);

std::cout << foodx << foody << std::endl;

food = true;

glBegin(GL_QUADS);

glVertex2d(foodx, foody); glVertex2d(foodx + 1, foody); glVertex2d(foodx + 1, foody + 1);


glVertex2d(foodx, foody + 1);

glEnd();
}

Dept. of CSE, Vemana IT Page 20


Snake 2D-Game 2017-2018

BIBLIOGRAPHY

Books referred:

 Edward Angel: Interactive Computer Graphics A Top-Down Approach with OpenGL,


5th Edition, Pearson Education, 2008.

Other Webpages

 freeglut.sourceforge.net/
 Opengl.org/documentation/
 Opengl.org/discussion_boards/
 Opengl-tutorial.org
 Nehe.gamedev.net/
 en.wikipedia.org/wiki/OpenGL

Dept. of CSE, Vemana IT Page 21

You might also like