Hudzilla Coding Academy: Assignment Two

Posted at 2:04pm on Saturday October 24th 2009

You should by now have completed the first eight projects of the Hudzilla Coding Academy, so you're starting to move out of the beginner's realm and into a more comfortable position where you feel confident tackling harder problems. If you're following this series with a tutor, it's now time to check you have learned everything fully, which means it's time for another assignment: a code project where you have to write the whole thing yourself.

These assignments aren't just me throwing you in the deep end to make you do work. And neither are they randomly chosen, skippable little tests that don't do much for your knowledge. Instead, these assignments are designed to pull together the skills you've been learning in the tutorials, getting you to put wha you've learned to use in new and interesting ways.

As with before, I'm going to give you some hints to help you solve the problems. But before I do that let's see what the assignment actually is...

Your task

If everything other project is a game, it's only fair that every other assignment is a game too: your task is to produce a game that shows the player increasing numbers of items and asks them to spot which item appears only once.

More specifically, your game must do the following:

Show lots of items on the screen, bouncing around. Every item should appear at least twice (it can appear three times, four times, or whatever), except one item, which should only appear once.

In the beginning, few items should be shown. For example, the player might see two apples, three bananas, two pineapples and a lemon. In this case, the lemon is the correct answer - it's the only item that appears once.

If the player gets the correct answer, the game should level up and show more items. Eventually, at level 5, the screen will be full of items, at which point the player wins.

If the player chooses the wrong item, the game should level down and show fewer items. Don't go below the starting level.

Each time the level changes, the screen should be cleared and a new set of items should be shown. The item that appears once will also be different, because it should be chosen randomly.

Your game should keep track of how long it takes for the player to win, then add that time to a high-score database that gets shown at the end. The table should show only the top ten scores.

Some hints from Hudzilla

It should be clear to you that to solve this assignment you're going to need to draw on a wide range of skills - making items bounce around the screen, saving and loading files, databases, drawing text, and more. Still, all of these things have been covered: look to project six for bouncing items and drawing text, project five for working with files, and project eight for working with databases. The only difference here is that we're pulling all these skills together, so try to break them down into smaller parts.

You'll probably want to use Bubble Trouble as your base, as that already has independently moving items done for you already. In the source code for this assignment I have included some pictures you can use for the elements that should be bouncing around the screen, but notice that they are square: clearly the Bubble Trouble click detection isn't the right technique to use here.

Working with the database should be simple if you have already completed project eight, but you should give some thought to how you save the scores. How do you store the player's name? How do you store their time? The first problem is solved using this line of code:

string username = Environment.UserName;

That will give you the username of the user that's currently logged in. The second problem, how to store the score, is much simpler if you cast your mind back to project three, which explained how to store timestamps as simple integers. Keep in mind that SDL already tells us how many ticks have passed since the game launched, so you're most of the way there.

And now, the interesting bit: how can you be sure that there's always one item that appears only once? Have a go at working a solution to this out by yourself. I'm going to give you a suggestion here, but at least write some thoughts down on paper - it's an interesting problem to solve!

Still here? OK, here's a quick way to do it: shuffle up all your items, then create one of the first item and store it away somewhere so your game remembers that it's the unique item. Then loop over the remaining items in the array (loop from 1 up to the size of the array) and create a random number of each, between 2 and 2 + the current level number.

An alternate method - one that gives you precise control of how many total items appear on the screen each time - is to create the unique item first, then loop over the remaining items and create two of each until the desired number of items has been reached. To make this work, you need to ensure that you always ask to create an odd number of items.

Extra credit

The following are not required to successfully complete this assignment. You should only attempt them if you're really looking for a challenge.

When the high score table is showing, let players press C to clear the scores.

Show the current level and time taken on the screen at all times. To show the time taken, just showing a seconds counter is enough.

Let the player press H to get a hint: remove one type of item from the screen. For example, if the odd one out is a pineapple, pressing H might remove all the bananas from the screen. When this happens, add 30 seconds to the time taken.