About a year ago I slapped together a simple game. It worked fairly well, but had a couple problems. You'll find it takes far longer to tweak a program to make it what you want that it takes to flesh out a shell of your idea. That was definitely true here. Try out the code here, use the arrow keys to move the ball back and forth.

In order to truly understand what we're doing here you need to read Chapter 8 and pages 141-150 in Getting Started with Processing. You should also work all the code examples found in those sections.

If you only look at the draw() and setup() in my Falldown program you'll really have little idea of what is going on. All the real work is done in functions I created. Basically, in draw() I just call the functions in the appropriate order. I could have put the code for each function into draw(), but then things would have gotten really confusing if I had to try and trouble shoot my code. It also made things easier to think about.

For example, I use the ballFall() function twice. I realized there were two different times when the ball needed to fall. Once when it's not on a line and then second is when it's "on a line" and on a hole at the same time. In both cases the ball falls. So, all I needed to do was call ballFall() and move on.

Quick Code explanations:

In this program I used arrays to keep track of the positions of each line and the hole on those lines. On every frame I had to update the positions of each line while also updating the postion of the ball. The arrays are created in the beginning of the sketch, in variable declaration. l[] for the line postions and hole [] for the location of the holes on each line.

In the draw() you'll notice I check a boolean variable lose. Boolean variables can be true or false. The initial state of loose is false in this sketch. I do this so that I can have an animation happen after you lose the game rather than just keep drawing and updating the lines and the ball. 

if (lose) means the same thing as if (lose==true). So if you lost, then none of the functions for the game to continue will be called until lose is set to be false.

Task -  Make my game better. Black and white is a bit boring. Add in some (or lots of) color. Just make sure it's not too distracting. You should also change the controls to respond to mouse functions so the game could be controlled on a touch screen.

Subpages (1): Falldown Code