Table football (game tutorial)

I will show you how to create a very well known table football game. Surely each of you at the moment of boredom took advantage of the opportunity to play in such a simple and nice game. I also made a game with show how you can use code below to create playable game. Game presented below is not an advertisement of me. Game below show how you can use code, this tutorial have to show only game engine, hearth of the game. If you have any question, like “why my panel glows and panel in your game not” or “how did you do ….” just ask i will answer you. When i prepare tutorial, i always want to create oportunity for you to test skills, give you challenge. Try to create your own game based on this code and you will see your skills will increase your skills.

pilkarzykiico This is my game i create using the code

Using the code

Game form will not have too many elements, because they will be generated automatically during the game:

pilkarzyki1
Element type Element name Settings
Form Form1 Name: Form1

Text: Table football

Size: 654; 468

Panel Panel1 Name: Panel1

Size: 613; 407

Location: 12; 12

BackColor: SeaGreen

Anchor: Top, Bottom, Left, Right

PictureBox Main_panel Name: Main_panel

Size: 100; 50

Location: 58; 0

BackColor: SeaGreen

As I wrote earlier, all items will be generated in live, so we only have three items. The main element will be PictureBox, where we will be played the game, and the panel under it is just aesthetic background.

First, set the PictureBox’s size and centered his position, then generate the graphics and clickable elements on the board. We will start with the grid generation of the playground, unlock the element:

Pitch elements we will draw using:

This method code  looks like this:

This is a clean drawing, you can type the code, then fired the program and watched where it appears :]. Once activated you can already see the boxes as on a checkered sheet:

pilkarzyki2

Now it’s a bit like table football. You can experiment and change the colors, add some circles to the pitch to make this view more enjoyable.
Now we add the active elements of the board to do it, we have to divide it a little:
– the panels at the goal (three that allow the shot to the goal (in yellow))
– the sideline panels (those at the edge of the field (in green))
If you add colors to your’s panels I got the following effect:

pilkarzyki3

LocationList  will keep all the panels on the pitch, but they coud be in special lists with their own special qualities like sideline special bounced element.
The code for this element is shown below:

You can now activate this method in Form_Load:

This will mark the center panel of the field and add a ball to the game (black dot).

pilkarzyki4

The goal panels will be added separately because of the lock that we create, if we are close enough to the goal, the field in the goal will be activated and then we will be able to score a goal. Unlock in Form_Load

The code that will add the goal panels to the pitch:

Goals are those highlighted yellow panels:

pilkarzyki5

We have goals, we have the field, we have the ball so time to start the game.

Unlock in GenerateMainPanels() and Goal_Click() method:

pan_Click would looks like this:

Method drawing lines is quite complicated:

First, we generate graphics and block the panel where we are currently located. Then we add the graphics of the pitch. We check that the player is near the goal and we unlock corresponding goal. The next stage is a special location that has additional restrictions (you can not move along the lines of the sideline, but you can bounce off the line (useful when we split the game into two players)). The next step is to add the line we have drawn to the list and block already selected panels (lines drawn), adding already drawn lines to the graphic and the current line. At the very end we add the ball and change the graphic of the pitch to the current one. Effect:

pilkarzyki6

The game does not yet have its limitations, which causes many bugs, one of them is the availability of all the panels at pitch, so the lines can be made as you like. Our restriction sits in the method UnlockPanel(), so unlock it:

It limits the range of available fields, reduces the blinking of the board, and allows you to put in rules of the game.

As you have noticed, the UnlockPanel method is before the draw method, because the first method blocks all panels and unlocks all the +1 and -1 panels, and then the draw method puts restrictions. Two combined methods make game  playeble :]

pilkarzyki7

In this game you can play with a friend or go one step further and create an AI that will play with you. Everything is available on this step so you can easily add new things to game. I leave it you to do, if you have any trouble, just write email or leave a comment.

Permanent link to this article: https://visualmonsters.cba.pl/en/table-football-tutorial/

Leave a Reply

Your email address will not be published.