Wednesday, December 17, 2008

A silly little Twilight Game

There are some days I go to sleep after a long, hard day, and I think to myself, today... I really accomplished something, and that makes me happy and I fall asleep content.

Then there are days like today, where I'm just confused... but I suppose it was for a good cause. Brian, a friend of mine who got one of the first GamePacks ever, emailed me asking how hard would it be for me to build the shell of a game for his kids. He wanted the theme of the game to be the movie Twilight. (Un?)fortunately I haven't gotten a chance to see that movie in theatres yet...

So, while this is not my proudest accomplishment ever, if it makes his kids happy, I suppose I can justify the effort (and at worst, it refreshed my memory about how hard it is to do object collision detection for games, even if it is just Edward's head vs. rotating pictures of bad vampires. Ugh. I can't believe I just wrote that. I think a little piece of me deep down inside just died a little bit...). But I digress:

Here's the game, written for the GamePack and the InputShield. I've uploaded the source code for it all over at the "app store" (which is really becoming more and more just a place to throw source code so I can at least stay a little organized). The Arduino runs a continuous polling algorithm on the InputShield to read the dual potentiometers (which are set to mode A), and then sends the bytes up to the TouchShield. The TouchShield, in turn, interprets the commands, and moves a little picture (sprite) of Bella's head around the screen. Meanwhile, Edward's head moves around too, and tries to avoid the bad vampire heads.

Although technicaaaaaallly...

If my quick scan of the character summary over at Rotten Tomatoes serves me correctly, Edward doesn't shy away from confronting these bad guys in the movie. Oh well.

In my version, he randomly selects an opposing direction, and flees in either the inverse X, inverse Y, or some combination of those directions. On a collision with the vampire head, the vampire then iteratively selects whether to flee in the inverse x or y direction... or do nothing and continue charging ahead (dun dun dun). The suspense is already killing me :)

Here's a picture of it sitting on my computer. You can see the paper on the left side, which actually has the math and logic for the sprite collision checking algorithm. There must be a more optimized way to do it besides 8 ands, 16 compares, and 8 additions?

Probably the hardest part to code was the Edward vs. vampires collision detection and randomized scatter function:

if ( ((xPos > xmet && xPos<xmet+i) && (yPos>ymet && yPos<ymet+i)) ||
((xPos+i>xmet && xPos+i<xmet+i) && (yPos>ymet && yPos<ymet+i)) ||
(xPos>xmet && xPos<xmet+i) && (yPos+i>ymet && yPos+i<ymet+i)) ||
((xPos+i>xmet && xPos+i<xmet+i) && (yPos+i>ymet && yPos+i<ymet+i)))
if (choicedir) {
} else {
if (choicedir++ > 4) {
choicedir = 0;
xme += 2 * dirx2;
yme += 2 * diry2;

All of the source code is released under the GNU GPL over in a zip file, with the images and everything, over here.


1 comment:

Chris said...

Very cool!! Looking at your randomized scatter function reminds me of those late nights in school working with Karnaugh simplification maps.