The requirements for this tutorial are:
- Visual Studio 2008/2010 [Full or C# Express]
This tutorial takes into account that you know how to do the following already:
- How to declare variables
- How to use If Statements
We are going to use the following libarys:
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;
Now we have the requirements sorted, Lets get started.
First off we're going to need three rectangles so, lets make our rectangle's variables to do this we will type the following code:
public partial class Form1 : Form { private Rectangle Goal = new Rectangle(350, 600, 50, 50); private Rectangle Player = new Rectangle(350, 0, 50, 50); private Rectangle Enemy1 = new Rectangle(0, 150, 75, 75); private Rectangle Enemy2 = new Rectangle(599, 350, 75, 75); public Form1() { InitializeComponent(); } }
Now then, lets break that down. We are making our variables for the rectangles, The format for the four numbers you see after the new Rectangle( ); peice it works like this: new Rectangle(X,Y,Width,Height) it is good to know this, if you want to change the starting locations of any of them.
Now we've declared our rectangles, We need to paint them on our form. Create a paint event for the form so that we can paint them.
There are many ways to paint rectangles but for this tutorial we will use a single line for each rectangle.
private void Form1_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawRectangle(Pens.Red, Goal); e.Graphics.DrawRectangle(Pens.Blue, Player); e.Graphics.DrawRectangle(Pens.Red, Enemy1); e.Graphics.DrawRectangle(Pens.Red, Enemy2); }
What this part does is it draws the rectangles on the form, We only need two parts to use the method and they are the Pen colour which will be the border colour of the rectangles and the name of the rectangle we want to draw.
Later on we'll need to limit the form size, so lets set it to 750x750 and limit it.
public Form1() { InitializeComponent(); this.MaximumSize = new Size(750, 750); this.MinimumSize = new Size(750, 750); }
Ok, So we have our rectangles but they don't really do much at the moment, So lets make a way to control our rectangle which is called Player.
To do this we will need to make a KeyDown event on the form, Do this and go to the code.
In order for us to move the rectangle we're going to need to be able to change the location so we need two more variables, one for the X position and one for the Y position.
private void Form1_KeyDown(object sender, KeyEventArgs e) { int PlayerX = Player.Location.X; int PlayerY = Player.Location.Y; }
Ok, Now we're going to make a switch, So we can determine what the program should do when a certain key is pressed.
private void Form1_KeyDown(object sender, KeyEventArgs e) { int PlayerX = Player.Location.X; int PlayerY = Player.Location.Y; switch (e.KeyData) { case Keys.Up: Player.Location = new Point(PlayerX += 0, PlayerY -= 20); this.Refresh(); break; case Keys.Down: Player.Location = new Point(PlayerX += 0, PlayerY += 20); this.Refresh(); break; case Keys.Left: Player.Location = new Point(PlayerX -= 20, PlayerY += 0); this.Refresh(); break; case Keys.Right: Player.Location = new Point(PlayerX += 20, PlayerY += 0); this.Refresh(); break; } }
Ok, So for the switch we want it to pick up the event key data so we put e.KeyData, the e stands for event and the key data is what key is pushed.
You can see that for each case they are very similar, The Keys.Up part means if the up key is pressed the switch will perform that peice of code.
The next important part is the peices inside the case statements. What we're doing is moving the Players location by 20 pixels each time, so if we want to move it up we want to take away 20 pixels from it's current y location so it goes up! simple

We need to refresh so that the rectangle will move on the GUI also.
Right, We have a user controlled rectangle now, lets make a custom method to detect if any rectangles hit eachother.
public void HitDetect() { if (Player.IntersectsWith(Goal)) { MessageBox.Show("You Win!"); } int PlayerX = Player.Location.X; int PlayerY = Player.Location.Y; if (Enemy1.IntersectsWith(Player)) { Player.Location = new Point(PlayerX = 350, PlayerY = 0); MessageBox.Show("You lose!"); } if (Enemy2.IntersectsWith(Player)) { Player.Location = new Point(PlayerX = 350, PlayerY = 0); MessageBox.Show("You lose!"); } }
Ok what we've done is made an if statement to detect if the player's rectangle intersects with the any other rectangle, when it does it show a message box. But, if the player hits an enemy it will move it back to the beginning.
Now we've made our method, let's call it after the switch statement, To do this we simply type HitDetect();
I mean, This is all well and good but what's the point in moving without anything else moving, it's too easy! so lets make our enemies move around a bit.
In order to do this add a timer to your form, set it to enabled and make the event for the tick.
Because above we have set the form to 750 by 750 so we know the boundries of the form.
As when we were moving the player we need variables for the two enemies X and Y location.
private void timer1_Tick(object sender, EventArgs e) { int EX1 = Enemy1.Location.X; int EY1 = Enemy1.Location.Y; if (Enemy1.Location.X > 600) { Enemy1.Location = new Point(EX1 = 0, EY1 = 150); } Enemy1.Location = new Point(EX1 += 30, EY1 += 0); this.Refresh(); }
Ok, The important part of this is the if statement and the line underneath it. The if statement detects if the X location is greater than 600 and if it is, it will reset the enemy back to the other side of the form. The code outside the if statement will keep it moving at 30 pixels per tick of our timer.
Just to make the game a little bit harder, we'll make the other enemy go the other way so we need to do the following code;
int EX2 = Enemy2.Location.X; int EY2 = Enemy2.Location.Y; if (Enemy2.Location.X < 0) { Enemy2.Location = new Point(EX2 = 599, EY2 = 350); } Enemy2.Location = new Point(EX2 -= 30, EY2 += 0); this.Refresh();
Notice because we want it to move the other way, we -30 pixels on each tick.
Ok well, We have those moving but the player can cheat! it can go outside the form, Lets fix that by limiting it in the same way.
Below where we called our HitDetect method we'll put
if (Player.Location.X > 650) { Player.Location = new Point(X -= 20, Y += 0); } if (Player.Location.X < 0) { Player.Location = new Point(PlayerX += 20, PlayerY += 0); } if (Player.Location.Y > 590) { Player.Location = new Point(PlayerX += 0, PlayerY -= 20); } if (Player.Location.Y < 1) { Player.Location = new Point(PlayerX += 0, PlayerY += 20); }
We have four so that we can't go outside any part of the form, to prevent any cheating. But this time, when we hit the border it will move the player back by the value it is trying to move forward so we can't get past the border!
and that's it, You have a working game

Here is the full program listing, Remeber I put my timer in using the designer mode, not in code so make sure you either make a coded one or implement one from the designer.
public partial class Form1 : Form { private Rectangle Goal = new Rectangle(350, 600, 50, 50); private Rectangle Player = new Rectangle(350, 0, 50, 50); private Rectangle Enemy1 = new Rectangle(0, 150, 75, 75); private Rectangle Enemy2 = new Rectangle(599, 350, 75, 75); public Form1() { InitializeComponent(); this.MaximumSize = new Size(750, 750); this.MinimumSize = new Size(750, 750); } private void Form1_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawRectangle(Pens.Red, Goal); e.Graphics.DrawRectangle(Pens.Blue, Player); e.Graphics.DrawRectangle(Pens.Red, Enemy1); e.Graphics.DrawRectangle(Pens.Red, Enemy2); } private void Form1_KeyDown(object sender, KeyEventArgs e) { int PlayerX = Player.Location.X; int PlayerY = Player.Location.Y; switch (e.KeyData) { case Keys.Up: Player.Location = new Point(PlayerX += 0, PlayerY -= 20); this.Refresh(); break; case Keys.Down: Player.Location = new Point(PlayerX += 0, PlayerY += 20); this.Refresh(); break; case Keys.Left: Player.Location = new Point(PlayerX -= 20, PlayerY += 0); this.Refresh(); break; case Keys.Right: Player.Location = new Point(PlayerX += 20, PlayerY += 0); this.Refresh(); break; } if (Player.Location.X > 650) { Player.Location = new Point(PlayerX -= 20, PlayerY += 0); } if (Player.Location.X < 0) { Player.Location = new Point(PlayerX += 20, PlayerY += 0); } if (Player.Location.Y > 590) { Player.Location = new Point(PlayerX += 0, PlayerY -= 20); } if (Player.Location.Y < 1) { Player.Location = new Point(PlayerX += 0, PlayerY += 20); } HitDetect(); } public void HitDetect() { if (Player.IntersectsWith(Goal)) { MessageBox.Show("You Win!"); } int PlayerX = Player.Location.X; int PlayerY = Player.Location.Y; if (Enemy1.IntersectsWith(Player)) { Player.Location = new Point(PlayerX = 350, PlayerY = 0); MessageBox.Show("You lose!"); } if (Enemy2.IntersectsWith(Player)) { Player.Location = new Point(PlayerX = 350, PlayerY = 0); MessageBox.Show("You lose!"); } } private void timer1_Tick(object sender, EventArgs e) { int EX1 = Enemy1.Location.X; int EY1 = Enemy1.Location.Y; if (Enemy1.Location.X > 600) { Enemy1.Location = new Point(EX1 = 0, EY1 = 150); } Enemy1.Location = new Point(EX1 += 30, EY1 += 0); this.Refresh(); int EX2 = Enemy2.Location.X; int EY2 = Enemy2.Location.Y; if (Enemy2.Location.X < 0) { Enemy2.Location = new Point(EX2 = 599, EY2 = 350); } Enemy2.Location = new Point(EX2 -= 30, EY2 += 0); this.Refresh(); } }
Tasks for you to do:
- Add a Label that if you get hit by the enemies you lose a point, but if you hit the goal you gain a point
- Try to make it so the form does not flicker as much
Author notes:
Why did you make this: I made this so that beginners to programming can make a rewarding game whilst learning more about programming and how it works.
You did it in a bad way: I am aware this is not the best of ways to do this program, But please remeber this is for beginners, Your welcome to post your opinions but be gentle!
Please enjoy this tutorial, I hope you learnt something new, with several functions mentioned you could make a much better windows form game, The best way to learn is to play around with things good luck and Happy Programming!