Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CG Arnav, Jashandeep, Sarthak

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

Computer Graphics (UCS505)

Micro Game: Space shooter

Submitted By

Arnav Agarwal 101916133

Jashandeep Singh 101916123

Sarthak Gupta 101916127

B.E. Third Year – CSE 12

Submitted To:

Jyoti Rani Ma’am

Group No: 11

Computer Science and Engineering Department

Thapar Institute of Engineering and Technology

Patiala – 147001
Table of Contents

Sr. No. Description Page No.

1. Introduction to Project 3

2. Computer Graphics concepts used 3

3. User-Defined Functions 4

4. Code 5

5. Output/ Screenshots 22
1. INTRODUCTION
Space shooter is a game that is set in a space setting. In the game, a player plays against the
computer and tries to take down the opponent’s spaceship. It is built using the OpenGL library
with C++.

The game is played with the simple objective of shooting the opponent’s spaceship. Every time
a spaceship gets shot its life decreases by 10%. The total life of each spaceship is 100 points in
the beginning.

On starting the game, a home screen appears which includes options to

1. Start the game

2. Read instructions

3. Quit

The instructions section consists of information about the keyboard controls of the game. The
controls for the game are as follows:

For User Instructions:

w : Front , s : Back , a : Left , d : Right , space : Fire ,esc : Exit

2. CONCEPTS USED
2D-Transformations:

Transformation is a process of modifying and re-positioning the existing graphics. 2D


Transformations take place in a two-dimensional plane. Transformations are helpful in
changing the position, size, orientation, shape, etc. of the object.

A translation moves an object to a different position on the screen. You can translate a

point in 2D by adding the translation coordinate (tx, ty) to the original coordinate X, Y to get

the new coordinate X′, Y′.


Polygon Filling:

Filling a Polygon is the process of colouring every pixel that comes inside the Polygon Region.

Line Drawing:

GL_LINES – treats each pair of vertices as an independent line segment.

Polygon Drawing:

GL_POLYGON – It draws polygons out of provided points. If the number of points provided are
n then it will draw a n sided polygon

3. User Defined Functions


- void Display()
Used to render the gameplay and check for various conditions which include translation
of ships, visibility of bullets/ammo and damage handling in case of getting hit.

In the first case where gameover == 0 the logic for running game is written. Here, the
first set of conditional statements the function handles the movement of user’s ship.
There are 4 possibilities: r, l, u, d and the x and y coordinates change accordingly.

Then comes the logic of rendering ammo. In this part of the code the ammo is rendered
with a predefined speed and direction.

In the end we handle the health of both computer and the player and calculate the
remaining health by deducting the damage caused by a hit. Once any one of the 2
health bars reach 0, the game ends.

In the second case, gameover == 2, we display the text for user’s victory.

In the third case, gameover == 3, we display the text for the computer’s victory.

In the last case, gameover == 10, we return to the home screen.


- void keyboard(unsigned char key, int x, int y)

Used to handle keyboard inputs and return the appropriate response. This function take
in 3 parameters: key, x, y. Key is the keyboard input and x & y are the coordinates of
user’s ship.

We use a switch statement to handle different keyboard inputs. The keys used and their
function are as follows:

Enter: Starts a new game

Esc: Exits the game

w, s, d, a: For movement

Spacebar: Shoots bullets.

- void Timer()

The timer function is called using glutTimerFunc() in the main function. It is a callback
function and is repeatedly called after a specified time (0 milliseconds in this case) and it
takes in a parameter value. This function is used to control the movements and firing of
computer’s automated ship and also initialize game settings.

4. CODE
#include "./freeglut-3.2.1/include/GL/freeglut.h"

#include <stdio.h>

#include <bits/stdc++.h>

#include <GL/glut.h>

#include <math.h>

#include <GL/glut.h>

#include <GL/glu.h>
#include <GL/gl.h>

#include <stdio.h>

#include <string.h>

#define AMMO 100

#define AMMO2 100

#define MOVEACCELERATION 0.02

#define FIRESPEED 0.06

#define REFRESH 20

#define COMPRANGE 1

#define DAMAGE 0.01

double speed = 0, speed2 = 0, xbat = -0.1, ybat = -0.8, xfire[AMMO],


powercom = 0.4, powercom2 = 0.4, yfire[AMMO], xdist = 0.008, ydist =
0.007, x, y,

x1 = -0.98, xtwoplayer = 0.71, xcomfire[AMMO2], ycomfire[AMMO2],


firespeed = 100;

int i, j, numoffire = 0, fireVisible[AMMO], numofcomfire = 0,

comfirevisible[AMMO2], REFRESH2 = 0, gameOver = 10;

char xd = 'r', yd = 'u';

void printString(char print[], int Stringlenght, double stringX, double


stringY, int size)

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();
glTranslated(stringX, stringY, 0);

glScaled(0.00001 * size, 0.00001 * size, 0.00001 * size);

for (i = 0; i < Stringlenght; i++)

glutStrokeCharacter(GLUT_STROKE_ROMAN, print[i]);

void Timer(int Value)

for (i = 0; i < AMMO; i++)

yfire[i] += FIRESPEED;

for (i = 0; i < AMMO2; i++)

ycomfire[i] -= FIRESPEED;

if (xbat < -0.96)

xbat = -0.96;

speed = 0;

if (xbat > 0.69)


{

xbat = 0.69;

speed = 0;

if (ybat > -0.2)

ybat = -0.2;

speed2 = 0;

if (ybat < -1.0)

ybat = -1.0;

speed2 = 0;

xbat = xbat + speed;

ybat = ybat + speed2;

if (speed >= 0.0004)

speed = speed - 0.002;

else if (speed <= (-0.0004))

speed = speed + 0.002;


}

else

speed = 0;

if (speed2 >= 0.0004)

speed2 = speed2 - 0.002;

else if (speed2 <= (-0.0004))

speed2 = speed2 + 0.002;

else

speed2 = 0;

glutPostRedisplay();

if (gameOver == 0)

glutTimerFunc(REFRESH, Timer, 0);

REFRESH2 += REFRESH;

if (xbat + COMPRANGE >= x && xbat - COMPRANGE <= x)

if (REFRESH2 >= firespeed)

xcomfire[numofcomfire % AMMO2] = x;

ycomfire[numofcomfire % AMMO2] = y;
comfirevisible[numofcomfire % AMMO2] = 1;

numofcomfire++;

REFRESH2 = 0;

void keyboard(unsigned char Key, int x, int y)

switch (Key)

case 13:

gameOver = 0;

glutTimerFunc(REFRESH, Timer, 0);

glutPostRedisplay();

break;

case 27:

exit(0);

case 'd':

speed = speed + MOVEACCELERATION;

x1 = -0.95;

xtwoplayer = 0.60;

break;
case 'a':

speed = speed - MOVEACCELERATION;

x1 = -0.72;

xtwoplayer = 0.38;

break;

case 'w':

speed2 = speed2 + MOVEACCELERATION;

x1 = -0.58;

xtwoplayer = 0.22;

break;

case 's':

speed2 = speed2 - MOVEACCELERATION;

x1 = -0.42;

xtwoplayer = 0.14;

break;

case 32:

xfire[numoffire % AMMO] = xbat;

yfire[numoffire % AMMO] = ybat;

fireVisible[numoffire % AMMO] = 1;

x1 = -0.19;

numoffire++;

xtwoplayer = 0.1;

break;
}

void Display()

if (gameOver == 0)

if (xd == 'r')

x = x + xdist;

else if (xd == 'l')

x = x - xdist;

if (yd == 'd')

y = y - ydist;

else if (yd == 'u')

y = y + ydist;

if (x < x1)

xd = 'r';

else if (x > xtwoplayer)

xd = 'l';

if (y > 0.2)

yd = 'd';
else if (y < -0.5)

yd = 'u';

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glClear(GL_COLOR_BUFFER_BIT);

glTranslated(xbat, ybat, 0);

glColor3d(0.0f, 1.0f, 0.0f);

glBegin(GL_POLYGON);

glVertex2d(-0.04, 0.025);

glVertex2d(-0.04, -0.025);

glVertex2d(0.09, -0.025);

glVertex2d(0.09, 0.025);

glEnd();

glBegin(GL_TRIANGLES);

glVertex2d(0.02, 0.20);

glVertex2d(-0.01, 0.0);

glVertex2d(0.06, 0.0);

glEnd();

glLoadIdentity();

glTranslated(x, y, 0);

glBegin(GL_POLYGON);

glColor3d(1.0f, 0.0f, 0.0f);

glVertex2d(-0.04, 0.8);
glVertex2d(-0.04, 0.75);

glVertex2d(0.09, 0.75);

glVertex2d(0.09, 0.8);

glEnd();

glBegin(GL_TRIANGLES);

glVertex2d(0.02, 0.55);

glVertex2d(-0.01, 0.8);

glVertex2d(0.06, 0.8);

glEnd();

glLoadIdentity();

glColor3d(1.0f, 1.0f, 1.0f);

glBegin(GL_LINES);

glVertex2d(0.8, -1.0);

glVertex2d(0.8, 1.0);

glEnd();

glBegin(GL_LINES);

glVertex2d(-1.0, 0.0);

glVertex2d(1.0, 0.0);

glEnd();

glLoadIdentity();

glTranslated(0.9, 0.5, 0);

glColor3d(1.0f, 0.0f, 0.0f);

glBegin(GL_POLYGON);
glVertex2d(-0.08, powercom);

glVertex2d(-0.08, -0.4);

glVertex2d(0.08, -0.4);

glVertex2d(0.08, powercom);

glEnd();

glLoadIdentity();

glTranslated(0.9, 0.5, 0);

glColor3d(1.0f, 1.0f, 1.0f);

glBegin(GL_LINE_LOOP);

glVertex2d(-0.08, 0.4);

glVertex2d(-0.08, -0.4);

glVertex2d(0.08, -0.4);

glVertex2d(0.08, 0.4);

glEnd();

glLoadIdentity();

glTranslated(0.9, -0.5, 0);

glColor3d(0.0f, 1.0f, 0.0f);

glBegin(GL_POLYGON);

glVertex2d(-0.08, powercom2);

glVertex2d(-0.08, -0.4);

glVertex2d(0.08, -0.4);

glVertex2d(0.08, powercom2);

glEnd();
glLoadIdentity();

glTranslated(0.9, -0.5, 0);

glColor3d(1.0f, 1.0f, 1.0f);

glBegin(GL_LINE_LOOP);

glVertex2d(-0.08, 0.4);

glVertex2d(-0.08, -0.4);

glVertex2d(0.08, -0.4);

glVertex2d(0.08, 0.4);

glEnd();

for (i = 0; i < AMMO; i++)

if (fireVisible[i] == 1)

glLoadIdentity();

glColor3d((double)91 / 255, (double)226 / 255,

(double)255 / 255);

glTranslated(xfire[i] + 0.02, yfire[i] + 0.09, 0);

glutSolidSphere(0.01, 10, 10);

for (i = 0; i < AMMO2; i++)

if (comfirevisible[i] == 1)
{

glLoadIdentity();

glColor3d((double)91 / 255, (double)226 / 255,

(double)255 / 255);

glTranslated(xcomfire[i] + 0.025, ycomfire[i] + 0.55, 0);

glutSolidSphere(0.01, 10, 10);

for (i = 0; i < AMMO; i++)

if (fireVisible[i] == 1 && (x < xfire[i] + 0.09 && x >


xfire[i] - 0.09) && y + 0.7 > yfire[i] && y + 0.5 < yfire[i])

if (powercom > -0.4)

powercom = powercom - DAMAGE;

fireVisible[i] = 0;

else if (powercom <= 0.4)

gameOver = 2;

}
for (i = 0; i < AMMO2; i++)

if (comfirevisible[i] == 1 && xbat < xcomfire[i] + 0.09 &&


xbat > xcomfire[i] - 0.09 && ybat - 0.3 > ycomfire[i] && ybat - 0.5 <
ycomfire[i])

if (powercom2 > -0.4)

powercom2 = powercom2 - DAMAGE - 0.05;

comfirevisible[i] = 0;

else if (powercom2 <= -0.4)

gameOver = 3;

printf("Computer %d", gameOver);

glColor3d(1.0f, 1.0f, 1.0f);

printString(" POWER", 6, 0.79, 0.04, 32);

printString(" POWER", 6, 0.79, -0.96, 32);

printString("COMPUTER", 8, 0.812, 0.94, 26);

printString(" YOU", 4, 0.816, -0.06, 32);


if (gameOver == 2)

glClear(GL_COLOR_BUFFER_BIT);

glColor3d(0.0f, 1.0f, 0.0f);

printString("YOU WON", 7, -0.25, 0, 80);

else if (gameOver == 3)

glClear(GL_COLOR_BUFFER_BIT);

glColor3d(0.0f, 1.0f, 0.0f);

printf("Computer %d string", gameOver);

printString("COMPUTER WON", 12, -0.45, 0, 80);

else if (gameOver == 10)

glColor3d((double)0 / 255, (double)217 / 255, (double)11 / 255);

glClear(GL_COLOR_BUFFER_BIT);

printString("SPACE SHOOTER", 14, -0.7, 0.8, 150);

printString("Developers : Arnav Agarwal,Jashandeep Singh,Sarthak


Gupta",

strlen("Developers : Arnav Agarwal,Jashandeep


Singh,Sarthak Gupta"), -1.0, 0.6, 60);

printString("Submitted to : Jyoti Rani Ma’am",


strlen("Submitted to : Jyoti Rani Ma’am"), -1.0, 0.4,
60);

printString("Controls : ", strlen("Controls : "), -1.0, 0.2, 60);

printString("1) w : Front", strlen("1) w : Front"), -0.63, 0.1,


60);

printString("2) s : Back", strlen("2) s : Back"), -0.63, 0.0, 60);

printString("3) a : Left", strlen("3) a : Left"), -0.63, -0.1,


60);

printString("4) d : Right", strlen("4) d : Right"), -0.63, -0.2,


60);

printString("5) Space : Fire", strlen("5) Space : Fire"), -0.63, -


0.3,

60);

printString("6) Esc : Exit", strlen("6) Esc : Exit"), -0.63, -0.4,


60);

printString("Game : Kill the Computer Ship.",

strlen("Game : Kill the Computer Ship."), -1.0, -0.6,


60);

printString("Press Enter to play game.",

strlen("Press Enter to play game."), -1.0, -0.8, 60);

glutSwapBuffers();

int main(int argc, char **argv)

for (j = 0; j < AMMO; j++)


{

fireVisible[j] = 0;

for (j = 0; j < AMMO2; j++)

comfirevisible[j] = 0;

gameOver = 10;

glutInit(&argc, argv);

glutInitWindowSize(1440, 920);

glutInitWindowPosition(200, 300);

glutInitDisplayMode(GLUT_DOUBLE);

glutCreateWindow("Space Ship Game");

glutFullScreen();

glutDisplayFunc(Display);

glutTimerFunc(0, Timer, 0);

glutKeyboardFunc(keyboard);

glClearColor(0.0, 0.0, 0.0, 1.0);

glutMainLoop();

return 0;

}
5. OUTPUT / SCREENSHOTS

You might also like