Click here to Skip to main content
15,885,216 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.3K   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.Collections.Generic;
using System.Text;
using ShadowEngine;
using Tao.OpenGl;  

namespace CarRace
{
    class Road
    {
        int initList;

        public void Create()
        { 
            initList = Gl.glGenLists(1);
            Gl.glNewList(initList, Gl.GL_COMPILE);

            int texturaDelimitador = ContentManager.GetTextureByName("delimitador.jpg");

            //start line
            Gl.glEnable(Gl.GL_TEXTURE_2D);
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, texturaDelimitador);
            Gl.glPushMatrix();
            Gl.glTranslatef(0, 0, 8);
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3f(-0.5f, 0.1f, 0);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3f(-0.5f, 0.1f, 1);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex3f(4f, 0.1f, 1);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3f(4f, 0.1f, 0);
            Gl.glEnd();
            Gl.glPopMatrix();

            //end line
            Gl.glPushMatrix();
            Gl.glTranslatef(0, 0, -50);
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3f(-0.5f, 0.05f, 0);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3f(-0.5f, 0.05f, 1);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex3f(3.5f, 0.05f, 1);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3f(3.5f, 0.05f, 0);
            Gl.glEnd();
            Gl.glPopMatrix();


            int texturaAsfalto = ContentManager.GetTextureByName("asfalto.jpg");
            Gl.glEnable(Gl.GL_TEXTURE_2D);
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, texturaAsfalto);
            Gl.glPushMatrix();
            Gl.glTranslatef(0, 0, -100);

            int count = 0;
            for (int y = 0; y < 40; y++)// this for loop draws the road
            {
                Gl.glBegin(Gl.GL_QUADS);
                Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3f(-0.8f, 0, count);
                Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3f(-0.8f, 0, count + 10);
                Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex3f(3.8f, 0, count + 10);
                Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3f(3.8f, 0, count);
                Gl.glEnd();
                count += 10; 
            }

            Gl.glPopMatrix();

            Gl.glEndList(); 
        }

        public void Draw()
        {
            Gl.glCallList(initList);  
        }
    }
}

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