Hello readers…,
I am here with a new article. In this post lets learn how to create a simple game in visual studio using c#.
First lets talk about c#. There are many programming languages in the trending technology and in the latest technology there is a huge demand for c#.
C# is the language in the .net framework where you can build all types of applications what not everything like windows, web, mobile applications. Learning this language and coding in the language is the best option for your future carrier.
In this post lets learn how to create a game for Windows using C#.
Lets enter in to the topic.
For this all we have to do is open visual studio in your pc and then click on new then select new project. Then enter a name for your project as car racing then click on ok.
Design the game as shown in the picture below.
For more details and explanation checkout the video from my channel in YouTube. by clicking the below link.
https://youtu.be/XgzDnU3j-5Q
Hit a like for the video and subscribe to my channel on YouTube.
Code your game as the same code below.
Code for game…
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace car_racing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
over.Visible = false;
close.Visible = false;
restart.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
moveline(gamespeed);
enemy(gamespeed);
gameover();
}
int gamespeed = 0;
void moveline(int speed) // to move lines on the road//
{
if (pictureBox1.Top >= 500)
{
pictureBox1.Top = 0;
}
else
{
pictureBox1.Top += speed;
}
if (pictureBox2.Top >= 500)
{
pictureBox2.Top = 0;
}
else
{
pictureBox2.Top += speed;
}
if (pictureBox3.Top >= 500)
{
pictureBox3.Top = 0;
}
else
{
pictureBox3.Top += speed;
}
if (pictureBox4.Top >= 500)
{
pictureBox4.Top = 0;
}
else
{
pictureBox4.Top += speed;
}
if (pictureBox5.Top >= 500)
{
pictureBox5.Top = 0;
}
else
{
pictureBox5.Top += speed;
}
if (pictureBox6.Top >= 500)
{
pictureBox6.Top = 0;
}
else
{
pictureBox6.Top += speed;
}
if (pictureBox7.Top >= 500)
{
pictureBox7.Top = 0;
}
else
{
pictureBox7.Top += speed;
}
if (pictureBox8.Top >= 500)
{
pictureBox8.Top = 0;
}
else
{
pictureBox8.Top += speed;
}
if (pictureBox9.Top >= 500)
{
pictureBox9.Top = 0;
}
else
{
pictureBox9.Top += speed;
}
if (pictureBox10.Top >= 500)
{
pictureBox10.Top = 0;
}
else
{
pictureBox10.Top += speed;
}
if (pictureBox11.Top >= 500)
{
pictureBox11.Top = 0;
}
else
{
pictureBox11.Top += speed;
}
if (pictureBox12.Top >= 500)
{
pictureBox12.Top = 0;
}
else
{
pictureBox12.Top += speed;
}
}
Random r = new Random();
int a, b;
void enemy(int speed)
{
if (enemy1.Top >= 500)
{
a = r.Next(0, 100);
enemy1.Location = new Point(a, 0);
}
else
{
enemy1.Top += speed;
}
if (enemy2.Top >= 500)
{
a = r.Next(0, 250);
enemy2.Location = new Point(a, 0);
}
else
{
enemy2.Top += speed;
}
if (enemy3.Top >= 500)
{
a = r.Next(0, 150);
enemy3.Location = new Point(a, 0);
}
else
{
enemy3.Top += speed;
}
if (enemy4.Top >= 500)
{
a = r.Next(0, 350);
enemy4.Location = new Point(a, 0);
}
else
{
enemy4.Top += speed;
}
}
void gameover()
{
if (car.Bounds.IntersectsWith(enemy1.Bounds))
{
timer1.Enabled = false;
over.Visible = true;
close.Visible = true;
restart.Visible = true;
}
if (car.Bounds.IntersectsWith(enemy2.Bounds))
{
timer1.Enabled = false;
over.Visible = true;
close.Visible = true;
restart.Visible = true;
}
if (car.Bounds.IntersectsWith(enemy3.Bounds))
{
timer1.Enabled = false;
over.Visible = true;
close.Visible = true;
restart.Visible = true;
}
if (car.Bounds.IntersectsWith(enemy4.Bounds))
{
timer1.Enabled = false;
over.Visible = true;
close.Visible = true;
restart.Visible = true;
}
}
private void close_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void restart_Click(object sender, EventArgs e)
{
Application.Restart();
}
//To control the car. //
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
if (car.Left > 0)
car.Left += -8;
}
if (e.KeyCode == Keys.Right)
{
if (car.Right < 350)
car.Left += 8;
}
if (e.KeyCode == Keys.Up)
{
if (gamespeed < 20)
gamespeed++;
}
if (e.KeyCode == Keys.Down)
{
if (gamespeed > 0)
gamespeed--;
}
}
}
}
That’s all for code then run the program using ctrl+F5.
Checkout the video from youtube for more complete procedure and steps to right the code.
Subscribe my youtube channel for more interesting videos and hit a like for the video share to your friends as they can also learn programming.
Hope you enjoyed creating the game.
Like the post and subcribe for more interesting posts..
Thank you
Your s
vinay
I am here with a new article. In this post lets learn how to create a simple game in visual studio using c#.
First lets talk about c#. There are many programming languages in the trending technology and in the latest technology there is a huge demand for c#.
C# is the language in the .net framework where you can build all types of applications what not everything like windows, web, mobile applications. Learning this language and coding in the language is the best option for your future carrier.
In this post lets learn how to create a game for Windows using C#.
Lets enter in to the topic.
1. Selecting new project.
For this all we have to do is open visual studio in your pc and then click on new then select new project. Then enter a name for your project as car racing then click on ok.2. Designing for application
Before coding the game, we have to create a design for our game. So, lets design our game.Design the game as shown in the picture below.
For more details and explanation checkout the video from my channel in YouTube. by clicking the below link.
https://youtu.be/XgzDnU3j-5Q
Hit a like for the video and subscribe to my channel on YouTube.
Coding the program
Now its time to code for our project.Code your game as the same code below.
Code for game…
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace car_racing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
over.Visible = false;
close.Visible = false;
restart.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
moveline(gamespeed);
enemy(gamespeed);
gameover();
}
int gamespeed = 0;
void moveline(int speed) // to move lines on the road//
{
if (pictureBox1.Top >= 500)
{
pictureBox1.Top = 0;
}
else
{
pictureBox1.Top += speed;
}
if (pictureBox2.Top >= 500)
{
pictureBox2.Top = 0;
}
else
{
pictureBox2.Top += speed;
}
if (pictureBox3.Top >= 500)
{
pictureBox3.Top = 0;
}
else
{
pictureBox3.Top += speed;
}
if (pictureBox4.Top >= 500)
{
pictureBox4.Top = 0;
}
else
{
pictureBox4.Top += speed;
}
if (pictureBox5.Top >= 500)
{
pictureBox5.Top = 0;
}
else
{
pictureBox5.Top += speed;
}
if (pictureBox6.Top >= 500)
{
pictureBox6.Top = 0;
}
else
{
pictureBox6.Top += speed;
}
if (pictureBox7.Top >= 500)
{
pictureBox7.Top = 0;
}
else
{
pictureBox7.Top += speed;
}
if (pictureBox8.Top >= 500)
{
pictureBox8.Top = 0;
}
else
{
pictureBox8.Top += speed;
}
if (pictureBox9.Top >= 500)
{
pictureBox9.Top = 0;
}
else
{
pictureBox9.Top += speed;
}
if (pictureBox10.Top >= 500)
{
pictureBox10.Top = 0;
}
else
{
pictureBox10.Top += speed;
}
if (pictureBox11.Top >= 500)
{
pictureBox11.Top = 0;
}
else
{
pictureBox11.Top += speed;
}
if (pictureBox12.Top >= 500)
{
pictureBox12.Top = 0;
}
else
{
pictureBox12.Top += speed;
}
}
Random r = new Random();
int a, b;
void enemy(int speed)
{
if (enemy1.Top >= 500)
{
a = r.Next(0, 100);
enemy1.Location = new Point(a, 0);
}
else
{
enemy1.Top += speed;
}
if (enemy2.Top >= 500)
{
a = r.Next(0, 250);
enemy2.Location = new Point(a, 0);
}
else
{
enemy2.Top += speed;
}
if (enemy3.Top >= 500)
{
a = r.Next(0, 150);
enemy3.Location = new Point(a, 0);
}
else
{
enemy3.Top += speed;
}
if (enemy4.Top >= 500)
{
a = r.Next(0, 350);
enemy4.Location = new Point(a, 0);
}
else
{
enemy4.Top += speed;
}
}
void gameover()
{
if (car.Bounds.IntersectsWith(enemy1.Bounds))
{
timer1.Enabled = false;
over.Visible = true;
close.Visible = true;
restart.Visible = true;
}
if (car.Bounds.IntersectsWith(enemy2.Bounds))
{
timer1.Enabled = false;
over.Visible = true;
close.Visible = true;
restart.Visible = true;
}
if (car.Bounds.IntersectsWith(enemy3.Bounds))
{
timer1.Enabled = false;
over.Visible = true;
close.Visible = true;
restart.Visible = true;
}
if (car.Bounds.IntersectsWith(enemy4.Bounds))
{
timer1.Enabled = false;
over.Visible = true;
close.Visible = true;
restart.Visible = true;
}
}
private void close_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void restart_Click(object sender, EventArgs e)
{
Application.Restart();
}
//To control the car. //
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
if (car.Left > 0)
car.Left += -8;
}
if (e.KeyCode == Keys.Right)
{
if (car.Right < 350)
car.Left += 8;
}
if (e.KeyCode == Keys.Up)
{
if (gamespeed < 20)
gamespeed++;
}
if (e.KeyCode == Keys.Down)
{
if (gamespeed > 0)
gamespeed--;
}
}
}
}
That’s all for code then run the program using ctrl+F5.
Checkout the video from youtube for more complete procedure and steps to right the code.
Subscribe my youtube channel for more interesting videos and hit a like for the video share to your friends as they can also learn programming.
Hope you enjoyed creating the game.
Like the post and subcribe for more interesting posts..
Thank you
Your s
vinay