12,242,718 members (54,575 online)
alternative version

49K views
57 bookmarked
Posted

# A Basic 3D Asteroid Game in openGL with C#

, 30 Dec 2011 CPOL
 Rate this:
A Basic 3D Asteroid Game in openGL with C#

## Introduction

This article is intended for beginners who want to start with 3D game programming and don’t know where to start. It is programmed in Opengl using VS 2008 and a small graphic engine I made myself called Shadowengine. It has the basics of what a game should have: a Score, levels of difficulty and a life counter. All this is written in a small number of code lines, with the objective of being simple and understandable.

The first problem I had to achieve is the scene to look like outer space. For that issue, I set the background color to black. In opengl, it is set this way:

`Gl.glClearColor(0, 0, 0, 1);//red green blue alpha `

The other problem were the stars and I solve it by drawing random white points on the screen. The algorithm is more or less this way. I generate a random point and measure the distance to the spaceship, and if it is less than a predefined number, I discard it and repeat the process until it creates the desired stars. Look at the code:

```public void CreateStars(int cantidad)
{
Random r = new Random();
int count = 0;
{
Position p = default(Position);
p.x = (r.Next(110)) * (float)Math.Pow(-1, r.Next());
p.z = (r.Next(110)) * (float)Math.Pow(-1, r.Next());
p.y = (r.Next(110)) * (float)Math.Pow(-1, r.Next());
if (Math.Pow(Math.Pow(p.x, 2) + Math.Pow(p.y, 2) +
Math.Pow(p.z, 2), 1 / 3f) > 15)
{
count++;
}
}
}```

The score is a number which increases over time and it grows faster everytime I pass a level. The level increases every 450 frames.

The last issue I had to address is the problem of asteroid collision. I make for the ship three positions (one for the ship and two for the wings) and every once in a while, I check the distance between all the asteroids and those three positions. If it is under a predefined value, I execute the collision event.

The project has 6 classes:

• AsteroidGenerator.cs - It handles the creation of asteroids in random places and with random sizes and speeds. It also has the method to query whether or not there has been a collision between the spaceship and an asteroid.
• Asteroid.cs - It handles all concerning an asteroid such as the movement, texture selection, drawing, among others.
• Star.cs - It has a method to create random white points and to draw them.
• Camera.cs - It handles the selection of the user camera and to set the right perspective view of the scene.
• SpaceShip.cs - This class contains all methods and attributes regarding the spaceship, such as movement, drawing, etc.
• Controller.cs - This class manages the game creation, game logic and contains all the classes needed to run the game. Here is a portion of code:
```using System;
using System.Collections.Generic;
using System.Text;

namespace Naves
{
public class Controller
{
Camera camara = new Camera();
Star star = new Star();
SpaceShip spaceShip = new SpaceShip();
public SpaceShip Nave
{
get { return spaceShip; }set { spaceShip = value; }
}
public Camera Camara
{
get { return camara; }
}
public void BeginGame()
{
AsteroidGenerator.GenerateAsteroid(35, false);
}
public void ResetGame()
{
AsteroidGenerator.GenerateAsteroid(35, true);
spaceShip.Reiniciar();
}
public void CreateObjects()
{
star.CreateStars(450);
spaceShip.Create();
Asteroid.Crear();
}
public void DrawScene()
{
star.Draw();
AsteroidGenerator.DrawAsteroids();
spaceShip.Dibujar();
}
}
}```

Main.cs - This is the form which contains this visual components of the game. It contains the controller class and gives the player information through proper windows controls. It has a timer to periodically draw the scene and has the code for texture and object loading.

If you want to add sound to the project, uncomment the commented lines and press Ctrl+Alt+E and under managed debugging assistants, uncheck the loaderlock exception.

I am hoping to receive feedback from this example. If you like it, you can visit my blog at www.vasilydev.blogspot.com for more stuff like this. I’ll try to make a version where the spaceship can shoot at the asteroids.

## History

• 28th December, 2011: Initial version

## Share

 Software Developer United States
Born on 86, had my first computer at the age of 8, wrote my first code at the age of 15(pascal), began to study software engineering at 2005 and graduated in 2010. Now I am currently developing software for ERP systems in "Michell Consulting, LLC" . I have a dev blog www.vasilydev.blogspot.com. My real passion is 3D game programming and playing guitar. I've programmed stuff in C#, python, Delphi, PHP, C++, JS, QT and others...

## You may also be interested in...

 First Prev Next
 How is the texturas mapped to the nave.3ds? bitzhuwei15-Jun-13 22:30 bitzhuwei 15-Jun-13 22:30
 Re: How is the texturas mapped to the nave.3ds? Vasily Tserekh16-Jun-13 6:10 Vasily Tserekh 16-Jun-13 6:10
 Re: How is the texturas mapped to the nave.3ds? Vasily Tserekh16-Jun-13 6:14 Vasily Tserekh 16-Jun-13 6:14
 Re: How is the texturas mapped to the nave.3ds? bitzhuwei15-Jul-13 18:28 bitzhuwei 15-Jul-13 18:28
 Re: How is the texturas mapped to the nave.3ds? Vasily Tserekh17-Jul-13 5:25 Vasily Tserekh 17-Jul-13 5:25
 Little Tidbit/Suggestion Death2592-May-12 9:12 Death259 2-May-12 9:12
 Re: Little Tidbit/Suggestion Vasily Tserekh5-May-12 18:48 Vasily Tserekh 5-May-12 18:48
 Re: Little Tidbit/Suggestion Death2597-May-12 7:52 Death259 7-May-12 7:52
 Re: Little Tidbit/Suggestion Vasily Tserekh7-May-12 10:11 Vasily Tserekh 7-May-12 10:11
 Re: Little Tidbit/Suggestion Death2597-May-12 10:17 Death259 7-May-12 10:17
 Re: Little Tidbit/Suggestion Vasily Tserekh8-May-12 7:03 Vasily Tserekh 8-May-12 7:03
 Re: Little Tidbit/Suggestion Death2598-May-12 7:04 Death259 8-May-12 7:04
 Is source code available? Perry217-Apr-12 9:08 Perry2 17-Apr-12 9:08
 Re: Is source code available? Death2592-May-12 9:16 Death259 2-May-12 9:16
 This is really cool! Larry Boeldt22-Mar-12 17:33 Larry Boeldt 22-Mar-12 17:33
 Re: This is really cool! Vasily Tserekh23-Mar-12 9:39 Vasily Tserekh 23-Mar-12 9:39
 My vote of 5 fredatcodeproject31-Dec-11 15:41 fredatcodeproject 31-Dec-11 15:41
 What .NET OpenGL Library Are You Using? BlazeCell30-Dec-11 6:38 BlazeCell 30-Dec-11 6:38
 Re: What .NET OpenGL Library Are You Using? J4\$PER30-Dec-11 9:46 J4\$PER 30-Dec-11 9:46
 Re: What .NET OpenGL Library Are You Using? GL_Terminator30-Dec-11 11:03 GL_Terminator 30-Dec-11 11:03
 Last Visit: 31-Dec-99 19:00     Last Update: 30-Apr-16 2:24 Refresh 1