0% found this document useful (0 votes)
913 views4 pages

Contra Game in C# Source Code by Rohit Programming Zone PDF

This document describes the code for a Contra game clone created in C# Visual Studio. It includes classes for a gun that fires bullets, aliens that move across the screen, methods for player movement and firing, and a timer to control the game loop. The timer ticks handle alien movement, collision detection between bullets and aliens to score points, and detecting collisions between the player and aliens to end the game. The game displays a "Game Over" or "Level Cleared" message depending on if the player dies or scores 100 points.

Uploaded by

BiNt E AAdm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
913 views4 pages

Contra Game in C# Source Code by Rohit Programming Zone PDF

This document describes the code for a Contra game clone created in C# Visual Studio. It includes classes for a gun that fires bullets, aliens that move across the screen, methods for player movement and firing, and a timer to control the game loop. The timer ticks handle alien movement, collision detection between bullets and aliens to score points, and detecting collisions between the player and aliens to end the game. The game displays a "Game Over" or "Level Cleared" message depending on if the player dies or scores 100 points.

Uploaded by

BiNt E AAdm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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