How To Develop A Simple Game Using C#
Today I am going to show some simple codes to develop a simple game.
It is an egg catching game. so we can call it as an “EGG Catch” (Try to give an interesting name ).
This game was developed in windows forms. Firstly, Complete the primary things.
· Open Visual studio
· Select new project
· Go Through
o Visual c# àWidows à Windows Form Application
· Name Your Project
Resize Your Form , Name it and Change Some Properties (*Only If You Really Need It).
Ok, Then we can prepare some images using Paint.
![]() |
| Basket |
![]() |
| Egg |
| Hens |
![]() |
| Wall |
Ha ha ! I am not so good in drawing, so you can draw some good pictures. Anyway I am using it.
Design your form like this and place the picture boxes , timers , labels like this. And select appropriate image of the picture box .
![]() |
| Design Like this.control name and its names are given. |
You can draw beautiful images and place it.
Now we can code it .
public partial class FrmEggCatch : Form
{
public FrmEggCatch()
{
InitializeComponent();
}
bool boolBasket = false;
int catches = 0;
int missed = 0;
Stopwatch sw = new Stopwatch();
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Left)
{
if (pbBasket.Left<pbWall.Left) boolBasket = false; else boolBasket = true;
if (boolBasket)
{
pbBasket.Left -= 10;
return true;
}
}
else if (keyData == Keys.Right)
{
if (pbBasket.Right > this.Width) boolBasket = false; else boolBasket = true;
if (boolBasket)
{
pbBasket.Left += 10;
return true;
}
}
else if (keyData == Keys.Escape)
{
//To escape from the game . if we press an escape key we will go out
this.Close();
}
return base.ProcessCmdKey(ref msg, keyData);
}
public void score()
{
//This function is to show scores and misses in score board
lblPoints.Text = $"Score :{catches}";
lblMissed.Text = $"Missed:{missed}";
}
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan ts = sw.Elapsed;
lblTime.Text = $"Time :{ts.Minutes}-{ts.Seconds}";
if (missed == 10) GameOver();
pbE1.Top += 8;
if(pbE1.Top+pbE1.Height>pbBasket.Top+10)
{
if(pbE1.Left>pbBasket.Left&&pbE1.Right<pbBasket.Left+pbBasket.Width)
{
//if egg1 Caught
pbE1.Location = new Point(83,68);
catches+=3;
score();
}
}
if(pbE1.Bottom>pbWall.Top)
{
pbE1.Location = new Point(83, 68);
missed++;
score();
}
}
private void GameOver()
{
//In The End Of this Game it will work
timer1.Stop();
timer2.Stop();
timer3.Stop();
lblEnd.Text = @"Game Is Over
Developed By sakkeer outlawz";
}
private void timer2_Tick(object sender, EventArgs e)
{
pbE2.Top += 15;
if (pbE2.Top + pbE2.Height > pbBasket.Top + 10)
{
if (pbE2.Left > pbBasket.Left && pbE2.Right < pbBasket.Left + pbBasket.Width)
{
pbE2.Location = new Point(341, 83);
catches+=5; score();
}
}
if (pbE2.Bottom > pbWall.Top)
{
pbE2.Location = new Point(341, 83);
missed++;score();
}
}
private void timer3_Tick(object sender, EventArgs e)
{
pbE3.Top += 5;
if (pbE3.Top + pbE3.Height > pbBasket.Top + 10)
{
if (pbE3.Left > pbBasket.Left && pbE3.Right < pbBasket.Left + pbBasket.Width)
{
pbE3.Location = new Point(639, 73);
catches+=2; score();
}
}
if (pbE3.Bottom > pbWall.Top)
{
pbE3.Location = new Point(639, 73);
missed++; score();
}
}
private void FrmEggCatch_Load(object sender, EventArgs e)
{
sw.Start();
}
}
Do not copy this code and design. try to understand and try to make your own changes
some suggested changes are given
- you can use good images
- you can change background image.
- you can replace golden eggs some time and you can give extra points.
- also try some shits that reduce points.
I am waiting for your reply....................!






Comments
Post a Comment