wiki:Pong/En:Vaihe1
Last modified 4 years ago Last modified on 2014-10-14 09:40:04

Languages:








Pong game, part 1

This is part 1/7 of the Pong tutorial. During this part we will

  • Create a visible ball on the screen

We'll start creating the game from an empty project, which we created using the previous instructions?.

You can also watch a video (in Finnish) explaining this part of the tutorial: part 1 (duration 08:57), part2 (06:08). Remember to watch in HD quality.

After the project has been created, you can try to run the program by pressing the F5-key, selecting Start Debugging from the Debug-menu or clicking the Play-symbol on the toolbar.

source:/trunk/help_symbols/try_to_run.png

When you run the game for the first time, you'll only see a blank screen.

You can exit the game by pressingEsc (or Alt + F4) on your keyboard.


At the end of this part the game will look something like this:

1. Getting started with programming

There are a lot of files in the new project. You can see the project files on the right hand side of Visual Studio. At this stage we're only interested in the file named Pong.cs that will contain code written in the C# programming language. Double click on that file to open it in the editor.

2. Programming code

The code file includes the game's code, which is a description of what the game does. We'll now start to modify this code.

There are several lines of code already, but we'll concentrate on the subroutine called Begin, which looks like this:

public override void Begin()
{
    // TODO: Write your code here

    PhoneBackButton.Listen(ConfirmExit, "Exit game");
    Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit game");
}

The Begin subroutine is the first thing that is executed when the game is started. As such, it's the appropriate spot to initialize the game, that is to say create the in-game objects and put them in the right places.

Find Begin in the code. The name of the subroutine (Begin) can be found at the end of the first line. After the name, there are parentheses ´(´ and ´)´.

After the name and the parentheses there are the curly brackets ´{´ and ´}´.

The lines of code that are a part of the subroutine are written in between the curly brackets.

The line starting with // is a comment. It doesn't do anything with regards to the game; instead it's only there to remind to programmer of the game (you!) that the game is initialized here. You can remove that line if you want to.

There are two lines after the comment line that create an exit button in the game. Because we're not programming for a phone right now, you can remove the line that starts with PhoneBackButton.

3. Creating an object

First we want to add a ball into our game. For the ball we'll create an object of the type PhysicsObject. We use physics objects because we're making a physics game and we want the objects to behave according to the laws of physics, i.e. bounce, collide etc. The physics object has attributes like real-life objects do, such as shape or mass.

Put the following code into the Begin subroutine to create a new physics object:

PhysicsObject ball = new PhysicsObject(40.0, 40.0);

A new object of the type PhysicsObject is created and stored in the variable called ball.

  • First we tell the system that we're using a variable, whose type is PhysicsObject and name ball.
  • We set the value of ball to be a new PhysicsObject object, which is created by a subroutine of the same name.
  • After the name of the subroutine there are parametres, which define the size of the object. The order of the parametres does matter.

The parametres of the physics object are decimal numbers and they're given in the following order: first width, then height. In this case we set both the width and height to be 40.0 (note that one must use a period to separate the decimals). The unit of measurement can be anything, you can think of them as centimetres, for example.

In order for the ball to become a part of the game it must be added to the group of in-game objects.

Write this line after the creation of the ball:

Add(ball);

The command Add adds the object it is given to the game.

Now your Begin subroutine should look like this:

public override void Begin()
{
    PhysicsObject ball = new PhysicsObject(40.0, 40.0);
    Add(ball);

    Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit game");
}


Remember to save your file every few minutes. You can save by either selecting ''Save Pong.cs'' from the ''File' menu or by using the keyboard shortcut Ctrl + S.

Visual Studio, however, also saves your work every time you run the game with the F5 button.

See if the game works!

source:/trunk/help_symbols/try_to_run.png

4. Making the object into a ball

Our ball doesn't yet look like one. Luckily PhysicsObjects have an attribute called Shape, so we'll want to change the shape attribute of our PhysicsObject ball into a circle. After an object has been created its shape can be changed in the following way:

ball.Shape = Shape.Circle;

You don't have to write PhysicsObject in front of the ball anymore, because we've already introduced it as being of that type. The shape can only be changed after the object has been created. If we were to do it before then, there wouldn't even be a PhysicsObject called ball.

The diametre of the ball will now be 40.0, which means that its size is determined by the width and height given at its creation.

Add this line in between the creation of the ball and adding it to the game. Now your Begin subroutine should look like this:

public override void Begin()
{
    PhysicsObject ball = new PhysicsObject(40.0, 40.0);
    ball.Shape = Shape.Circle;
    Add(ball);

    Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit game");
}

Note that even though we had a variable called ball it was at first a square. This means that the computer doesn't understand what the names of the variables mean. It generally doesn't care about them as long as we use the names in a way it thinks is correct. The names are, however, important to people who are trying to understand the code. Variables and subroutines that are named well and in a descriptive fashion make it a lot easier for both the writers and readers of the code to undestand it! It wouldn't be very smart to give the ball the name "triangle", "object1" or "sdaydfs".

When you now run the program (by pressing F5), you should be able to see a ball in the middle of the screen, like in the picture at the top of this page.

source:/trunk/help_symbols/try_to_run.png

5. The end result

You can check out the finished code (in Finnish) for this part by clicking on the following link:

https://trac.cc.jyu.fi/projects/npo/browser/trunk/esimerkit/Pong/Vaihe1/Pong.cs