2d Laxman
2d Laxman
ACKNOWLEGDEMENT
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
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.
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
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.
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.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.
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
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;
RECTA ball={10,10,20,20};
RECTA wall ;
RECTA player_1 ={100,490,40,500};
}
}
//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();
}
//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
//openGL Setting
void Setting(double r,doubleg,doubleb,double alpha)
{
glClearColor (r, g, b, alpha);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
5. SNAPSHOTS
14
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
1. EDWARD ANGEL
Interactive Computer Graphics, 5th edition, universities of New Mexico.