The Montoya Herald, a weblog about Blueprint, jQuery, design, music and life, publishing on the web since September 2005. Written by Christian Montoya: developer, designer and entrepreneur.

The Montoya Herald — ChristianMontoya.com

Search

Things I Do

Supported By

Like What I Do?

My Amazon.com Wish List

On this domain

Elsewhere

Tunnel: A Javascript & CSS (jQuery-powered) game

Posted on May 10, 2007.

I've spent the past few days doing three things:

I'm here to talk about the last one I mentioned. I decided to get some practice with Javascript by making a web game, and I decided to port one of the games my lab partner & I programmed for our ECE 476 final project. Now I know the idea of making a game with Javascript & CSS sounds crazy, but I had a few good reasons for giving this a shot:

So I dove in. I made a game using jQuery with quite a bit of DOM manipulation and a few fade effects, as well as some Ajax requests talking to a PHP backend using a text file to store high scores. It was a big challenge at times but I was able to figure out most everything with the help of the jQuery site as well as php.net. Some things I discovered:

So putting it all together, my result is here: Tunnel. To explain, it's a very basic game where you use the mouse to move your ship left and right. As you play, the walls narrow until eventually you have about 2 widths of your ship to fly between. The game ends when you die, and your score gradually increases as long as you live. When you die, your score is checked against the top 10 and if you get a high score, you enter your name to put yourself on the list. It's not too complicated and you can check out the source to see everything that's going on at the front-end. I should mention, however, that this is my own work, so you can't steal it and put it on your own site. Plus, it has a couple bugs that I think are worth mentioning:

All that being said, the game works pretty well and I think it's a good example of what can be accomplished with Javascript & CSS. Feel free to have a look at it, learn from it, and get your own ideas. If you happen to use any of my code or ideas, please do give credit where credit is due. And I dare you to beat my high score ;)

p.s.: Want to know what's cool about making a game with Javascript and CSS? It works even when images are disabled! Disable images in your browser and check out the game to see what I mean.

Get a trackback link

1 Trackbacks/Pingbacks

  1. Pingback: All in a days work… on May 13, 2007

11 Comments

  1. Great work!
    It works a lot faster on IE7 than in Firefox 2.0 on Windows XP (it's not the bug, it's just the Javascript handling)

  2. kramtark on May 11, 2007

    Okay, I think 129's my best for tonight =)

  3. MN Web Development on May 11, 2007

    Haha, nice!! I only got up to like 36 points. Oh well. Better luck next time! ;)

  4. kramtark on May 11, 2007

    yawn Good morning. You know, I think that the path stopped decreasing in size after 90 or so… theoretically, even the so-called "hacks" (i.e., the higher scores) are reachable using the same technique I'm currently using without cheating. I am using IE7 on Windows XP, and the game seems to be running smoothly.

    "I dare you to beat my high score" — Done. Darn peer pressure…

  5. Christian Montoya on May 11, 2007

    Of course the path stops decreasing in size, otherwise there would be an upper limit to the score you could acheive.

  6. kramtark on May 11, 2007

    Still, it seems like the path could decrease in size once more or so and still be playable.

  7. Christian Montoya on May 11, 2007

    kramtark: It could, but it would be incredibly hard… if you are playing the game at full speed (in IE, for example) then I think you might agree. Plus, not everyone is a pro like you… I could add multiple difficulty levels though… that sounds like an idea!

  8. James on September 19, 2008

    It's too fast on Chrome. Use a slower wait period . . . 2 ms is too short! No one needs 500 FPS on a game like this.

  9. Christian Montoya on September 19, 2008

    You are correct sir, I just tested it in Chrome and it is waaaay too fast. I'll fix it.

  10. Ed on September 19, 2008

    Not too bad. Regarding your first point and that most recently raised in the comments, you don't really need to buy Flash development software to compile a SWF–plus I imagine it would be easier to guarantee a consistent experience using a VM whose authors are working to ensure browser compatibility, versus a language where the implementation is solely up to the browser vendor. Then again, Flash is my livelihood, so I might be biased ;).

    PS - How's life in the far (north)east?

    PPS - I'm drunk.

  11. Christian Montoya on September 25, 2008

    Well, there is a way to ensure that Javascript FPS are the same in every browser, and I've tested and confirmed this method, but I haven't actually had a chance to post a demo of it. I'm too busy with other things these days. And… I don't try to beat Flash with Javascript like I used to.

    NYC is a cool place. I'm always too busy though.

Leave a comment

Use Markdown or basic HTML. For posting code, use Postable. Please keep comments respectful and on topic.