iOS For High School Students: Text Adventure Game

This is the next tutorial in the beginning programming for iOS series – for high school students, by high school students! This tutorial will walk you through creating an app from the very beginning. The tutorials are cumulative, so principles will build upon the first iOS For High School Students tutorial. As in the first […] By .

Leave a rating/review
Save for later
Share

A tutorial series about iOS for high school students - by high school students!

A tutorial series about iOS for high school students - by high school students!

This is the next tutorial in the beginning programming for iOS series – for high school students, by high school students! This tutorial will walk you through creating an app from the very beginning.

The tutorials are cumulative, so principles will build upon the first iOS For High School Students tutorial.

As in the first tutorial, you will create a Mac command line app to start simple. If you’re dying to get to the iPhone, don’t worry – the next tutorial in this series will be for an iPhone app! ;]

This time around, you’ll build a text-based adventure/survival game! You will take the role of a character crashed on a desert island, and your goal is to escape the island without being killed along the way.

In the process, this tutorial will cover the following topics:

  • Arrays
  • Classes
  • Objects
  • Methods
  • Properties

You’ve probably heard all of those words before, but not in the context of programming. You may be starting with no idea of what those words have to do with building apps, but trust me, you want to make friends with these concepts. And by the end of this tutorial, you will!

Getting Started

First of all, it would be a good idea to review the first tutorial in this series to jog your memory before continuing with this part.

All done? Then let’s get going with our game!

Games are by nature interactive – users make decisions that affect the outcome of the game. This game is no exception. When a decision is needed, it will ask the user for a number (1 or 2). The user’s input will trigger a resulting series of events.

Some text/adventure games have four options for the user to choose from (A, B, C, D). However, each additional option makes the game more complex, as you can imagine. :P

When I had the idea for the survival game, I took some time to write down the different parts of the program, as an outline. This approach will save you time programming. To give you a preview of what you’ll be building, here are the different components I listed:

  • Get the user’s input (name, responses)
  • Allow the user to restart easily
  • Create plot/storyline (consisting of good/bad/neutral decisions)
  • Give the user a health rating that can change through the course of the program

With the above in mind, let’s create our Xcode project!

Start up Xcode and create a new project with the Mac OS X\Application\Command Line Tool template. Enter MyAdventureGame for the product name, leave the company identifier as is, select Foundation for the Type, and make sure Use Automatic Reference Counting is checked:

Save your project to a location that you will remember. Now switch to main.m in your Xcode project navigator.

With the release of Xcode 4.2, Apple has added some neat new features for memory management called Automatic Reference Counting (ARC). If you recall, in the last tutorial you had to instantiate a NSAutoreleasePool, which held your memory, and then later release it (using [pool drain]) to free your computer for other tasks. Between the two screenshots below, note the difference in the structure of main.m.

The original main() function before ARC.

The original main() function before ARC.

The new main() function utilizing ARC.

Without going into a lot of detail, Automatic Reference Counting uses an autoreleasepool block. This determines which objects remain in memory based on pointers and references to the object as specified by the programmer.

For now, it is important to recognize the new structure. If you would like a beautiful explanation, click here.

The Framework

In order to make your program as clear to read as possible, main.m will be the file where all of your information is brought together. This means that the meat of your program will be stored in header files (.h) and other implementation (.m) files. All you will have to do is import the right files into main.m and then execute some methods.

To set up the framework, add some new files.

Create a new file with the Mac OS X\Cocoa\Objective-C class template. Name the class Game, and make it a subclass of NSObject (You probably will have the sub-class already set to NSObject. If so, you don’t have to do anything).

Save the file to the project folder. You’ll generally have the project folder location come up by default.

In Game.h you should see the following:

#import <Foundation/Foundation.h>

@interface Game : NSObject

@end

In the Game.m file, you should see the following:

#import "Game.h"

@implementation Game

@end

The purpose of the Game.h file is to lay out the names of the different variables and methods that will be implemented as part of the Game class. The Game.m file is where you will actually implement each of the methods, constructing how they work.

Notice that at the top of Game.m the “Game.h” file is imported. This connects the interface with the implementation. In order to begin using the Game class in your program, you simply have to import the header file for the Game class into main.m.

At the top of main.m, right below the existing #import line, add the following:

#import "Game.h"

To clarify, Game is a class you will create to contain all the guts of the program. Ultimately, your program will create an instance of the Game class, and you’ll call it myGame. Game.h and Game.m are just the blueprint for this class. As you create the Game class, I’ll go into more detail.

For now, your first goal is to enable the program to receive input from the user. The standard way of receiving input via the keyboard is the scanf() function, which takes the next character that the user types and stores that character in a variable of type char.

scanf( "%c", &aCharVariable); 

The %c tells the compiler to look for inputs that are of type char (this means that they are characters, not integers). When the program executes this line of code, the console will wait for the user to type in a character. If the user types x, then the value of aCharVariable will be set to x.

In this program, you’ll be using scanf() every time you need the user to enter an integer or character. Simply due to the nature of text-adventure games, where the plot is determined by user input, scanf() is going to be called a lot!

Because scanf() waits for user input, it can also be used to pause dialogue. For example, it would be better for the user to read a line, press enter, read a line, press enter, etc., rather than being given a whole paragraph to read at once. (The game needs to be interactive, duh!)