Click here to Skip to main content
15,881,173 members
Articles / Multimedia / OpenGL
Tip/Trick

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.1K   18.4K   44   25
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.

Introduction

Yesterday, I was looking at the demo I published in my last article, the asteroid demo, it had a good acceptance because it was made in a very simple way and today I will do the same. This article is a simple straightforward car race game with minimum lines of code (LOC). 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. The user can follow the race with the keys W (forward), S (backward) and can change the camera takes. At the end of the race, a message is shown telling which car was the winner and you are able to reset the race and start over again. The car race is a simulation and the user can’t take control over any car.

Well, let’s dive into the code:

Above is the project solution explorer. As you can see, there are only a few classes. I will give a brief explanation of what each class does specifically.

Camara.cs

This class has two methods: one for selecting a camera position and another for initializing the camera. The code is very simple.

C#
public class Camara
{
    public void SelectCamara(int camara)
    {
        Gl.glMatrixMode(Gl.GL_MODELVIEW);
        Gl.glLoadIdentity();
        switch (camara)
        {
            case 0:
                {
                    Glu.gluLookAt(2, 3, 14, 2, 0, 5, 0, 1, 0);
                    break;
                }
            case 1:
                {
                    Glu.gluLookAt(-2, 7, 10, 1, 3, 1, 0, 1, 0);
                    break;
                }
            case 2:
                {
                    Glu.gluLookAt(0, 35, -15, 1, 0, -16, 0, 1, 0);
                    break;
                }
            case 3:
                {
                    Glu.gluLookAt(3, 3, -47, 1, 0, 1, 0, 1, 0);
                    break;
                }
        }
    }

    public void SetPerspective()
    {
        //select the projection matrix
        Gl.glMatrixMode(Gl.GL_PROJECTION);
        //reset it
        Gl.glLoadIdentity();
        //55 = vision angle
        //1  = aspect ratio
        //0.1f = minimum draw distance
        //1000 = maximum draw distance
        Glu.gluPerspective(55, 1, 0.1f, 1000);
        SelectCamara(0);
    }
}

Car.cs

A 3D model is a set of meshes and this car is not an exception. In this class, I make a little tweak to draw the car. I draw the tires and the rims separate from the car because they have a rolling movement besides a displacement movement. Then, I make a list of the meshes that roll and calculate their pivot point which is its center and insert it to a separate list.

When I draw a car, I draw the chassis, accessories, and the tires separately.

The chassis takes the color of the car, the accessories are the same in the three cars, and the tires and rims go in a list because I rotate them.

This portion of code goes on the drawing function of the car and contains the race logic:

C#
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;
   }
}

If the race has started, then in each frame, I translate the car on the Z axis through the traveled distance which increases by adding a number called speed, and every 30 frames I randomize that speed. This will keep the race interesting giving the same shots to every car to win the race.

World.cs

This class is for drawing the skybox and the terrain (not the road). As you will see later on the project, it is very simple..

Road.cs

This class draws the road and the begin and end line. For drawing the road, I made a for loop drawing quads containing the texture of the road.

Controller.cs

This class manages the drawing of the entire scene as well as methods to restart the race, querying which cars go first, among others.

Mainform.cs

This form is where everything is drawn; it has a timer which draws the scene at a regular interval and the buttons to select the cameras and to restart the race. Inside the code, you will find the graphics initialization function, the lighting setup, the resize screen algorithm, etc.

I hope you find this project useful.

History

  • 29th March, 2012: Initial version

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

 
Question5 stars once you add much more detail to the post and make it an article. Pin
cplas6-Apr-20 10:01
cplas6-Apr-20 10:01 
QuestionDisplay the game in a panel instead of using the whole form Pin
Member 431485919-Mar-19 23:29
Member 431485919-Mar-19 23:29 
AnswerRe: Display the game in a panel instead of using the whole form Pin
Member 431485920-Mar-19 0:23
Member 431485920-Mar-19 0:23 
QuestionPls help me with the code that will make curve both the car and road and the direction to apply it. Pin
Member 1047036324-Jul-14 6:07
Member 1047036324-Jul-14 6:07 
QuestionHOW I PLAY THE GAME? Pin
Member 1095713418-Jul-14 23:48
Member 1095713418-Jul-14 23:48 
AnswerRe: HOW I PLAY THE GAME? Pin
Vasily Tserekh19-Jul-14 7:43
Vasily Tserekh19-Jul-14 7:43 
QuestionThanks! Pin
Yuvraj Singh 9418-May-14 4:52
Yuvraj Singh 9418-May-14 4:52 
AnswerRe: Thanks! Pin
Vasily Tserekh18-May-14 17:10
Vasily Tserekh18-May-14 17:10 
Questionhello Pin
chiaronghsieh12-May-14 20:20
chiaronghsieh12-May-14 20:20 
AnswerRe: hello Pin
Vasily Tserekh13-May-14 3:37
Vasily Tserekh13-May-14 3:37 
QuestionQuestion Pin
Member 1047036324-Jan-14 23:10
Member 1047036324-Jan-14 23:10 
QuestionCar racing Pin
Member 1040475022-Nov-13 13:20
Member 1040475022-Nov-13 13:20 
Questioncurves Pin
scf0724-Jan-13 18:34
scf0724-Jan-13 18:34 
AnswerRe: curves Pin
Vasily Tserekh3-Apr-13 16:05
Vasily Tserekh3-Apr-13 16:05 
AnswerRe: curves Pin
Vasily Tserekh3-Apr-13 16:07
Vasily Tserekh3-Apr-13 16:07 
Questioncargame controls Pin
lcckochin20-Sep-12 20:47
lcckochin20-Sep-12 20:47 
AnswerRe: cargame controls Pin
Vasily Tserekh21-Sep-12 4:34
Vasily Tserekh21-Sep-12 4:34 
Questionall the project code Pin
ahmed zeen aldeen 21-Apr-12 13:06
ahmed zeen aldeen 21-Apr-12 13:06 
AnswerRe: all the project code Pin
Vasily Tserekh21-Apr-12 16:50
Vasily Tserekh21-Apr-12 16:50 
GeneralRe: all the project code Pin
machallo14-Nov-12 5:57
machallo14-Nov-12 5:57 
GeneralRe: all the project code Pin
Vasily Tserekh14-Nov-12 6:13
Vasily Tserekh14-Nov-12 6:13 
GeneralRe: all the project code Pin
Vasily Tserekh14-Nov-12 6:16
Vasily Tserekh14-Nov-12 6:16 
Questioncontrol one car movement by keys Pin
Member 869287215-Apr-12 19:36
Member 869287215-Apr-12 19:36 
AnswerRe: control one car movement by keys Pin
Vasily Tserekh21-Apr-12 16:48
Vasily Tserekh21-Apr-12 16:48 
Questionmy vote of 5 Pin
beqiraj2542-Apr-12 3:10
beqiraj2542-Apr-12 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.