# Asteroids game using C++

If you are new to programming then you must always have wondered how tough could it be implementing a language in developing a game. But it is as easy as implementing some simple mathematical formulae to a problem and obtaining a solution.

To start with this C++ project, let me introduce you to the game. There are asteroids, a spaceship, and obviously a black screen in the game. The game is based in space where the user can rotate the spaceship and shoot bullets and try and break the floating asteroids which come in his way. Colliding with the asteroid at any moment will end and restart the game. Hence, the game is really about how long can the user survive.

Looking to build projects on C++?:

C++ Kit will be shipped to you and you can learn and build using tutorials. You can start for free today!

Now, to build this game all you need to have is a good knowledge of C++, some simple mathematical and kinematic formulae, and a command prompt game engine.

The command prompt engine will allow graphics and input in the console at the command line. Hence, import the necessary libraries and start by creating two functions namely user create and user update. The former will be called when the application launches and later updates the screen.

1. First and foremost, start by building your space that can be done with your function user update which will fill your screen with black pixels.

2. Next are the main elements of the space that is asteroids and spaceships. For these, you need to create a struct that will represent asteroids and spaceships.

3. The struct will have x and y coordinates for positions and dx and dy co-ordinates for velocity and direction. You will have to store the variable size of the asteroids here as well. You have to choose the shape of a circle for the asteroids and draw it by iterating through x and y and draw function to create pixels.

4. Store the asteroids and spaceship in vectors.

5. One most important thing that you will have to keep in mind is that the mapping of the screen should be toroidal such that if the floating asteroids cross the screen at one end, it returns from the other. This can be achieved with some concepts of polymorphism.

## Latest projects on C++

Want to develop practical skills on C++? Checkout our latest projects and start learning for free

6. The kinematics equations that you will need to change the positions of the objects on the screen/space will be-

If P2 and V2 are a new position and new velocity of the object and A and V are the acceleration and known velocity of the object then,

P2= Vt + P1

V2= At + V1, where ‘t’ is the time taken.

7. Coming to our mini-spaceship the suggested shape should be of an isosceles triangle. Users need to control the angle of rotation of the ship and also apply thrust. For which, you can apply some basic trigonometric formulae of sin and cos.

8. The left and the right key will help rotate it and the up-arrow key will help apply thrust.

9. Last but not the least, draw a bullet which is nothing but a single white pixel and the space key button releases the bullets from the ship. Consider that toroidal mapping does not apply to bullets. Any bullet gone off the screen should be removed.

10. When the bullet shoots an asteroid, it divides it into two i.e. if the size of an asteroid is 16 then 16 to 8-8, 8 to 4-4, 4 to 2-2, and 2 to 0.

11. To kill the spaceship, apply collision detection i.e. if the distance between the points of the spaceship and the midpoint of any asteroid is less than the radius of the asteroid then the collision has happened and the game ends.

Did you know

Skyfi Labs helps students learn practical skills by building real-world projects.

You can learn from experts, build working projects, showcase skills to the world and grab the best jobs.
Get started today!

Kit required to develop Asteroids game using C++:
Technologies you will learn by working on Asteroids game using C++:
###### Asteroids game using C++
Skyfi Labs Last Updated: 2022-04-16

Join 250,000+ students from 36+ countries & develop practical skills by building projects

Get kits shipped in 24 hours. Build using online tutorials.

## Subscribe to receive more project ideas

Stay up-to-date and build projects on latest technologies