How to Write a Keyboard-Controlled Fly Procedure in MicroWorld EX

By Camille McCue

This may be your first MicroWorld EX project that includes a keyboard-controlled interface. It uses a single key on the keyboard — the up arrow key — to execute the fly procedure by the UFO. The fly procedure lets a player fire the thrusters on the UFO to oppose the pull of gravity from the moon.

On the project Procedures tab (located in the lower-right corner of the window), write a fly procedure as follows:


For a Windows game:

to fly
let [key readchar]
if (ascii :key) = 38 [seth 0 fd 20 wait 1]
For a Mac game:
to fly
let [key readchar]
if (ascii :key) = 30 [seth 0 fd 20 wait 1]

Simply put, the fly procedure reads the character of the key pressed by a player. If the key is the up arrow (ASCII code 38 in Windows, or ASCII code 30 on a Mac), the commands inside the square brackets are executed.

Here, the example UFO has its heading set to north (seth 0) and then it moves in that direction 20 pixels (fd 20) with a wait of 1. You may need to tinker with the code to create the upward thrust you want.

Note that when the UFO turtle is clicked on, its OnClick commands constantly point and move it south. This fly procedure will be put into the Go! button, which you create later in this project.

See Project 12 for a more detailed explanation of how a keyboard-controlled procedure works.

If you plan to distribute your game on both Windows and Mac platforms, you can put both sets of ASCII keyboard commands into the fly procedure so that the game operates everywhere. Just type the if line of code for Windows, followed by the if line of code for the Mac on the next line.