SEARCH C# PROGRAMMING CODES HERE

Car Racing Game in C# Visual Studio

Car Racing Game In C#

In this project I am going to show you how you can create a Car Racing Game in C# Visual Studio Using (.Net Framework) 
So before we start let's go through what you are going to learn ?
In this project you are going to learn, how you can handle controls like car speed using up and down arrow keys and about the random car movements and also you are going to learn to create a parallax scrolling background on win form, and you will understand the concept to detect collision between two objects and also to implement in the program.This game has a speed control meter which shows the current speed on the scoreboard 
This project is divided into three parts. 
In the first part we will create a new project in c# visual studio in which we will create the base for the user screen by settings properties and In the second part you will learn about designing and setting the properties for the user interface screen with the help of some required assets and the required components and in the third part you will learn to write the code and implement them in the program for all the controls for the user screen so simply follow these steps given below one by one to make it happen,so let’s start the project.


Part-1- To create a new project in c# Visual Studio

Open Visual Studio then Go to the top Menu Bar, Choose File > New > Project > Choose Visual C# then Click On Windows Desktop Then Select Windows Form App(.Net Framework) And Name It As Car Racing Game Then Press OK.


Once the New project created go to the Form1 properties and set the properties as follows.
Form1
Size-500,700
Start position-Center
Text-Car Racing Game














Part-2-User Screen Designing


1- Creating a Road Track

Now we need some Required Components for Designing Car Racing Game.
So add 3 Picture Boxes from the Toolbox and set the properties as follows.


2- Displaying Cars On The Road Track.

To display cars on the road track we need 3 picture boxes from the toolbox and then we will add the car images into all picture boxes one by one by setting the properties,I have already downloaded car vector images from the internet according to my choice you can do so,Now select picture boxes one by one and go to the properties section and set the properties as shown in the following figure.













Now you have to select and go to properties of each picture boxes
as (picturebox4,picturebox5,picturebox6) one by one and then click on the background image > Project Resources File and then click on the import button to add images from the computer as shown in the following figure.






















3-Displaying( Score),(Game-Over) and(Speed) Details.

To display the score, game over and speed details we need Label for the toolbox so add 3 labels from the toolbox and set the properties as shown in the following figure.















Part3- Now we will move to the coding part.

4-Creating Road Track Movements.

 To get the effect that car is running on the road track for that we have to make the Road track to move continues to get the effect, we required 1 timer to play this important role, so add one-timer from the toolbox and set the properties as shown in the figure below.











Now we will create a function for all the road tracks(track_1, track2, track_3), to move continues, so for that go to the Form1 code view section and copy the red highlighted code as follows.

using System;
using System. Drawing;
using System.Windows.Forms;

namespace Car_Racing_Game
{
    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void track_line(int speed)
        {
            if (track_1.Top >= 700)
            {
                track_1.Top = 0;
            }
            else
            {
                track_1.Top += speed;
            }
            if (track_2.Top >= 700)
            {
                track_2.Top = 0;
            }
            else
            {
                track_2.Top += speed;
            }
            if (track_3.Top >= 700)
            {
                track_3.Top = 0;
            }
            else
            {
                track_3.Top += speed;
            }

        }


Now to perform the above function we have to call it, so for that go to the Form1 design view and at the bottom double click on timer1 
Then it will take you to the timer1_Tick event then simply copy the red highlighted code as follows.

 private void timer1_Tick(object sender, EventArgs e)
        {
            track_line(5);
        }

5-Car controls to move right or left.
To move the car right or left we have to create a function to perform this task,  so for that go to the Form 1 properties section and click on events then select on a key Down event and double click on it as shown in the figure below.
Now it will take you to the Form 1 Key down event now simply copy the red highlighted code as following.

 private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode ==Keys.Left )  // this function is for moving left//
            {
                if(player.Left>50)
                {
                    player.Left += -35;
                }
            }
            else if(e.KeyCode==Keys.Right) //this function is for moving right//
            {
                if(player.Right<450)
                {
                    player.Left += 35;
                }
            }

6-Enemy Car control to move randomly from top to bottom…..

To perform this task we have to create a function to measure the distance between top to bottom in the sense from X location to Y location or you can say from one place to another and also to perform random moves for enemy cars , so go back to the track_line(int speed) function, inside this function copy the red highlighted code as following.

  private void track_line(int speed)
        {
            if (track_1.Top >= 700)
            {
                track_1.Top = 0;
            }
            else
            {
                track_1.Top += speed;
            }
            if (track_2.Top >= 700)
            {
                track_2.Top = 0;
            }
            else
            {
                track_2.Top += speed;
            }
            if (track_3.Top >= 700)
            {
                track_3.Top = 0;
            }
            else
            {
                track_3.Top += speed;
            }
            Random rnd = new Random();
            int x, y;
            if (enemy_1.Top >= 650)
            {
                x = rnd.Next(0, 200);
                enemy_1.Location = new Point(x, 0);
            }
            else
            {
                enemy_1.Top += speed;
            }

            if (enemy_2.Top >= 500)
            {
                y = rnd.Next(0, 400);
                enemy_2.Location = new Point(y, 0);
            }
            else
            {
                enemy_2.Top += speed;
            }
        }


 7- Displaying Score on the user screen….
To display the score on the user screen we have to check the distance between top to bottom in other words from X location to Y location,
Increase the score on the user screen once the enemy reaches from
X location to Y location then( Score=+10)For this we will create an if else condition in which we will define X, Y location to perform this task, go to timer1 Tick Event and copy the red highlighted code as follows.

 int score = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            track_line(5);
            Gameover();

            if (enemy_1.Top > 560)
            {
                enemy_1.Top = 0;
                score += 10;
                lbl_score.Text = "Score - " + score;
            }
            else if (enemy_2.Top > 560)
            {
                enemy_2.Top = 0;
                score += 10;
                lbl_score.Text = "Score - " + score;
            }
        }
 8- Speed controls…
Now we will create a function for the speed control when the up arrow key pressed then the speed should increase and when the down arrow key pressed Speed should decrease. To perform this task first declare the current speed as well as player speed so for that go to the partial class Form 1 section and copy the red highlighted code.


 public partial class Form1: Form
    {
        int currentSpeed = 1;
        int PlayerSpeed = 0;
       
 public Form1()
        {
            InitializeComponent();
}

Now go to the Form 1 key Down Event and copy the red highlighted code as below.

 private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                if (player.Left > 50)
                {
                    player.Left += -35;
                }
            }
            else if (e.KeyCode == Keys.Right)
            {
                if (player.Right < 450)
                {
                    player.Left += 35;
                }
            }
            else if (e.KeyCode == Keys.Up)
            {
                if (PlayerSpeed < 50)
                    PlayerSpeed++;
                    lbl_speed.Text = "Speed -" + currentSpeed + " MPH".ToString();
                currentSpeed++;
            }
            else if (e.KeyCode == Keys.Down)
            {
                if (PlayerSpeed < 50)
                    PlayerSpeed--;
                    lbl_speed.Text = "Speed -" + currentSpeed + " MPH".ToString();
                currentSpeed--;
            }
        }
To perform the above function correctly go to the timer1 Tick Event and replace the blue code to red code as follows.

private void timer1_Tick(object sender, EventArgs e)
        {
           // track_line(5);//
            track_line(currentSpeed);
            Gameover();
            if (enemy_1.Top > 560)
            {
                enemy_1.Top = 0;
                score += 10;
                lbl_score.Text = "Score - " + score;
            }
            else if (enemy_2.Top > 560)
            {
                enemy_2.Top = 0;
                score += 10;
                lbl_score.Text = "Score - " + score;
            }
        }
9- Using Collision in the program….
Collision is short-duration interaction between two objects or more than two objects so in this project we have 1 player car and 2 enemy ‘s cars so these are the objects in between, we will create a condition to perform collision, Condition will be very simple, when the enemy car hit ‘s the player car then the player Car running (Speed =0 Mph, Score Text= Score = ” ”, Show Message =Game-Over,timer1.stop=true)
To perform this task we will create a game-over function, inside this function we will define the if else conditions to perform this task to happen, so for that go to the Form 1 code view section and copy the red highlighted code as follows.

 public Form1()
        {
            InitializeComponent();
        }

        private void Gameover()
        {
            if (player.Bounds.IntersectsWith(enemy_1.Bounds))
            {
                timer1.Enabled = false;
                lbl_game_over.Visible = true;
                lbl_speed.Text = "Speed -0 MPH";

            }
            if (player.Bounds.IntersectsWith(enemy_2.Bounds))
            {
                timer1.Enabled = false;
                lbl_game_over.Visible = true;
                lbl_speed.Text = "Speed -0 MPH";
            }
        }

 private void Form1_Load(object sender, EventArgs e)
        {
            lbl_game_over.Hide();
        }

To perform the above function we have to call it first so go to the timer1 Tick Event and copy the red highlighted code as follows.

 private void timer1_Tick(object sender, EventArgs e)
        {
            track_line(5);
            Gameover();
         }
So finally we are done with this project Now run the program and check all controls are working correctly or not if any error occurs don’t worry
Recheck all your source code once as given below and try again.

Full Source code

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Car_Racing_Game
{
    public partial class Form1: Form
    {
        int currentSpeed = 1;
        int PlayerSpeed = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Gameover()
        {
            if (player.Bounds.IntersectsWith(enemy_1.Bounds))
            {
                timer1.Enabled = false;
                lbl_game_over.Visible = true;
                lbl_speed.Text = "Speed -0 MPH";

            }
            if (player.Bounds.IntersectsWith(enemy_2.Bounds))
            {
                timer1.Enabled = false;
                lbl_game_over.Visible = true;
                lbl_speed.Text = "Speed -0 MPH";
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            lbl_game_over.Hide();
        }
        

        private void track_line(int speed)
        {
            if (track_1.Top >= 700)
            {
                track_1.Top = 0;
            }
            else
            {
                track_1.Top += speed;
            }
            if (track_2.Top >= 700)
            {
                track_2.Top = 0;
            }
            else
            {
                track_2.Top += speed;
            }
            if (track_3.Top >= 700)
            {
                track_3.Top = 0;
            }
            else
            {
                track_3.Top += speed;
            }
            Random rnd = new Random();
            int x, y;
            if (enemy_1.Top >= 650)
            {
                x = rnd.Next(0, 200);
                enemy_1.Location = new Point(x, 0);
            }
            else
            {
                enemy_1.Top += speed;
            }

            if (enemy_2.Top >= 500)
            {
                y = rnd.Next(0, 400);
                enemy_2.Location = new Point(y, 0);
            }
            else
            {
                enemy_2.Top += speed;
            }
        }

        int score = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            track_line(currentSpeed);
            Gameover();
            if (enemy_1.Top > 560)
            {
                enemy_1.Top = 0;
                score += 10;
                lbl_score.Text = "Score - " + score;
            }
            else if (enemy_2.Top > 560)
            {
                enemy_2.Top = 0;
                score += 10;
                lbl_score.Text = "Score - " + score;
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                if (player.Left > 50)
                {
                    player.Left += -35;
                }
            }
            else if (e.KeyCode == Keys.Right)
            {
                if (player.Right < 450)
                {
                    player.Left += 35;
                }
            }
            else if (e.KeyCode == Keys.Up)
            {
                if (PlayerSpeed < 50)
                    PlayerSpeed++;
                    lbl_speed.Text = "Speed -" + currentSpeed + " MPH".ToString();
                currentSpeed++;
            }
            else if (e.KeyCode == Keys.Down)
            {
                if (PlayerSpeed < 50)
                    PlayerSpeed--;
                    lbl_speed.Text = "Speed -" + currentSpeed + " MPH".ToString();
                currentSpeed--;
            }
        }
    }
}