2012-09-17

In the last Post we wrote the first interactive javascript application in haskell where a paddle on the bottom of the canvas could be moved via keyboard input.

In this next step we will add ball (a moving circle) that can bounce of the paddle and the walls.

Here is a preview (again, click on the canvas to get input focus). If you are not viewing this blog article on blogspot and the application does not work, try the original article page.

** Note: ** This currently only works if you are viewing this article only (not in the flow of the complete blog). I am working on the problem ...

But first we will need some perquisites. I will utilize Functional Reactive Programming (FRP) using the functions defined here: Purely Functional, Declarative Game Logic Using Reactive Programming. I take the terminus "coroutine" from that blog article. I like to think of a coroutine as "state full function". The output of the coroutine does not only depend on its input but also on the input passed to it in previous calls. So make sure you read and understand that blog article. The resulting code can be found here: Coroutine.hs.

So, let us get started!

Imports and definitions

I follow the source file Pong.hs and therefor start with the imports and some definitions used later in the game.

We will use Arrow Syntax and tell the compiler that we do. Actually UHC does not support Arrow Syntax (yet?), but more about that later.

We import Data.VectorSpace allowing us to use some basic vector operation with tuples of Doubles. Here we only need addition, but if we need more VectorSpace is handy.

The input data will be a series of Keyboard up and down events with corresponding key codes. BallCollision describes a collision of the ball with the wall or the paddle in a certain direction.

The rest is types we need in the game and should be self explaining.

Next we will declare some values defining subtleties of the game.

Again, these should be relatively self explaining. Keycode 37 and 39 correspond to the arrow keys. canvas2 is the name of the canvas defined in the html code of this blog.

Entry point and callbacks

In difference to the last blog article we will not use a javascript function to save and store global objects. Instead the objects will be stored in IORefs which are passed to the callbacks.

So main sets the initilize function to be called then the window is loaded. initilize creates 2 IORefs, one for the main coroutine (which will be defined later) and one for the input stream, which is a list of input events.

The main coroutine is the place where the game logic happens. The output of the main coroutine is the current game state. Because the current main coroutine depends on the previous calls to it, it must be stored between game updates.

onKeyDown and onKeyUp are called when a key is pressed or released and expand the input stream.

update is set to be called every 20 milliseconds with the state and input IORefs passed to it.

Updating and drawing the game sate

Next we will draw the game state (the output of the main coroutine). This is basicly the same as what we did in the last blog article, only that now we also need to draw a circle for the ball.

The draw function should be self explaining. If not, read my last blog articles. Some javascript functions have been added, but they all follow the same principle as in the last blog article.

The update function reads the current main coroutine and input stream. The coroutine is updated and the new game state is obtained by calling the coroutine with the current input stream. Finally the game state is drawn and the new coroutine is saved.

Some helper functions

Before the main game logic a few helper functions are defined.

keyDown takes a keycode and outputs a coroutine indicating at all times if the given key is down given the input stream (The Event type comes from Coroutine.hs). We will need this because the paddle is supposed to be moving as long as an arrow key is pressed.

Note that this is a little different that what we did in the last post. Actually there it only worked because javascript fires continuous "keyDown" events when a key is hold down, but that is a platform dependent behavior and we do not want to rely on it. Also this firing of key down events does not immediately start when a key is pressed. There is a short break. If you go back on that post and try the application, you will note that the paddle does not start moving immediately, but there is a short delay after pressing a key.

rectOverlap tests two rectangles if they overlap (used for collision detection). playerRect and ballRect return the rectangle occupied by the paddle and ball respectively.

The main Coroutine

The main coroutine takes input events as input and outputs the game state. The type synonym MainCoroutineType is introduced for verbosity. Earlier it allowed us to create the IORef for the main coroutine in a more readable way (in my opinion).

The player state is computed with the input events. The collisions of the ball with player and wall solely depend on the previous ball state. ballWallCollisions and ballPlayerCollisions can therefore be pure functions and not coroutines. That is why "colls" is defined in a let expression. The new ballState is calculated using this collisions information.

The construct with "rec" and "delay" is needed because the ball state from the last frame is required. This construct is explained in Purely Functional, Declarative Game Logic Using Reactive Programming.

The Player

The player is moved with the arrow keys without crossing the bounding of the game.

boundedIntegrate is a coroutine defined in Coroutine.hs which integrates the input and clips it to a given range.

The Ball state

Collisions

The ball state needs the collision events as input (see the main coroutine).

Updating the ball state

Using this collisions events the ball is updated by moving and bouncing according to the collision events.

The + operator is defined in the vector space package and adds two vectors (in our case tuples of doubles).

Compiling

That it. Now we need to compile ...

haste

For haste make sure the newest version is installed. Because we use vector-space we need to install it for haste.

First install vector space via cabal:

Now unpack vector-space with cabal, and install AdditiveGroup.jsmod.

That it! Now put Pong.hs, Coroutine.hs, the haste version of JavaScript.hs and the javascript helper functions helpers.js in a directory and compile with

You should receive a file "Pong.js" which can be included in a html file, like this one: haste html

UHC

With UHC it is a little bit more work. UHC does not support arrow syntax, so we must translate the haskell file with arrowp:

I choose the name PongNA.hs for "Pong no arrows". For some reason I also can not get vector space to compile with UHC. Luckily we have not used much of vector space, only the + operator. So edit PongNA.hs and replace the line

with

Now copy Coroutine.hs and JavaScript.hs (the UHC version) into the directory and compile with

The canvas needs to be added to the generated html file, so add

Since we do not need any additional javascript functions, the generated html page should work!

Conclusion

I have little experience with FRP (this blog article is my first attempt to write a FRP application). I would have liked to use Reactive Banana for this, but at present I am unable to compile Reactive Banana with UHC or haste.

According to this Reactive Banana has been compiled with UHC, but in the new version, support for UHC will be dropped.

haste failed to compile Reactive Banana because of missing PrimOps. According to the maintainer of haste, that is a solvable problem and will be fixed in the future.

In the next article, we will add "blocks" that can collide with the ball and disappear to have a breakout like game.


Writing JavaScript games in Haskell by Nathan Hüsken is licensed under a Creative Commons Attribution 3.0 Germany License.

Show more