C Sharp Platform Game in Visual Studio PDF
C Sharp Platform Game in Visual Studio PDF
start a new project, Choose windows form application in C# and name it platformgame
In the properties window change the setting to Size - 500, 650 and Text to Platform Game
Lets add the second picture box to form this time we are going to add the player.
__________________________________________________________________________________________________
More tutorials on www.mooict.com
Change the name property to player and back colour to blue.
Click on the from and go to the events panel in the properties window
__________________________________________________________________________________________________
More tutorials on www.mooict.com
Find the key down - Type keyisdown and press enter
Both of these actions will take you to the code view. We need to do one more thing before we can start coding for this
game.
__________________________________________________________________________________________________
More tutorials on www.mooict.com
These are variables we need for this game. This is what is looks like in visual studio.
Notice we entered the code above the public
Go left and go right and jumping are Booleans which form() line.
will determine the direction of the player.
This is where it should be.
Integer jumpSpeed is holding the value 10.
Integer force is holding the value 8.
Integer score is zero.
private void keyisdown(object sender, KeyEventArgs e) This is the key is down function. This will
{ trigger each time the buttons are pressed.
if (e.KeyCode == Keys.Left)
{
goleft = true; When the left button is pressed we can go left
} to true.
if (e.KeyCode == Keys.Right) When the right button is pressed we can
{ change the go right to true.
goright = true;
} When the space bar is pressed AND the
if (e.KeyCode == Keys.Space && !jumping) character is not jumping we change the
{ jumping to true.
jumping = true;
}
}
key is up function
private void keyisup(object sender, KeyEventArgs e) This function will turn all of Booleans back to
{ false when the users releases each key for
if (e.KeyCode == Keys.Left)
{
example when the LEFT is up we change go
goleft = false; left back to false. Same for right and SPACE
} bar.
if (e.KeyCode == Keys.Right)
{
goright = false;
}
if (jumping)
{
jumping = false;
}
}
Below are the first part of the timer event codes, enter them inside the function.
__________________________________________________________________________________________________
More tutorials on www.mooict.com
player.Left -= 5; This line above is checking if the player is
} jumping and force of the jump is less than 0 if
if(goright)
so then we can change jump back to false.
{
player.Left += 5; if(goleft)
} {
player.Left -= 5;
if (jumping) }
{ If go left is true then we can push the
jumpSpeed = -12; character towards left of the screen.
force -= 1;
}
else
{ if(goright)
jumpSpeed = 12; {
} player.Left += 5;
}
if (jumping)
{
jumpSpeed = -12;
force -= 1;
}
If jumping is true then we change the jump
speed integer to minus 12 which means it will
thrust the player towards the top and we
decrease the force by 1. If we don't do this
then character can fly over everything we
need to give the jump a limit.
else
{
jumpSpeed = 12;
}
ELSE the character is not jumping then we can
keep the jump speed on 12.
foreach (Control x in this.Controls) In this piece of code we will scan the whole
{ form to find the picture boxes with this once
if (x is PictureBox && x.Tag == "platform")
{
our player interacts it will have to land on top
if (player.Bounds.IntersectsWith(x.Bounds) && !jumping) of it.
{
force = 8; For each control x in this.Controls means for
player.Top = x.Top - player.Height; each of the windows component we create a
}
} variable called X and give it a type of controls.
}
IF X is a type of picturebox AND tag is equals
to "platform"
__________________________________________________________________________________________________
More tutorials on www.mooict.com
Try the game out now.
Move left
Move right
Now off course we can't just have a single platform on a platform game.
You can choose any back colour you want. For this example we will use the BROWN one.
Note make sure you change the TAG for each of the picture boxes to platform.
__________________________________________________________________________________________________
More tutorials on www.mooict.com
Run the game and check.
It worked.
We need to have a way for the player to win or lose the game in case you want to go further with this.
name it door.
Lets add a small code so when we reach the door we can end the game
__________________________________________________________________________________________________
More tutorials on www.mooict.com
There you go Job done.
It's a simple
You can also do a another picture box which will end the game.
Since all of them are picture boxes we can design the level to make it look more game such see the example below.
You make your own or use some images from the internet and experiment with making your own platform game.
Lets add another picture box to form. Give this one a size of 20, 20 and back colour of yellow. Make sure it has TAG of
coin.
__________________________________________________________________________________________________
More tutorials on www.mooict.com
Now lets add the following code inside the for loop where we have written about the player and platform collision before.
Test Run
It works. Lets copy and paste the coin all over the level now.
__________________________________________________________________________________________________
More tutorials on www.mooict.com
Below are the full code for this game.
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;
namespace platformGame
{
public partial class Form1 : Form
{
bool goleft = false;
bool goright = false;
bool jumping = false;
public Form1()
{
InitializeComponent();
}
if (e.KeyCode == Keys.Right)
{
goright = false;
}
if (jumping)
{
jumping = false;
}
}
if (goleft)
{
player.Left -= 5;
}
if (goright)
{
player.Left += 5;
__________________________________________________________________________________________________
More tutorials on www.mooict.com
}
if (jumping)
{
jumpSpeed = -12;
force -= 1;
}
else
{
jumpSpeed = 12;
}
if (player.Bounds.IntersectsWith(door.Bounds))
{
timer1.Stop();
MessageBox.Show("You WIN");
}
}
}
}
__________________________________________________________________________________________________
More tutorials on www.mooict.com