Saturday, March 27, 2010

Game In A Day - "Free Fall"

A theme I've done twice now is "Game In A Day" in which I sit down and create a brand new game from scratch in no more than 24 calendar hours. This post will outline the first such "Game In A Day" experience which took place a little over a month or 2 ago...

I'm huge into music. I play guitar, and I can't accomplish much of anything without my favorite tunes on. As I've mentioned in another post, I've always wanted to create a game based entirely around music. I coupled this desire with my desire to make a brand new game from scratch in less than 1 day. The goal: Create a game that is inspired and based around a single song, and do it all in 1 day.

The Tools
Due to the time constraints and past success with the language/library, I chose to use Python along with the PyGame API for this project. PyGame is really just a front-end for SDL with some added goodies. I use SDL extensively from C++, so PyGame feels very comfortable to me.

The Song
As for the song, that was a harder decision. I loaded up about 5 band's worth of material, turned my monitor off, turned my lights off, and paced around for about an hour trying to find something that screamed, "Game!" to me.

After a while, I decided on Dethklok - Comet Song. It is very fast paced and evokes a sense of urgency and thrill, 2 emotions perfect for a game.

You can check the song out here:
http://www.youtube.com/watch?v=kRQ47oEbA6s

The Game Design
Using the flow of the song as my guide, I started to picture an environment in which I was under constant pressure to evade and/or maneuver. The more I listened to the song, the theme of being in space (as is the theme for the song) started to appeal to me. I decided to design the game around this idea in the following ways:
  • The character will be falling through space
  • There will be objects coming at it with varying rates of speed that it must avoid
  • The character will only have 2 controls: move left, move right
  • The goal will be to stay alive as long as you can, earning points the longer you're alive
Pretty simple, but definitely has the potential for some fun, and it fits the song perfectly.


The Game Implementation

Parallax
Right off the bat I knew I wanted to create some sort of parallax effect. I had never implemented it before, and creating a game in 1 day really isn't that difficult, so I wanted to add some level of challenge to it, as well as gain some experience doing something cool like parallax.

The idea behind parallax is that you create the illusion of movement by layering background images on top of each other, giving each a different speed (and some layers don't move at all) to give the illusion of distance and speed in relation to the character.

I spent probably more time designing and tweaking the parallax than I did on anything else in the project. I wanted to fake the character's fall through space by creating the parallax in such a way that the character would only ever move left or right, but the outer space background(s) would move from the bottom of the screen to the top of the screen, thus creating a falling effect.

My final design was as follows:
  • For each background image traveling at a particular speed, there needs to be another background image traveling at that same speed. The image can be different, but the speed must be the same. The images are the exact size of the screen coordinates.This second image will be offset from the first one by a distance of the height of the image (SCREEN_HEIGHT). The reason for this is so that as both images move towards the top, there will never be a gap in backgrounds before the second half of the pair appears on the screen.
  • Apply 1 or more static background images to create points of reference for the character
  • Have 2 pairs of moving layers: a fast pair and a slow pair. In my implementation, each pair had their own image, but the image was the same within the pair. The images were just basic star maps I made in The Gimp using various colors and sizes of dots placed randomly around the image.
  • Once an image has moved from the bottom of the screen to the top of the screen and then completely off the screen, reset its position to its original starting position (either y = SCREEN_HEIGHT or y = SCREEN_HEIGHT * 2)
That's basically it. It took a lot of tweaking to get it to that level, but in the end it's pretty simple, but does the job nicely for this project. Horizontal parallax is a bit more involved, but not too far of a reach from what I've done here.

Asteroids
Since we're in space, I figured what better obstacle than asteroids and space debris? I browsed Google Images for something I could work with and then doctored it up in The Gimp. I essentially cut my own shape out of an image of a real asteroid and then colorized the image with a nice bright color. I ended up with 4 different colors if I recall correctly and they actually look really nice.

Next, I had to decide where the asteroids would be "attacking" from. I toyed with the idea of having them come from every direction, but felt this would make it too difficult. Then I thought about maybe just up from the bottom and down from the top, but in practice, it took away from the parallax effect and it was no longer believable that the character was falling. I settled on just having the asteroids appear at a random X-value at the bottom of the screen and head upwards toward the player and I'm very happy with the result of this.

I also had to determine how many asteroids I wanted to generate on each update of the game loop. I just went simple and kept track of the number of updates and used the modulus function to only generate an asteroid every 5 updates and it works surprisingly well.

The Character
Next, I needed a fun character to play the main role in the game. I ended up deciding on a cute little leaf. It fit the theme of falling, and I thought there was some neat stuff I could do with a leaf in the game. When I threw the leaf in initially, I knew I had a problem: Only allowing left and right movement wasn't selling the parallax at all.

I essentially had a static image that would just move in a straight line from one side of the screen to the other. So, what could I do? Hey, it's a leaf, how do leaves fall? They sway back and forth, right? So, my leaf should do the same! I used the same modulus trick to tackle this one, and every 5 updates I would rotate the leaf image by 90 degrees. This gave it a nice floating, flipping effect as you see leaves demonstrate when they fall. Once again, modulus saved the day!

Scoring
For scoring, I toyed with the idea of adding objects that you wanted to collide with (power-ups, bonuses, etc...) and the player would get points from those. I tossed this idea because with the random nature of how asteroids were being created, it would have proved difficult for the player to obtain these power-ups. I decided to just award points to the player for staying alive. I got lucky and picked some value multipliers at random and they ended up working out and I stuck with them:
  • The player has 3 lives
  • 1 point is added to the player's score on every game loop update
  • 100 points are deducted for colliding with an asteroid
  • On your 3rd collision, no points are deducted, thus allowing you to finish with the amount of points you've obtained since your last death.
The idea here is to facilitate high scores to keep the player excited and interested in beating their high score. With the scoring and lives, I had to go and implement a simple HUD (heads-up display). It's pretty basic and renders the number of lives (complete with mini images of the leaf) and the current score in the upper-left hand corner of the screen, and the high score in the upper-right hand corner of the screen.

Extras
After some play-throughs, I realized how fun and addicting the game was. It lacked one key thing though: score persistence. I found myself wanting to beat my high score, but only to find that I couldn't keep track of it! I whipped up a save and load mechanism to keep the player's score on disk. I also added the ability to play again once you died instead of having to restart the game.

Conclusion
This project was a total success. I finished the entire thing start to finish in under 10 man hours, and that counts dallying around with images! I'm very pleased with the outcome in terms of the goal I set out to achieve, and honestly didn't expect the game to turn out to be as fun as it is!

Screenshots

No comments: