OS X games from the ground up: your first programme

You can see an index of all the posts in this series: go to index.

If you are starting from this point, or need a fresh set of files, here are the starter files from the end of the previous post: first steps

In the first part of this series, you looked at Buzzword, the BASIC programme we’ll be converting and you also took your first steps with Xcode, the Integrated Development Environment (or IDE) we’ll be using to build it. Start up Xcode, if it’s not already running, and load the Buzzword project. If you’ve just reopened Xcode, you should see the project listed in the splash screen, so you can open it by double clicking on it from there:

Xcode recent files

If Xcode is already running but the project is not opened, select File, Open Recent, Buzzword.xcodeproj.

Make sure you can see the Project Navigator, and then open the main.c file.

I left you with a couple of exercises at the end of the last post. The first was to try and get the programme to display something other than the message “Hello, World!”, your name for example. If you completed this exercise successfully, you should have something that looks like this but with your name or personal message:

Buzzword exercise 1

When you build and run it, you should get something that looks like this:

Buzzword exercise 1 output

If you managed to get it to work, congratulations. As you have seen, printf will display whatever is between the quotation marks. But what about that strange character combination at the end? The second exercise was to work out what \n does. What did you come up with? If you tried putting it in the middle of the original phrase as I suggested, your code should have looked something like this:

Buzzword Exercise 2

And the output when you build and run should look something like this:

Buzzword Exercise 2 Output

You can see that everything after the \n appears on a new line. So that’s what these two characters do.

But why can’t you just hit the enter key in the middle of the message and insert a line break that way? Well let’s try it. Delete the \n and hit the enter key to insert a line break. Xcode will respond by showing error messages and if you try to build and run you’ll get a Build Failed! message.

Newline error

In C it is not permitted to put a physical line break into the middle of quotes like this. In fact there are a whole range of things you can’t insert, like tabs and backspaces for example. Because you might have a legitimate reason for wanting to include new lines or tabs or backspaces, C provides a way to include them by using what are known as escape sequences. These are the backslash character followed by a one letter code. You’ve already seen that \n is used for new line, others include \t for tab and \b for backspace.

You might be wondering what you do if you want to use the backslash character just as a straightforward backslash. Well there’s an escape code for that too – you use a backslash followed by another backslash: \\. A similar situation occurs when you want a double quote mark to appear in your message. If you just include it as it is, c will mistakenly think that’s the end of the message and you’ll get an error message again. You’ve probably figured out the solution. Yes, you use yet another escape sequence: \”. Here’s an example of how these are used with printf:

Of the six quotation marks used here, the four with a backslash in front of them will be displayed.

You might have noticed that this line of code ends with a semi-colon. A C programme is divided into a number of different statements. The semi-colon is how we tell C this is the end of the statement. You must remember to include a semi-colon at the end of every statement. If you don’t, C will complain by showing you error messages until you fix it.

OK it’s time to get started recreating Buzzword. If you refer back to the flowchart in the previous post, you’ll see that the first task is to show an introduction and some instructions. Let’s have a look at the start of the original BASIC programme to see how this is done:

Note that BASIC uses line numbers as a way of putting statements in order of execution. C doesn’t use line numbers – execution is determined by the order that statements occur in the file.

The command that BASIC uses to show information should look quite familiar, as it is very close to the C equivalent. Have a go at converting this to C and then building and running again. Don’t worry about the TAB instructions – we’ll show everything left aligned. By the way, when you see a PRINT statement without any quote marks following it, this is the BASIC equivalent of printf(“\n”);.

Here’s what my version looks like:

If  you chose to use several printf commands rather than just one, that’s fine too, but remember that C will run on the output from each of these. If you want a line break, you will need to explicitly put one in using \n.

Now when you try to run this, you’ll find that the output will run off the bottom of the tiny pane at the bottom of the screen. It’s time to introduce you to another Navigator. At the top of the Navigator pane, select the last icon, the one that looks like a speech bubble. This is the Log Navigator. It maintains a record of the output each time you run Buzzword. You can see the records of each time you have built and run in the Log Navigator. The latest run is at the top. Click this and you should now see the output from this run in the main pane. Your screen should look similar to this:

Xcode Log Navigator

So that’s the first stage of our Buzzword programme complete. In the next part of this series we’ll look at how we can store information in our programme.

This entry was posted in Games, Programming Tutorials and tagged , , , , , . Bookmark the permalink.

Leave a reply