Using Flowcode and Eblocks to create Arcade Games – Pong

One of the best ways to learn programming is to create a game. It forces your hand to try and keep things fast and efficient but also when your finished it gives you something entertaining you can play and share with your friends.

20150922_102925

The Matrix EBM001 Graphical Colour LCD board provides the user with a screen consisting of 160 x 128 individually addressable pixels, each of which can be set to any one of 65536 colours.

Communications with the display requires only five signal lines with an extra signal to control the display back light if needed. The availability of colour and graphical capabilities allows system data to be represented in a wide range of formats, including simple animations. As with most technologies, one of the best ways to demonstrate the capabilities is with a game. This example is a colour version of one, the simple 2-player bat & ball style games from the 1970s.

This program demonstrates the use of the Flowcode gLCD component line, box, text, and colour control functions to draw the playing surface, animate the bats and ball, and display the scores.

The game currently uses a 5 x 5 pixel resolution for the movement of the bats and ball. The trajectory of the ball is calculated using simple, straight-line geometry and integer maths. The horizontal velocity of the ball remains constant (1 block per cycle) and the trajectory is controlled by adjusting the vertical velocity.

At the start of each game the ball is launched towards the red bat after a 2 second delay. The ball will rebound off the edges of the playing area and the bats. The edges are flat surfaces but the bats act like convex surfaces and will change the rebound angle unless hit in the middle. A point is scored if the ball is at either end of the playing area and the appropriate bat is not correctly positioned to rebound it into play. The microcontroller’s decision is final!

20150922_103000

The program is set up with the EBM001 connected to Port D and the control switches connected to Port B. This allows the game to be run on most target hardware, including the EB006, EB091 , ECIO and EB093 Arduino Shield for those familiar with Arduino programming.

Blue bat: RB7 = UP / RB6 = DOWN
Red bat: RB1 = UP / RB0 = DOWN

Any of the port settings can be changed if other system configurations are wanted.

The bats can also be controlled in the simulation by using the keyboard. Note that you cannot simply hold the key down to move as you can with the switches, you need to keep pressing the key for each movement.

Blue bat: A = UP / Z = DOWN
Red bat: K = UP / M= DOWN

BatSim

The code uses approximately 40% of the available program and data memory of a PIC16F1937, and included a 100ms delay in each cycle of the program, so there are plenty of resources available to improve the game play. The 16F1937 can also be clocked at rates up to 32MHz which should also provide some extra processing power if needed.

Suggested improvements:

•  Split the control buttons onto two ports and provide each player with an EB-007 Switches Board to use as a controller.
•  Randomise the side and trajectory of the ball launch at the start of the game.
•  Randomise the trajectory of the ball and select the appropriate launch side after each point is scored.
•  Improve the resolution of the ball motion (it currently uses the same 5 x 5 pixel resolution as the bats) and maybe add a motion blur trail effect.
•  Program the microcontroller to control one of the bats for a single player game.

There are a large number of alternative games that could be developed using the EBM001. Strategy games, making even more use of the colour and graphic capabilities, would be particularly suitable. The EB091 and ECIO40P16 are especially suitable platforms for games creation due to their high speed and 16-bit architecture.

BatGame Source Code

6,405 total views, 1 views today

Leave a Reply