Previous | Next

Making your own macros | Parallelism

In yellowPix.esp we have made one part of a drawing application: the part that lights a cell in yellow when you put down a yellow block. Now we will extend this application into an application that allows you to use the other blocks as well.

Creating a macro

First we need to explain how you can create your own macros. The macros we used thusfar, such as await and drawShape are defined in the tagtiles.esp library. However, it is also often handy to create your own macros, because this allows you to keep your application more readable, and to reuse functionality you created earlier.

In order to keep your the application readable, we will first rewrite it a little:

//Right-click to download this code

import "tagtiles.esp"

main = (
  handleYellow
)

handleYellow = (
  loop (
    await (tagEvent down isYellowBlock);
    drawShape (Cell lastTagCoord) yellow
  )
)
The combination of macros that draws a yellow cell when placing a yellow object has been isolated into a new macro that we have called handleYellow. The macro handleYellow does not require any parameters. If you want to, you can give the macro any name you want by changing the word handleYellow before the = sign. We have also redefined the bit after main = into (handleYellow) which means that when the application is started, it will start the handleYellow macro.

It is important to note that, although we wrote it down in a different way, the application does still the same as before.

Exercise

  • Add a macro handleBlue to yellowPix2.esp that deals with the blue block.
  • When the application is started, we want it to react on both the blue and yellow object simultaniously. To achieve this, rewrite the main macro as follows:
    main = (handleYellow | handleBlue)

Parallelism

The vertical line |, also known as a pipe, indicates that both macros are working simultaneously. This means that the application will reacts to the yellow and blue block simultaneously, because both macros are working in parallel.

Now you know how to make your own macros and to put them in parallel, it is time to finish the drawing application.

Exercise

Add the macros handleRed and handleGreen. When each macro has been defined, put them in parallel in the main macro.

A complete drawing application

Below you can see what the complete application looks like.

//Right-click to download this code

import "tagtiles.esp"

main = (
  handleYellow | handleBlue | handleRed | handleGreen
)

handleYellow = (
  loop (
    await (tagEvent down isYellowBlock);
    drawShape (Cell lastTagCoord) yellow
  )
)

handleBlue = (
  loop (
    await (tagEvent down isBlueBlock);
    drawShape (Cell lastTagCoord) blue
  )
)


handleRed = (
  loop (
    await (tagEvent down isRedBlock);
    drawShape (Cell lastTagCoord) red
  )
)


handleGreen = (
  loop (
    await (tagEvent down isGreenBlock);
    drawShape (Cell lastTagCoord) green
  )
)

Let’s recap what we have done in this application. First, we import the tagtiles.esp library which indicates the macros we are using for controlling the TagTiles board.
In the main macro we put the macros we want to run when the application is started. In this case, it runs in parallel the four macros defined below, that enable the player to draw with the colored blocks.


Previous | Next

Serious Toys logo -  tangible inspiration without limitation