You are on page 1of 4

Contra Game in C# Visual Studio

By Rohit Programming Zone

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

namespace Contra_Game_By_Rohit_Programming_Zone
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
game_lbl.Visible = false;

class Gun
{
public string direction;
public int speed=20;
PictureBox Bullet = new PictureBox();
Timer Bullet_Movement = new Timer();
public int bullet_Left;
public int bullet_Top;

public void Get_Bullet(Form Properties)


{
Bullet.BackColor = System.Drawing.Color.Aqua;
Bullet.Size = new Size(7, 7);
Bullet.Tag = "bullet";
Bullet.Left = bullet_Left;
Bullet.Top = bullet_Top;
Bullet.BringToFront();
Properties.Controls.Add(Bullet);
Bullet_Movement.Interval = speed;
Bullet_Movement.Tick+=new EventHandler(Bullet_Movement_Tick);
Bullet_Movement.Start();
}

private void Bullet_Movement_Tick(object sender, EventArgs e)


{
if (Bullet.Left < 0 || Bullet.Left > 850 || Bullet.Top > 500)
{
Bullet_Movement.Stop();
Bullet_Movement.Dispose();
Bullet.Dispose();
Bullet_Movement = null;
Bullet = null;

}
else if (direction =="left")
{
Bullet.Left -= speed;
}
else if (direction == "right")
{
Bullet.Left += speed;
}
else if (direction == "up")
{
Bullet.Top -= speed;
}

}
}

string target;
private void firing(string location)
{
Gun Bullet = new Gun();
Bullet.direction = target;
Bullet.bullet_Left = player.Left + (player.Width / 2);
Bullet.bullet_Top = player.Top + (player.Height / 2);
Bullet.Get_Bullet(this);
}

private void Form1_KeyDown(object sender, KeyEventArgs e)


{
if(e.KeyCode==Keys.Right)
{
target = "right";
player.Left += 5;
player.Image = Properties.Resources.Right_img;
}
if (e.KeyCode == Keys.Left)
{
target = "left";
player.Left -= 5;
player.Image = Properties.Resources.Left_img;
}
if (e.KeyCode == Keys.Up)
{
target = "up";
player.Image = Properties.Resources.Up_img;
}
}

public void alians(int Move)


{
Random rnd = new Random();
int x, y;
if(alien_1.Top>=500)
{
x = rnd.Next(0, 800);
alien_1.Location = new Point(x, 0);

}
else
{
alien_1.Top += Move;
}
if (alien_2.Top >= 500)
{
y = rnd.Next(0, 800);
alien_2.Location = new Point(y, 0);

}
else
{
alien_2.Top += Move;
}
if (alien_3.Top >= 500)
{
x = rnd.Next(0, 800);
alien_3.Location = new Point(x, 0);

}
else
{
alien_3.Top += Move;
}
if (alien_4.Top >= 500)
{
y = rnd.Next(0, 800);
alien_4.Location = new Point(y, 0);

}
else
{
alien_4.Top += Move;
}
if (alien_5.Top >= 500)
{
y = rnd.Next(0, 800);
alien_5.Location = new Point(y, 0);

}
else
{
alien_5.Top += Move;
}

public void Game_Over()


{
if(player.Bounds.IntersectsWith(alien_1.Bounds))
{
game_lbl.Visible = true;
player.Image = Properties.Resources.Dead_img;
timer1.Stop();
}
if (player.Bounds.IntersectsWith(alien_2.Bounds))
{
game_lbl.Visible = true;
player.Image = Properties.Resources.Dead_img;
timer1.Stop();
}
}
int score = 0;
private void timer1_Tick(object sender, EventArgs e)
{

alians(5);
Game_Over();
foreach(Control a in this.Controls)
{
foreach(Control b in this.Controls)
{
if(a is PictureBox && a.Tag=="alien")
{
if(b is PictureBox && b.Tag=="bullet")
{
if(a.Bounds.IntersectsWith(b.Bounds))
{
this.Controls.Remove(a);
a.Dispose();
this.Controls.Remove(b);
b.Dispose();
score += 20;
Score_lbl.Text = "Score - " + score;
if(score==100)
{
game_lbl.Text = "Level Cleared..";
game_lbl.Visible = true;
timer1.Stop();
}
}
}
}
}
}

private void Form1_KeyUp(object sender, KeyEventArgs e)


{
firing(target);
}
}
}

More Tutorial on http://rohitprogrammingzone.blogspot.com

You might also like