CG Arnav, Jashandeep, Sarthak
CG Arnav, Jashandeep, Sarthak
CG Arnav, Jashandeep, Sarthak
Submitted By
Submitted To:
Group No: 11
Patiala – 147001
Table of Contents
1. Introduction to Project 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.
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:
2. CONCEPTS USED
2D-Transformations:
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
Filling a Polygon is the process of colouring every pixel that comes inside the Polygon Region.
Line Drawing:
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
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.
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:
w, s, d, a: For movement
- 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 REFRESH 20
#define COMPRANGE 1
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(stringX, stringY, 0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, print[i]);
yfire[i] += FIRESPEED;
ycomfire[i] -= FIRESPEED;
xbat = -0.96;
speed = 0;
xbat = 0.69;
speed = 0;
ybat = -0.2;
speed2 = 0;
ybat = -1.0;
speed2 = 0;
else
speed = 0;
else
speed2 = 0;
glutPostRedisplay();
if (gameOver == 0)
REFRESH2 += REFRESH;
xcomfire[numofcomfire % AMMO2] = x;
ycomfire[numofcomfire % AMMO2] = y;
comfirevisible[numofcomfire % AMMO2] = 1;
numofcomfire++;
REFRESH2 = 0;
switch (Key)
case 13:
gameOver = 0;
glutPostRedisplay();
break;
case 27:
exit(0);
case 'd':
x1 = -0.95;
xtwoplayer = 0.60;
break;
case 'a':
x1 = -0.72;
xtwoplayer = 0.38;
break;
case 'w':
x1 = -0.58;
xtwoplayer = 0.22;
break;
case 's':
x1 = -0.42;
xtwoplayer = 0.14;
break;
case 32:
fireVisible[numoffire % AMMO] = 1;
x1 = -0.19;
numoffire++;
xtwoplayer = 0.1;
break;
}
void Display()
if (gameOver == 0)
if (xd == 'r')
x = x + xdist;
x = x - xdist;
if (yd == 'd')
y = y - ydist;
y = y + ydist;
if (x < x1)
xd = 'r';
xd = 'l';
if (y > 0.2)
yd = 'd';
else if (y < -0.5)
yd = 'u';
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
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);
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();
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();
glBegin(GL_POLYGON);
glVertex2d(-0.08, powercom);
glVertex2d(-0.08, -0.4);
glVertex2d(0.08, -0.4);
glVertex2d(0.08, powercom);
glEnd();
glLoadIdentity();
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();
glBegin(GL_POLYGON);
glVertex2d(-0.08, powercom2);
glVertex2d(-0.08, -0.4);
glVertex2d(0.08, -0.4);
glVertex2d(0.08, powercom2);
glEnd();
glLoadIdentity();
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();
if (fireVisible[i] == 1)
glLoadIdentity();
(double)255 / 255);
if (comfirevisible[i] == 1)
{
glLoadIdentity();
(double)255 / 255);
fireVisible[i] = 0;
gameOver = 2;
}
for (i = 0; i < AMMO2; i++)
comfirevisible[i] = 0;
gameOver = 3;
glClear(GL_COLOR_BUFFER_BIT);
else if (gameOver == 3)
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT);
60);
glutSwapBuffers();
fireVisible[j] = 0;
comfirevisible[j] = 0;
gameOver = 10;
glutInit(&argc, argv);
glutInitWindowSize(1440, 920);
glutInitWindowPosition(200, 300);
glutInitDisplayMode(GLUT_DOUBLE);
glutFullScreen();
glutDisplayFunc(Display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
5. OUTPUT / SCREENSHOTS