You are on page 1of 9

Asteroids Game

A 2D game using C++ and OpenGl glut lib

Urooba Shameem (B20102180)


Nabiha Faisal (B20102130)
About Game
The Asteroids game is a classic 2D arcade-style video game
where players take control of a spaceship navigating through
space.
The objective is to survive and earn points by shooting down the
asteroids.

User can play by using three keys:


● ‘W’ for thrust
● ‘A’ to rotate left
● ‘D’ to rotate right
● ‘S’ to shoot
init() Function:

● Initializes the game environment.


● Sets the background color.
● Configures the projection matrix using
gluOrtho2D for 2D rendering.
● Initializes the player's spaceship with its
position, angle, and thrust.
● Creates the initial set of asteroids with
random positions, angles, and sizes.
drawShip() Function:
drawAsteroids() Function:
● Draws the player's spaceship.
● Renders the asteroids.
● Uses OpenGL transformations
● Loops through the asteroid
(glPushMatrix, glTranslatef,
vector and uses OpenGL
glRotatef) to position and
transformations to position and
rotate the ship. rotate each asteroid.
● Draws the ship's main body as a ● Draws each asteroid as a
polygon. polygon with a specified number
● If the thrust is active, an of sides (12 in this case) to
additional flame is drawn at the create a circular appearance.
back of the ship.
drawBullets() Function: drawScore() Function:

● Renders the bullets. ● Displays the player's score on the


● Loops through the bullet vector screen.
and uses OpenGL ● Uses OpenGL's glRasterPos2f
to set the position for rendering
transformations to position and
text.
rotate each bullet.
● Converts the player's score to a
● Draws each bullet as a line,
string and displays it character
creating a simple visual
by character using GLUT's
representation of bullets. glutBitmapCharacter .
drawScene() Function: update() Function:

● Draws the entire game scene. ● Manages game updates.


● Clears the screen using glClear. ● Updates bullet positions based on
● Calls drawShip() to draw the player's their direction and speed.
ship. ● Checks for collisions between bullets
and asteroids.
● Calls drawAsteroids() to draw the
● If a bullet hits an asteroid, it sets the
asteroids.
bullet's active flag to false and
● Calls drawBullets() to draw the
removes the asteroid.
bullets. ● Increases the player's score when a
● Calls drawScore() to display the bullet successfully hits an asteroid.
score. ● Updates the ship's position if the
● Swaps the frame buffers using thrust is active.
glutSwapBuffers for smooth ● Uses a timer (glutTimerFunc ) to
rendering. repeatedly call itself for continuous
updates.
keyboard() Function:

● Handles keyboard input for controlling the game.


● 'w' activates the thrust, causing the ship to move forward.
● 's' fires bullets from the ship's current position and direction.
● 'a' and 'd' rotate the ship left and right.
● The ESC key can be used to exit the game.
main() Function:

● The main function:


● Initializes GLUT and sets up the game window.
● Registers callback functions for display and keyboard input.
● Initiates the game loop using glutMainLoop .
Physics and Collision

Physics:

● Bullets move at a constant speed (BULLET_SPEED) in the direction specified by their


angle.
● The ship's movement depends on its angle and thrust, with a speed of SHIP_SPEED.

Collision Detection:

● Collision detection between bullets and asteroids is done using the distance formula to
check if the distance between a bullet and an asteroid is less than the asteroid's size.
● When a bullet hits an asteroid, the bullet is marked as inactive (active = false), and the
asteroid is removed from the vector.
● The player's score increases by 10 points each time a bullet successfully hits an asteroid.

You might also like