Welcome to my second tutorial, This one is about a basic C# Windows Form game, Though this is not conventionally used, It is a nice way for beginners to make a rewarding program while learning basic C# Programming.

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:

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.

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.

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.

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.

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;

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.

- 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!

Thanx! This was a really easy and understandable little game. I hope u don't mind but I added a few tweaks.

This might be a little confusing but DON'T type "private void Form1_Paint(object sender, PaintEventArgs e) .....". In the design view, select your form and in the properties box click on the little thunderbolt to add an event, then double click on paint. Now copy the code inside the { }.

I changed some thing on the Form1_KeyDown method, it just worked better for me but the original code works too.
The if statement that check if your moving off the screen is part of the switch statement in mine.

One more thing I did is to call the HitDetect(); after the last this.refresh(); in the timer1_Tick method. When u move into a enemy's path and stop moving the enemy will pass through you without detecting the hit, the previous fix... well fixes this problem.

One more thing I did is to call the HitDetect(); after the last this.refresh(); in the timer1_Tick method. When u move into a enemy's path and stop moving the enemy will pass through you without detecting the hit, the previous fix... well fixes this problem.

If you use this you will have to add "Player.Location = new Point(350, 0);" to the HitDetect(); method.

I'd like to issue an apology for my lack of support and activity for this tutorial. I wrote this tutorial as I was still new so there are issues with it.

In terms of the lack of support I have had some real life issues so I needed to keep stress to a minimum.

I am truely sorry, but I am glad some of you have been able to learn from what is essentially a broken tutorial. It appears to give guidance on the code structure and leaves it to you to do what programming is all about, problem solving.

I will do my best to help in the future and look forward to becoming more active on here.

hey! thanks alot for this tutorial! learned alot although i can't seem to figure out how to make you "respawn" when you hit the corners, neither how you find out if the player hit the corners, can you please help me on this?
This is my current code:

Nice tutorial and worked perfectly but then I changed to reconize Left, Right, Up and Down but KeyDown event is not being generadted for this keys.. What can be wrong? (I did put a ShowMessage() to test if the event was generated but the message do not display with cursor keys, but with other keys all work fine. KeyPreview property is true...