Chapter 2. SuperCargo – Using Events to Track Game Progress

Mobile games and apps bring a new challenge to developers; they are frequently used in open spaces, on the go, while waiting for something, or in other circumstances where the user might be interrupted at any time. Some of the most common examples are as follows:

  • The user's meal (train, or person they're meeting) might arrive, causing them to put their device away
  • The user might be playing on a device with telephone functionality, and receive a call in the middle of the game
  • The user might switch to another app, causing your program to be closed to free up device memory
  • The user's device might run out of battery power

To be popular with users, apps have to "preserve state", that is, remember where they are at each given moment, so that if one of these or any other interruption occurs, they are not frustrated by losing their progress. We'll explore how the event model that we discussed in Project 1, Bat Swat – An Introduction to App Event Cycles, can help us streamline the process of remembering what the user has done so far.

What do we build?

To illustrate these principles, we'll make a clone of the popular puzzle game Sokoban. This game originated in Japan, where the title means warehouse keeper, and was popular among Unix users. Ports exist for many systems, including several mobile versions. The goal is to arrange boxes into target spaces through a confined space, without pushing any box into a corner that you can't get it out of. We'll cover the details in the game design document.

What does it do?

Like Bat Swat, SuperCargo will follow the basic splash screen-game-splash screen cycle of most arcade games. By the time we're done, the splash screen will allow the user to select one of several levels to play through. The game content will load the chosen level and display it using a tiled sprite sheet, then receive game moves from taps on different parts of the screen. The game engine will determine whether these moves are illegal, process the results of legal moves, and display them. The game will also track the number of moves used to progress through a given level and allow users to undo moves, so that they can escape from unwinnable conditions or consider alternate strategies. Finally, the game will remember progress through uncompleted levels, including more than one level at a time, to give the user more flexibility.

Why is it great?

Sokoban has a well-developed set of file formats and a huge body of existing levels, so this game would be easy to expand with in-app purchases or by allowing the user to add their own level files from public sources, so a lot of potential value is possible at no cost. For us as developers, however, the real benefit comes from seeing how easy it can be to preserve game progress by storing user input streams or other game events. The event model that we developed for Bat Swat means that we will actually be able to add saved progress with only the smallest of code changes.

How are we going to do it?

A couple of the tasks from the first project will be condensed to give more focus to the others, and because they consist of more familiar tasks such as:

  • Describing the game
  • Loading a level from a file
  • Displaying the map contents
  • Adding the interface
  • Making the game playable
  • Adding the shell
  • Supporting undo
  • Preserving game history

What do I need to get started?

You'll need two main resources, a levels file that contains text descriptions of the various levels, and a tiled image file that contains icons of the walls, empty floors, goal spaces, boxes, and the player. To ensure proper scaling, each tile in the image file should have a one-pixel border around it on all sides that repeats the pixels at the edges of the usable area. Without this, when using Corona's auto scaling to support higher-resolution displays, you may notice seam lines or a grid where the tiles don't quite touch each other.

Note

The included image file, bomb_party_v4.full.png, was generated from a downloaded sprite sheet by using ImageMagick's convert tool, a command-line tool highly useful for carrying out automated processing of images, including cropping, scaling, and layering images together. While a discussion of ImageMagick command syntax is well beyond the scope of this book, you can read many useful tips at http://www.imagemagick.org/Usage/ or review the tileextrude.sh file we've included in the code bundle for this project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.126.241