Click here to Skip to main content
15,896,063 members
Articles / Multimedia / OpenGL

A Basic 3D Asteroid Game in openGL with C#

Rate me:
Please Sign up or sign in to vote.
4.83/5 (18 votes)
30 Dec 2011CPOL3 min read 95.6K   12.2K   62  
A Basic 3D Asteroid Game in openGL with C#
In this article, you will learn the basics of what a game should have including a score, levels of difficulty and a life counter.
using System;
using System.Collections.Generic;
using System.Text;
using Tao.OpenGl;
using System.Windows.Forms;
using ShadowEngine;
using ShadowEngine.Sound;
using ShadowEngine.ContentLoading; 

namespace Naves
{
    public class SpaceShip
    {
        int vidas = 3;
        Position p = new Position(0, 0, 0);
        int count;
        int compare = 10;
        ModelContainer m;
        bool moviendo;

        public bool Moviendo
        {
            get { return moviendo; }
            set { moviendo = value; }
        }

        public int Vidas
        {
            get { return vidas; }
            set { vidas = value; }
        }

        public void Reiniciar()
        {
            vidas = 3;
            p = new Position(0, 0, 0);
        }

        public void Create()
        {
             m = ContentManager.GetModelByName("nave.3DS");
             m.CreateDisplayList(); 
        }

        public void Dibujar()
        {
            if (vidas > 0)
            {
                switch (Main.Moviendo)
                {
                    case 1:
                        {
                            if (p.x < 3)
                                p.x += 0.08f;
                            break;
                        }
                    case -1:
                        {
                            if (p.x > -3)
                                p.x -= 0.08f;
                            break;
                        }
                    case 2:
                        {
                            if (p.y < 3)
                                p.y += 0.08f;
                            break;
                        }
                    case -2:
                        {
                            if (p.y > -3)
                                p.y -= 0.08f;
                            break;
                        }
                    default:
                        break;
                }

                count++;
                if (count == compare)
                {
                    if (compare == 25)
                    {
                        compare = 10;
                    }
                    Position wingLeft = new Position(this.p.x - 1f, this.p.y, this.p.z);
                    Position wingRight = new Position(this.p.x + 1f, this.p.y, this.p.z);
                    if (AsteroidGenerator.CheckCollision(this.p, 1.1f))
                    {
                        RestarVida(); 
                    }
                    else
                        if (AsteroidGenerator.CheckCollision(wingRight, 1.1f))
                        {
                            RestarVida(); 
                        }
                        else
                            if (AsteroidGenerator.CheckCollision(wingLeft, 1.1f))
                            {
                                RestarVida(); 
                            }
                    count = 0;
                }


                Gl.glPushMatrix();
                Gl.glTranslatef(p.x, p.y, p.z);
                Gl.glScalef(0.3f, 0.3f, 0.3f);
                m.DrawWithTextures();
                Gl.glPopMatrix();
                Gl.glDisable(Gl.GL_TEXTURE_2D);
            }

           
        }
        void RestarVida()
        {
            //AudioPlayback.PlayOne("choque.mp3");
            compare = 25;
            vidas--;
            p = new Position(0, 0, 0);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States 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. 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...

Comments and Discussions