dark mode light mode Search Menu

An Apple a Day…

The fearless starship crew has been in space for months. Forced to subsist on nothing but bread and Plutonian rock slime, they have begun to suffer from a serious Vitamin C deficiency. Fortunately they make it to the Apple Nebula, where nature’s bounty awaits. Gather as many apples as you can before the time is up!

In recent issues, we covered how to install a virtual computer called Mini Micro and run the demos. (You can always find it at: https://miniscript.org/MiniMicro) This time let’s create a real game you can play with the keyboard or a game controller!

Launch Mini Micro, use the edit command to open the code editor, and then carefully type in Listing 1. Click the Close & Run button at the top of the editor, or exit the editor and type run at the prompt, to give it a try. If you get any errors, edit again and double-check your typing, being sure to match the capitalization and punctuation shown. If all is working correctly, you should be able to fly your spaceship around the bottom of the screen, collecting apples that fall from the top. How many can you catch in 30 seconds?

This program illustrates some very common game elements. The objects ship and target are “sprites” — little pictures that can be efficiently moved around the screen. These are set up near the top of the program, in lines 3-10. Lines 12-14 load a sound, clear the score, and define the game end time.

Next comes the main loop (lines 17-36). The main loop is the part of the program that executes over and over, about 60 times every second. This prints the current time and score at the top of the screen, and then moves the ship using the key.axis method. This is a great way to move a sprite because it works with the arrow keys, or the WASD keys, or even a gamepad or joystick.

Line 25 checks whether the ship and target (apple) are close enough to catch. And finally, the target is moved on line 31. If it goes off the bottom of the screen, the if block on lines 32-35 will move it back up to the top.

Want to take this program further? Try giving a little extra time whenever an apple is caught (hint: you’ll need to add to the endTime variable). Or try changing the score so that instead of 1 point per apple, you get more points for catching it higher up (hint: ship.y is the ship’s distance from the bottom of the screen). Or, copy the code from /sys/demo/stars.ms and insert it before line 3, to give the game a colorful starry background. Happy coding!

Figure 1.

Learn More

Make a game in Mini Micro


Mini Micro Coding Introduction


Video Tutorial