0% found this document useful (0 votes)
44 views15 pages

2d Laxman

This document is a mini project report on developing a 2D game using OpenGL. It includes an introduction describing the game and technologies used. It discusses the system requirements including hardware, software, and OpenGL functions needed. It also provides an overview of the game interface, purpose, and scope. The implementation section explains functions used in OpenGL to develop the game.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views15 pages

2d Laxman

This document is a mini project report on developing a 2D game using OpenGL. It includes an introduction describing the game and technologies used. It discusses the system requirements including hardware, software, and OpenGL functions needed. It also provides an overview of the game interface, purpose, and scope. The implementation section explains functions used in OpenGL to develop the game.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1

A Mini Project Report


On
“ 2DGAME”
Submitted in Fulfillment of the Requirement for the Degree
of
Bachelor of Computer Applications
Invertis University, Bareilly

Mini Project Coordinator Submitted By


Faculty Coordinator Name :Mr.Gurpreet Singh Student Name :laxman Singh
Designation Roll No : 2110201110
Department of Computer Applications
2

ACKNOWLEGDEMENT

I extend my thanks to the Faculty of Computer Applications, Invertis University,


Bareilly who has given me the opportunity to work on this Project.

Today I am feeling a great sense of excitement on my way to successfully complete my mini


project on " 2DGAME" under the guidance of Dr./Mr./Ms. "Mr.Gurpreet .

I sincerely thank him/her for responding with great confidence and faith in my work and
being with me to encourage and guide me to successful project completion.

I would also like to thank Dr. Archana Saxena, HOD, Faculty of Computer Applications,
Invertis University for their support and all our friends and colleagues who have created an
atmosphere to encourage me from time to time making our work easy.

I would also like to thank Dr. Manish Gupta, Dean Academics for his valuable
suggestions.

Thank You

Signature of the Candidate


(Laxman Singh)
3

INDEX
1. Introduction of project Page No : 4
2. System Analysis Page No : 6
3. Feasibility Study Page No : 7
4. Code Page No : 11
5. Output Screens Page No: 14
6. Bibliography Page No :16
4

1. INTRODUCTION

In the project “ 2D GAME”, the project is programmed using c/c++ . Oops concepts are
explored and the project involves the Bat and Ball on which basis game is built.In this game there will be
two players . PC will be the First player and USER will be the second player. This program contains
Functions to drag the ball, draw the bat etc.
This project includes the multiple windows, menus and submenus using which color of the bat & ball,
screen color, ball size will be changed. These all actions are assigned to keyboard and mouse.
User-interface is provided by means of both Keyboard and Mouse. By using arrow keys bat can be
moved. Mouse interaction is achieved by means of a menu which is operational only with the “right mouse
button” through which bat,ball,screencolorchanging,speed settings are enabled.

1.1 Computer Graphics:


Graphics provides one of the most natural means of communicating within a computer, since our
highly developed 2D and 3D pattern-recognition abilities allow us to perceive and process pictorial data
rapidly and effectively. Interactive computer graphics is the most important means of producing pictures
since the invention of photography and television. It has the added advantage that, with the computer, we
can make pictures not only of concrete real world objects but also of abstract, synthetic objects, such as
mathematical surfaces and of data that have no inherent geometry, such as survey results.
Computer graphics started with the display of data on hardcopy plotters and cathode ray tube screens
soon after the introduction of computers themselves. It has grown to include the creation, storage, and
manipulation of models and images of objects. These models come from a diverse and expanding set of
fields, and include physical, mathematical, engineering, architectural, and even conceptual structures, natural
phenomena, and so on. Computer graphics today is largely interactive. The user controls the contents,
structure, and appearance of the objects and of their displayed images by using input devices, such as
keyboard, mouse, or touch-screen
1.2 OpenGL Interface:
OpenGL is an application program interface (API) offering various functions to implement
primitives, models and images. This offers functions to create and manipulate render lighting, coloring,
viewing the models. OpenGL offers different coordinate system and frames. OpenGL offers translation,
rotation and scaling of objects.
Most of our applications will be designed to access OpenGL directly through functions in three libraries.
They are:
1. Main GL: Library has names that begin with the letter gl and are stored in a library usually referred
to as GL.
5

2. OpenGL Utility Library (GLU): This library uses only GL functions but contains code for creating
common objects and simplifying viewing.
3. OpenGL Utility Toolkit (GLUT): This provides the minimum functionality that should be accepted
in any modern windowing system.
1.3 OpenGL Overview:
• OpenGL(Open Graphics Library) is the interface between a graphic program and graphics hardware.
It is streamlined. In other words, it provides low-level functionality. For example, all objects are
built from points, lines and convex polygons. Higher level objects like cubes are implemented as six
four-sided polygons.
• OpenGL supports features like 3-dimensions, lighting, anti-aliasing, shadows, textures, depth effects,
etc.
• It is system-independent. It does not assume anything about hardware or operating system and is
only concerned with efficiently rendering mathematically described scenes. As a result, it does not
provide any windowing capabilities.

1. SYSTEM SPECIFICATION

2.1 HARDWARE REQUIREMENTS:


• Dual Core Processor
• 2GB RAM
• 40GB Hard disk
• DVD drive
• Mouse and other pointing devices
• Keyboard

2.2 SOFTWARE REQUIREMENTS:


• Programming language – C/C++ using OpenGL
• Operating system – Linux operating system
• Compiler – C Compiler
• Graphics library – GL/glut.h OpenGL 2.0

2.3 FUNCTIONAL REQUIREMENTS:


OpenGL APIs:
If we want to have a control on the flow of program and if we want to interact with the window
system then we use OpenGL API’S. Vertices are represented in the same manner internally, whether they
are specified as two-dimensional or threedimensional entities, everything that we do are here will be equally
6

valid in three dimensions. Although OpenGL is easy to learn, compared with other APIs, it is nevertheless
powerful.
GL/glut.h:
We use a readily available library called the OpenGL Utility Toolkit (GLUT), which provides the
minimum functionality that should be expected in any modern windowing system.
The application program uses only GLUT functions and can be recompiled with the GLUT library
for other window system.

2. ABOUT THE PROJECT

3.1 Overview:
Our game is a simple ball with bat game. The bat will be moved according to the movement of the mouse.
And the ball will move randomly in the created window. When the ball hits the right, left, or top wall – we
will refer to the window border as a wall - it will return back. When it hits the bottom wall it will not only
return back but it will increase the score of the computer,

3.2 User Interface:


The interface is mainly concentrated on use of mouse and keyboard. Clicking right button of mouse displays
a menu which has various options which helps in changing color of bat, ball and background..

By pressing P and R we can pause and restart the game.

3.3Purpose:
The aim of this project is to develop a graphics package which supports basic operations which include
building a 2D GAME using Open GL. The package must also has a user-friendly interface. The objective of
developing this model was to design and apply the skills we learnt in class.

3.4 Scope:
It provides most of the features that a graphics model should have. It is developed in C language. It has
been implemented on LINUX platform. The graphics package designed here provides an interface for the
users for playing 2D GAME using bat and ball.

3. IMPLEMENTATION
4.1FUNCTIONS IN OPEN GL
• void glClear(glEnum mode);
7

Clears the buffers namely color buffer and depth buffer. mode refers to GL_COLOR_BUFFER_BIT
or DEPTH_BUFFER_BIT.
• void glTranslate[fd](TYPE x, TYPE y, TYPE z);
Alters the current matrix by displacement of (x, y, z), TYPE is either GLfloat or GLdouble.
• void glutSwapBuffers();
Swaps the front and back buffers.
• void glMatrixMode(GLenum mode);
Specifies which matrix will be affected by subsequent transformations, Mode can be
GL_MODELVIEW or GL_PROJECTION.
• void glLoadIdentity( );
Sets the current transformation matrix to identity matrix.
• void glEnable(GLenum feature);
Enables an OpenGL feature. Feature can be GL_DEPTH_TEST (enables depth test for hidden surface
removal), GL_LIGHTING (enables for lighting calculations), GL_LIGHTi (used to turn on the light for
number i), etc.
• void glPushMatrix(); void glPopMatrix();
Pushes to and pops from the matrix stack corresponding to the current matrix mode.

• void glutBitmapCharacter(void *font, int character);


Without using any display lists, glutBitmapCharacter renders the character in the named bitmap
font. The fonts used are GLUT_BITMAP_TIMES_ ROMAN_24, GLUT_BITMAP_ HELVETICA_18.
• void glRasterPos[234][ifd](GLfloat x, GLfloat y, GLfloat z);
This position, called the raster position, is used to position pixel and bitmap write operations.
• void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble
near,GLdouble far);
Defines an orthographic viewing volume with all the parameters measured from the centre of the projection
plane.
• void glutInit(int *argc, char **argv);
Initializes GLUT; the arguments from main are passed in and can be used by the application.
• void glutInitDisplayMode(unsigned int mode);
Requests a display with the properties in the mode; the value of mode is determined by the logical OR of
options including the color model (GLUT_RGB, GLUT_INDEX) and buffering (GLUT_SINGLE,
GLUT_DOUBLE).
• void glutCreateWindow(char *title);
Creates a window on display; the string title 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.
• void glutMainLoop();
8

Causes the program to enter an event-processing loop.

• void glutDisplayFunc(void (*func)(void))


Registers the display function func that is executed when the window needs to be redrawn.
• void glutMouseFunc(void *f(int button, int state, int x, int y)
Registers the mouse callback function f. The callback function returns the button
(GLUT_LEFT_BUTTON,etc., the state of the button after the event
(GLUT_DOWN), and the position of the mouse relative to the top-left corner of the window.

• void glutKeyboardFunc(void *f(char key, int width, int height))


Registers the keyboard callback function f. The callback function returns the ASCII code of the key pressed
and the position of the mouse.

• void glClearColor(GLclampf r, GLclampf g, GLclamp b, Glclamp a)


Sets the present RGBA clear color used when clearing the color buffer. Variables of type GLclampf are
floating point numbers between 0.0 and 1.0.

• void glViewport(int x ,int y, GLsizei width, GLsizei height)


Specifies the width*height viewport in pixels whose lower left corner is at (x,y) measured from the origin of
the window.

• void glColor3[b I f d ub us ui](TYPE r, TYPE g, TYPE b)


Sets the present RGB colors. Valid types are bytes(b), int(i), float(f), double(d), unsigned byte(ub), unsigned
short(us), and unsigned int(ui). The maximum and minimum value for floating point types are 1.0 and 0.0
respectively, whereas the maximum and minimum values of the discrete types are those of the type, for eg,
255 and 0 for unsigned bytes.

• void glutInitWindowSize(int width, int height);


Specifies the initial height and width of the window in pixels.

• void glutReshapeFunc(void *f(int width, int height));


Registers the reshape callback function f. the callback function returns the height and width of the new
window. The reshape callback invokes the display callback.
• void createMenu(void);
This function is used to create menus which are used as options in program.

4.2 USER DEFINED FUNCTIONS


9

• void reshape ( ) function:


It is used for defining the viewport volume using glViewport(int x ,int y,GLsizeiwidth,Glsizei height).

• int main( ) function:


The main function is used for creating the window for display of the model of the atom. Here, we create
menu for ease of use for the user. The callback functions, i.e., mouse callback, keyboard callback, display
callback, idle callback, are written in main. The callback functions registered in main ( ) are,
glutKeyboardFunc (mykeys);
glutDisplayFunc (display); glutReshapeFunc (reshape);glutMouseFunc (mouse);

• void incsize(RECTA rect)


This function is used to increase the size of the ball. It is done in game using arrow keys.
• void decsize(RECTA rect)
This function is used to decrease the size of the ball. It is done by using arrow keys.
• void DrawBall(RECTA rect,doubler,doubleg,double b)
It is used to draw ball on the screen of game. RECTA struct is used to draw ball in rectangle shape.
• voidDrawBat(RECTA rect,doubler,doubleg,double b)
This function is used for drawing bat for playing game.Here we make use of user defined struct RECTA.
• void Timer(int v)
Using this function in the program the ball movements towards left, right, top, bottom is achieved.
• void drawText(char* string,int x, int y)
This is used to draw text used in frontscreen, startscreen etc which contains the information about
projects team members, rules of game, instructions to games etc.
• int Test_Ball_Wall(RECTA ball , RECTA wall)
To find out the collision between wall and ball and to assign the scores to PC and player this
function is used.
• bool Test_Ball_Player(RECTA ball,RECTA player)
According to the movements of bat,ball and collision of ball with wall and bat the scores are calculated in
this function.
• void MouseMotion(int x,int y)
This function is used to move the bat on x-axis with the help of mouse.

4. TESTING
Testing process started with the testing of individual program units such as functions or objects.
These were then integrated into sub-systems and systems, and interactions of these units were tested.
Testing involves verification and validation.
Validation: “Are we building right product?”
10

Verification: “Are we building the product right?”


The ultimate goal of the verification and validation process is to establish confidence that the
software system is ‘fit for purpose’. The level of required confidence depends on the system’s purpose, the
expectations of the system users and the current marketing environment for the system.
With the verification and validation process, there are two complementary approaches to the system
checking and analysis:

CODE
#include <GL/glut.h>
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include<string.h>
#define bool int
#define true 1
#define false 0
#define FROM_RIGHT 1
#define FROM_LEFT 2
#define FROM_TOP 3 #define FROM_BOTTOM 4
int game=0; int p=0; int a,b,c,d; static mouse_x=0; static int value = 0; static int submenu_id; static int
bmenu_id; static int menu_id; static int nu_id; static int id; static int window;
double r1=1,g1=1,b1=1,r2=1,g2=1,b2=1.0; static int z=0;
static int WINDOW_WIDTH ,WINDOW_HEIGHT; int playerResult=0; int pcResult =0;
static float Xspeed=1,Yspeed=1; //for moving ball static float delta=1; //ball movements in steps
char string [100]; static int sizeb=0;

//structure for drawing ball and bat typedef struct RECTA


{
float left,top,right,bottom;
}RECTA;

RECTA ball={10,10,20,20};
RECTA wall ;
RECTA player_1 ={100,490,40,500};

//increase ball size void incsize(RECTA rect)


{
ball.right+=10; ball.bottom+=10;
}

//decrease ball size void decsize(RECTA rect) {


if(ball.left<ball.right)
{
ball.left+=10; ball.top+=10;
11

}
}

//drawing ball
void DrawBall(RECTA rect,doubler,doubleg,double b)
{
glColor3f(r,g,b);glBegin(GL_QUADS);
glVertex2f(rect.left,rect.bottom); glVertex2f(rect.right,rect.bottom);
glVertex2f(rect.right,rect.top); glVertex2f(rect.left,rect.top); glEnd();
}

//drawing bat
void DrawBat(RECTA rect,doubler,doubleg,double b)
{
glColor3f(r,g,b); glBegin(GL_QUADS);
glVertex2f(rect.left,rect.bottom); glVertex2f(rect.right,rect.bottom);
glVertex2f(rect.right,rect.top); glVertex2f(rect.left,rect.top); glEnd();
}

//timer funtion for moving ball void Timer(int v)


{
ball.left+=Xspeed; ball.right+=Xspeed; ball.top+=Yspeed; ball.bottom+=Yspeed;
glutTimerFunc(1,Timer,1);
}

//drawing text
void drawText(char* string,int x, int y)
{ int len, i; glRasterPos2f(x,y); len=(int) strlen(string); for(i = 0; i<len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,string[i]); }
}

//test collision between ball and wall intTest_Ball_Wall(RECTA ball , RECTA wall)
{
if(ball.right>=wall.right) return FROM_RIGHT; if(ball.left<=wall.left) return FROM_LEFT;
if(ball.top<=wall.top) return FROM_TOP; if(ball.bottom>=wall.bottom) return
FROM_BOTTOM; else return 0 ;
}

//calculating score
bool Test_Ball_Player(RECTA ball,RECTA player)
{
if(ball.bottom>= player.top&&ball.left>= player.left&&ball.right<=player.right )
{
playerResult++; return true;
}
return false;
}
12

//repositioning ball void rend(RECTA rect)


{ ball.left=10; ball.top=10; ball.right=20; ball.bottom=20;
}
void rendp(RECTA rect)
{ a=ball.left; b=ball.top; c=ball.right; d=ball.bottom;
} voidrendr(RECTA rect)
{ ball.left=a; ball.top=b; ball.right=c; ball.bottom=d;
}

//key Board Messages


void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'e':exit(0);break; case 'n'|'N':startscreenn();break; case '1':choice1();break; case
'2':renderr2();break; case '3':exit(0);break; case '4':winscreenn();break; case
'p':rendp(ball);pause11();break; case 'r':rendr(ball);renderr1();break; case
'c':rend(ball);delta=1;pcResult=0;playerResult=0;game=0;renderr1();bre ak; case
'a':rend(ball);delta=1;pcResult=0;playerResult=0;game=1;renderr1();bre ak; }
}

//key Board Message


void inputKey(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT :decsize(ball);break; case GLUT_KEY_RIGHT:incsize(ball);break;
case GLUT_KEY_UP :delta++;break ;
case GLUT_KEY_DOWN :if(delta>1) delta--;break;
}
}

//moving bat on x-axis voidMouseMotion(int x,int y)


{
mouse_x=x;
}

//openGL Setting
void Setting(double r,doubleg,doubleb,double alpha)
{
glClearColor (r, g, b, alpha);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

//windowResize void reshape (int w, int h) {


WINDOW_WIDTH =w ; WINDOW_HEIGHT =h ;
glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity ();
gluOrtho2D (0, w, h, 0); glMatrixMode (GL_MODELVIEW); glLoadIdentity ();
}
13

//putting all things together void Renderc(void)


{
glClear(GL_COLOR_BUFFER_BIT ); glMatrixMode(GL_MODELVIEW); glLoadIdentity();
glColor3f(1,1,1); sprintf(string,"PC : %d ",pcResult);

5. SNAPSHOTS
14

6. CONCLUSION AND ENHACEMENTS


The project was started with modest aim with no prior experience in any programming projects as
this, but ended up in learning many things, fine tuning the programming skills and getting into the real world
of software development with an exposure to corporate environment. During the development of any
software of significant utility, we are forced with the tradeoff between speed of execution and amount of
memory consumed. This is simple interactive application. It is extremely user friendly and has the features,
15

which makes simple graphics project. It has an open source and no security features has been included. The
user is free to alter the code for feature enhancement. Checking and verification of all possible types of the
functions are taken care. Care was taken to avoid bugs. Bugs may be reported to creator as the need.
Further this project can be enhanced by adding few more options i,e menus in game. Using this we
can design a 3D game which contains cube instead of single window and multiple number of balls which are
randomly moving and all faces of cube is considered as wall.

7. BIBLIOGRAPHY

Reference Books and E-books

1. EDWARD ANGEL
Interactive Computer Graphics, 5th edition, universities of New Mexico.

2. Websites for Reference


• www.opengl.org/resources/code/samples/redbook.
• www.wikipedia.org

You might also like