Are you teaching yourself how to program and write code? Or do you have extra time to kill and want to brush up on your skills? Did you already make the "Hello World!" program in your language of choice, and maybe a Fahrenheit-To-Celsius converter, and now you're wondering what to do? While I was just starting out in the world of programming I often ran into this hurdle. Along the way I found some great resources and sources of inspiration to keep me going. Whether you're hoping to become a game programmer, write the next mobile app, or just want some practice, I think any of these exercises will help. Even advanced programmers may find some of the resources helpful (or they may already use them!)
All of these suggestions are language-agnostic. You can (and should) use whatever programming language you are comfortable with. I'm not suggesting that you do all of the following. These are just some ideas-- figure out what works best for you! I list the following exercises because this is what worked for me. I completed all of following goals and walked away with a lot of satisfaction and knowledge. I hope you can too.
Some simple programming exercises after Hello World
I've heard many people say, "I want to make a website that let's you make other websites. Should I learn PHP jQuery or should I learn HTML? Can I make a site like Yahoo in Dreamweaver?" Another question I've seen is "I want to become a game programmer. What's a good book for XNA programming that is for beginners?" Or, "I have this idea for an iPhone app. How do I make it?"
My reaction: Woah buddy! Slow down!
Let's just get this straight: Programming is (often) long, difficult, and very involved. Many games, mobile apps, and custom websites take months or possibly years to make. If you try to dive headfirst into creating the next hit mobile app or the latest and greatest website single-handedly you'll get frustrated and discouraged very quickly. You'll also probably make a lot of mistakes or end up going down the wrong path.
For beginner programmers who would rather not get discouraged my advice is to keep it simple. If you plan on making the next big thing you need to know how to do the small things first. So if you can make a Hello World application, can you also make a game of tic-tac-toe?
Write (in your language of choice) a text-based game of Tic-Tac-Toe
This is actually the first step towards making the latest and greatest website or mobile game/app. "But how does that make sense?" you might wonder. By completing this exercise you will surely ground yourself in the reality that is programming. You need to know how to read input in from a user, perform some logic on it, and spit out some new information back to the user. You need to understand how to keep repeating a loop until a goal is met. If you aren't able to perform these relatively small tasks, good luck making the next mobile app. However this project is pretty simple and makes for a great next-step exercise. Once you complete this you'll feel accomplished and ready to take on a more complicated project. Here's the project goal:
- Assume the user (player) is an X and the computer is a O
- Ask the player where they want to place their X
- The computer places an O
- Output the 9 tiles showing where each player went
- Ask the player where they want to place their X
- Repeat until a winner is determined
Sound pretty simple? Go and make it! Then play it. Try and break it. Put an X where an O is. See what happens when there is a "Cat" game (no winner). Also, when I mean text-based, here's what I am suggesting:
Write (in your language of choice) a text-based game of Hangman
Now that you have a working game of Tic Tac Toe, let's try Hangman. The underlying programming that you'll have to do will be fairly simple. Take what you've learned in creating Tic Tac Toe and use it to write really awesome code. Did you plan out your game of Tic Tac Toe before you started coding? Did you make functions, objects, or methods (or was it all in one big function?) Did you comment your code often and use meaningful variable names? If you didn't do any of that while creating Tic Tac Toe, start doing it while you're working on Hangman.
Here's the steps:
- Pick a random 5+ letter word, maybe from a list.
- Ask the player to guess letters
- If they guess correctly, show the progress.
- If they guess incorrectly, tell them how how close their person is to being hung.
Here's what I would imagine a text-based version looks like:
Write (in your language of choice) a GUI based game of Hangman
Now that you have a basic idea of programming and can write the underlying game logic it is probably time for a more user-friendly interface. Even if you plan to make a game engine yourself or hope to forever write console-based programs, learning how to work with a a GUI (or any framework for that matter) is a vital skill for programmers. Aspiring programmers have to learn how to use other people's code or frameworks and get their code to work nicely with it. This is something that comes up again and again. Software programming is not about reinventing the wheel every time you need to do something. For this reason I would suggest learning about what sort of GUI kits are available for your language of choice. For Java, you might want to check out Swing. For Python, you might want to check out TkInter. Do some research in which GUI frameworks are available for your language. However, don't stress too much about which framework to use. You'll be building a fairly simple game (and you already wrote the underlying game logic).
The key here is that you'll gain some experience with some of the more advanced topics of your language. You might not have had to worry about inheritance or objects and classes up until this point. However sooner or later you'll need to learn how to read the documentation for another framework and implement it correctly. You may realize that your game logic isn't as nice or modular as it could be. Or you may realize that you didn't do enough testing on one component of your software and now when you put everything together you can't figure out where the error is. You may even learn about design patterns (gasp!), such as the observer pattern that most GUI's are based upon. Regardless, this is often a very challenging step but a necessary one if you want to succeed as a programmer. It's hard to name even the most simple of software that wasn't built without other frameworks. Minecraft was built upon the LWJGL (which is actually built upon OpenGL and OpenAL), 3d Sound System, and JOrbis frameworks. Namebench was built upon Python, Tkinter, PyObjC, dnspython, jinja2 and graphy. Bioshock was built upon UnrealEngine 2.5 and Havok Physics. You have to know how to make your code work with other code.
I'll also throw in one more suggestion for making your Hangman game more exciting and fleshing out your skills-- try interfacing with a random word generator API to get a word before each game. For more information on using an API, see my tutorial here.
Write (in your language of choice) a simple 2D platformer
Now that you have the experience of writing simple game logic and the experience of working with a GUI framework you can move onto something more personal. A simple 2 dimensional platformer should have enough constraint and simplicity that you don't get overwhelmed and discouraged, but also enough flexibility to allow yourself to make this your project.
What if your end goal is to make a website? Or the next mobile app, or a search engine or a web crawler? Why a platformer game? Because programming games makes you think about writing good code. It forces you to think about the best data structure or the best algorithm. Game programming is not entirely different than programming for something else. If your game is running slowly you know your code isn't that great. A simple FPS (frames per second) counter allows you to continuously benchmark your progress. And best of all, programming a game from scratch is a lot of fun.
But remember, keep it simple! Start with really small goals:
- Create a stationary red square and display it on the screen.
Once that is taken care of, try adding animation. Here you'll have to determine how your programming language or GUI framework deals with threads and animation. (Hint: If you're using Java you'll probably implement a Runnable.)
- Have the red square move and bounce around on the screen.
Now once you have figured out basic animation you can add code so the red square responds to keyboard input. Here you'll have to determine how your programming language or GUI framework deals with keyboard input. (Hint: If you're using Java you'll probably implement a KeyListener).
- Instead of random bouncing movement, have the red square respond to keyboard buttons. (It moves up when the Up button is pressed, left when the Left button is pressed, etc).
Now step back and look at your code. Give yourself a pat on the back. Those three simple goals might not have turned out to be so simple after all. The good news is that now you can start customizing your game. Let's learn how to deal with File I/O and manipulating PNG images.
- Draw a small 20 by 10 pixel image and save it as a .png file.
- Have your game read in this image and display it instead of the red square.
If you are developing on a Windows machine, I would recommend using Paint.net to draw the image instead of the Microsoft Paint that comes with every copy of Windows. I also recommend saving it as a .png file because most programming languages have libraries that allow you to manipulate .png files more easily (and especially on a per-pixel level which may be very helpful down the road). Once you figured out how to read in and display a custom image, let's make this platformer feel like a platformer:
- Determine how you're going to store and display the level.
- Make the level.
- Display the level.
What does this mean exactly? Well, a platformer game is composed of platforms. Your player (the picture you just drew) will move around and interact with this platform. Maybe there are death spikes somewhere on the platform, or maybe just grass, or maybe there is snow. Regardless, you need to have a level that contains "blocks" or elements that the player interacts with. There are a couple ways of representing this level-- You could store the entire map and keep track of every single pixel, or you can break up your level into discrete elements (i.e. one block is 5 x 5 pixels). I prefer the latter approach because it allows you to create levels quickly and reuse components. Here are two ways of storing the level data:
The text file approach:
The .png file approach (100% magnification):
Notice that these two approaches contain the same sort of data. Either way you will have to write code that reads in a file (probably the .png or the .txt file), loops through the data, and builds the level. Here's what the level looks like after it translates the 54 x 40 element level into a 638 x 480 pixel game (each "element" is a 12 x 12 pixel block).
As you can see this is quite a simple game which was the goal of the project. Once you have figured out how to create, store, and render out a level you can start having fun. Here are some next steps (in no particular order) to practice and develop your programming knowledge:
- Create different blocks (a sticky block, death spikes, spawn points)
- Try adding gravity
- Figure out how to implement collision detection
- Add another level. Determine how you will "load" levels or switch to the next one. Does the "camera" follow along, or does the level change when you move to the edge of the screen?
- Put a measurable metric such as a Frames Per Second (FPS) counter somewhere in your game. How will you determine your frames per second?
- Create different blocks (a sticky block, death spikes, spawn points)
- Add other entities to the game such as enemies
- Make your static graphics move. Animate your character walking around.
Again these are just some ideas. I'll leave it up to you to figure out what you want your game to look like and to define your own goals. You'll quickly realize that this simple game may not be so simple after all. You'll also start realize the advantages of object-oriented design, or maybe why Flash ActionScript is a popular programming language for developing games in (animations, collision detection, graphic manipulations are all very trivial to perform in ActionScript). However all of these things will make you a more knowledgeable and skilled programmer in the end if you actually code them yourself.
As you struggle to make this "simple" game please realize that you are not alone. You will get stuck (and often). When I was working on this game and got stuck I gave myself a little break. Take a walk, get some coffee, surf the internet. Another good option is to see what the community is up to. Maybe someone else is making a very similar game and you can look to them for inspiration (Hint: Here is Minecraft Developer Notch's simple platformer in Java, as well as his source code (.zip) (which he released openly for Ludam Dare)). Take a look at what others are doing and how they handled the problems that you are facing.
Join a community (ask questions and answer questions)
Whether you are taking a break from your programming project (and you should take breaks!) or you just woke up and aren't quite ready for brain puzzles, a good middle ground is a programming community. Some suggestions are Stackoverflow, Reddit's /r/learnprogramming (for beginners), /r/programming (for advanced topics or news), or one of the many other communities that exist to bring people together and share knowledge. The important thing to remember as you struggle along on your journey is that you are not alone. All of the questions that you have or problems that are will run into have likely been brought up and discussed somewhere online. Take advantage of that!
Another perk of belonging in a programming community is that you'll often discover new ways or different approaches to tackling a problem. It also keeps possible solutions or technologies on your radar. Somewhere down the road you may have to do some specific task and thanks to frequently skimming the Q & A' s of a community forum you may have already read people's past experiences, problems, and solutions.
It can also be said that by teaching or explaining something you will be a better master of your craft. This is exactly why I started this website. The purpose of this website isn't just to teach you. My goal is to become a better writer, communicator, and programmer. If you can explain a concept to a complete stranger in a concise fashion and have them walk away knowledgeable then you will succeed in life. Your colleagues and friends will like you more. You will do better in job interviews. Your boss will appreciate you more. Communication is incredibly important for just about any job, and programmers are generally terrible at it. Take a look at some of the most successful scientists, engineers, and programmers of our time. Are they good communicators? You bet so.
Join a community (such as a game competition)
Another community to look into are one of the many game competitions that exist out there. Even if you have never made fully working software, a game, or an app before you probably know enough to make something. Even if your game is as simple as a two button, poorly drawn animation loop, it's still an awesome accomplishment (no offense to Shadow's game, I think it's great). Just remember, keep it simple. Some of the most successful games and software are often the simplest.
There are so many reasons why you should enter and participate in a game competition. You'll learn a lot. You will go through all of the stages of game/software development-- brainstorming, prototyping, creating, bug hunting, and publishing. You'll get a lot of publicity. People will actually play your game and give you feedback. And best of all, at the end of it, you can look back and point to a completed project. Just how good does that feel? Answer: really good. Instead of talking about all of your good ideas, this will actually force one out of you.
If you're unsure of which ones to join, here are my suggestions: Ludam Dare has an amazing community of fun, respectful, and sometimes famous developers (this competition can be partly attributed to Minecraft developer Notch's online following). Ludam Dare also has really fun "I'm in" videos and hosts to kick off each competition. Another competition worth checking out is the Java4K competition where participants try to develop a game in under 4 KB of Java code.
Find and solve short programming problems
Another resource worth tapping are one of many websites that provide simple programming problems. A website that immediately comes to mind is Project Euler. On it there are some 350+ problems that make for perfect little programming puzzles to work through. They are organized from the most easily solved to the ones that few people have solved. What I really found helpful was that you know when you solved the problem. There's a box to input your guess and the website will tell you whether you got it right or wrong. Once you've proven your merit you will be presented with a forum to look at other people's solutions to see how it was solved in another language or perhaps with a novel algorithm or approach. Many of the initial problems can be solved by beginner programmers with sloppy inefficient solutions, but as you progress you may realize that you brute force approach will get you the right answer eventually (but could take hours), forcing you to rethink your approach and create the optimal solution.
Other places to look for programming problems and exercises are academic courses. Many computer science professors put up all of the lectures, assignments, exams, and sometimes solutions on their course website allowing people from around the world to gain critical knowledge without paying absurd tuition rates. Some institutions deliberately open up their course information for this reason-- MIT has curriculum available, and Stanford has a program as well.
Take advantage of these resources. As mentioned at the top of this article, only by programming will you gain programming knowledge. Tackle complex problems, and at all times challenge yourself. Athletes don't stay in shape by taking a walk. Driving to work doesn't make you a professional driver. Replying to emails doesn't improve your typing skills. Good programmers don't get any better by writing code that they know how to write. Challenge yourself.
Books I found helpful
And finally take a minute to remember books. For many of us who are constantly reading blogs or have gone to school so long ago we may often forget that there is a plethora of textbooks available whose sole purpose is to teach us and provide knowledge. In each field there are always the classics (such as Strunk and White's standard for English majors) and computer science is no exception. Many people will point to the widely regarded K & K book on C programming as required reading. For those in software development, a required reading often cited is Design Patterns written by the "Gang of Four". And for whatever reason, computer scientists and programmers often reference Lewis Carroll's Alice in Wonderland as well as Through the Looking Glass, so it might be a good fun book to read before bed.
With those "required readings" out of the way I will briefly mention two books that really jump started my journey into programming. The first is Larry Ullman's PHP 6 and MySQL 5 for Dyanmic Websites which allowed me to create fun, useful, and interactive websites quickly. I had attempted to learn various other languages before picking up that book, but that was the first time that I really felt like I was learning and creating. I believe that Larry Ullman has recently (summer 2011) written a 4th edition of his book which you may want to check out at as well.
Once I had picked up the basics on programming I found an amazing Java book that I used to dive into the deeper aspects of programming. I believe that I am not alone in recommending Head First Java by Sierra and Bates (take a look at the reviews as well). Though it may not be ideal if you are just starting out (read my lessons learned as a beginner programmer here), it is definitely worth checking out after you know the basics (which is what this article is aimed at).
Disclaimer: I receive a small percentage of the sale when you buy those books (or anything from Amazon for that matter) by following one of those links listed above. It doesn't increase the price for you, but it helps pay for this web hosting. Thanks!
I hope you found this helpful! If you have any tips or resources that you've used along the way please share them in the comments. I hope you've noticed that I've deliberately left out advice on picking a programming language and just about all of my examples and resources are accessible in whichever language you want. For my reasoning behind that, you may want to read my article reflecting on my journey into programming.