Click here to Skip to main content
15,895,667 members
Articles / Multimedia / OpenGL

A Very Simple Car Race Game in C# and OpenGL

Rate me:
Please Sign up or sign in to vote.
4.89/5 (25 votes)
30 Mar 2020CPOL3 min read 141.8K   18.4K   44  
A simple straightforward car race game with minimum LOC.
This article is a simple straightforward car race game with minimum lines of code. There are 3 cars that begin in a start line, they randomly change their speeds on the race and the race ends on an end line, quite simple.
/*
 * This projects belongs to Vasily Tserekh, if you find it useful 
 * Stop by for my blog and make a comment, I'll really apreciate some 
 * feedback
 *http://vasilydev.blogspot.com/
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using ShadowEngine;
using Tao.OpenGl; 
using System.Windows.Forms;  
using ShadowEngine.ContentLoading; 

namespace CarRace
{
    class Car
    {
        float tireAngle;
        static int carColor = 1;
        Color color;
        Position pos;
        float speed;
        float traveledDistance = 0;
        static Random randomizer = new Random(); // to randomize the speed value
        int counter;
        ModelContainer m;
        
        List<Mesh> tires = new List<Mesh>();
        Mesh body; // this is the chassis of the car
        
        int texture;


        public float TraveledDistance
        {
            get { return traveledDistance; }
        }

        public Car()
        {
            if (carColor == 1)
            {
                color = Color.Blue;
                pos = new Position(0, 10);  
            }
            if (carColor == 2)
            {
                color = Color.Red;
                pos = new Position(1.5f, 10);  
            }
            if (carColor == 3)
            {
                color = Color.Black;
                pos = new Position(3, 10);
            }
            carColor++;
            speed = 0.2f + (float)randomizer.NextDouble() / 18f;
        }

        public void ResetRace()
        {
            traveledDistance = 0;
        }

        public void Create()
        {
            m = ContentManager.GetModelByName("carro.3DS");
            m.CreateDisplayList(); //optimice the model and load it in opengl display lists
            
            m.ScaleX = 0.1f;
            m.ScaleY = 0.1f;
            m.ScaleZ = 0.1f;

            foreach (var item in m.GetMeshes)
            {
                item.CalcCenterPoint();// calculate th epivot point
                switch (item.Name)
                {
                    case "tireA":
                    case "tireB":
                    case "tireC":
                    case "tire":
                    case "rimA":
                    case "rimB":
                    case "rimC":
                    case "rim":
                        tires.Add(item);
                        break; 
                    case "body":
                         body = item;
                         break;
                } 
            }

            if (color == Color.Blue)
            {
                texture = ContentManager.GetTextureByName("bodyBlue.jpg");   
            }
            if (color == Color.Red)
            {
                texture = ContentManager.GetTextureByName("bodyRed.jpg");
            }
            if (color == Color.Black)
            {
                texture = ContentManager.GetTextureByName("bodyBlack.jpg");
                m.RemoveMeshesWithName("body");
                m.RemoveMeshesWithName("tire");
                m.RemoveMeshesWithName("rim");
            }
        }

        public void Draw()
        {
            Gl.glPushMatrix();
            Gl.glTranslatef(pos.x, 0, pos.y);

            #region race logic

            if (Controller.StartedRace == true)
            {
                //move the object the traveled distance
                Gl.glTranslatef(0, 0, traveledDistance); 
                if (traveledDistance > -59)
                {
                    tireAngle -= 24;
                    traveledDistance -= speed;
                }
                else
                    if (Controller.FinishedRace == false)
                    {
                        Controller.FinishedRace = true;
                    }
                counter++;
                // if counter == 30 i change the speed
                if (counter == 30)
                {
                    counter = 0;
                    speed = 0.2f + (float)randomizer.NextDouble() / 20f;
                }
            }

            #endregion

            m.DrawWithTextures(); // draw the car accesories

            #region draw chasis

            Gl.glPushMatrix();
            Gl.glScalef(0.1f, 0.1f, 0.1f);

            Gl.glEnable(Gl.GL_TEXTURE_2D);// enable textures
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
            body.Draw();

            Gl.glDisable(Gl.GL_TEXTURE_2D);
            Gl.glPopMatrix(); 

            #endregion

            #region draw rolling tires

            foreach (var item in tires)
            {
                Gl.glColor3f(0.5f, 0.5f, 0.5f);//especifico el color de la rueda
                Gl.glPushMatrix();
                Gl.glScalef(0.1f, 0.1f, 0.1f);
                Gl.glTranslatef(item.CenterPoint.X, item.CenterPoint.Y, item.CenterPoint.Z); //traslada a la posicion original 
                Gl.glRotatef(tireAngle, 1, 0, 0);// se rota 
                Gl.glTranslatef(-item.CenterPoint.X, -item.CenterPoint.Y, -item.CenterPoint.Z); // traslado al centro
                item.Draw();  // dibujo la rueda 
                Gl.glPopMatrix();
                Gl.glColor3f(1, 1, 1); 
            }

            #endregion

            Gl.glPopMatrix();
        }
    }
}

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