Click here to Skip to main content
Click here to Skip to main content

DeeHTML a web based platform game

By , 30 May 2007
 

Sample Image - deehtml.gif

Introduction

As an educational exercise, I have created a platform game. It's a beginner's Mario-type platformer using JavaScript, CSS and HTML only. It runs in Internet Explorer 5+ and Firefox. The game stars Dee, our hero, who must leap onto baddies' heads and collect lemons. 100 gives an extra life. Along the way, he will encounter cannons, moving platforms, water and a flying bird that drops eggs onto his head. Realistic, eh? This is my first attempt at writing a platformer game. I was using JavaScript to move tables around a screen when I thought about the possibility of capturing the user's keyboard input and allowing them to control the HTML tables using JavaScript.

Development

The idea grew to having a little image which moves in response to the keyboard arrow keys. Then I placed an HTML table on the screen. After each keyboard input I checked if the image was in the bounding box of the table, namely if it was between the left & right and top & bottom co-ordinates. If so, I stopped the movement, creating the illusion of collision. I drew a few more tables and set then out over the screen. Then I added some expando properties to the hero image. This allows me to add things not in the official <IMG> tag, but it is useful for my game. For example, my game hero tag has:

<IMG SRC='hero.gif' id=hero width=10 height=10 ohdestspeed=3 
    destx=100 desty=999>

Now see ohdestspeed, which is an expando property of the <IMG> tag that allows me to set and vary the player's speed. destx is the player's destination when you push the right arrow. Using JavaScript, I set the destx property to be the player's current posLeft plus ohdestspeed (3 pixels). In JavaScript:

Onkeydown="if(event.keyCode==39){
    document.getElementById('hero').style.posLeft+= document.getElementById(
    'hero').ohdestspeed;}"

For the left arrow, I subtract the player's posLeft, etc. For jumping, I set the desty to be whatever the player's posTop is and subtract 60. This means he will start to move up 60 pixels. When he reaches there, I add in code to make him fall. This sets the desty property to be the bottom of the screen (screen.clientHeight). When I added water later, to make the player slower, I just changed ohdestspeed to be 1. So, instead of 3 pixels per move, he now only moves 1.

All things must port

Making the game run in Firefox required some changes, as the custom expando tag attributes do not work the same. Instead, I put all of the custom attributes of each object in a comma-seperated list in the title tag. This is read into an array at runtime and works as before. Previously, I used document.all for parsing through elements. However, for the cross-browser approach, I now look through the document.body.childNodes collection which happily runs in both browsers. Element positioning in IE allows me to use element.style.pixelLeft (and pixelTop) and return an integer, fast and convenient. Yet in Firefox, I have to use element.style.left ( and .top), which returns a string like "144 px." Sadly, I have to use the parseInt() function, which makes this numerical much slower.

Let it roll

With moving objects, I made a function using JavasScript's setTimeout timer that runs every millisecond or so. Basically, this function called u1() moves enemies, checking their destx and desty and setting their posLeft and posRight properties. In this function, the player's position is checked against the co-ordinates of the tables (enemies and walls are tables). Because setInterval updates every millisecond, it gives the illusion of real movement. The display waits for the script to finish before updating, so that's why there is an interval (setInterval) between u1() function calls. During this interval, the display engine updates the screen and we resume. According to MSDN, "These timers cannot be relied on as being exact for timing purposes down to the exact millisecond." This does not argue well for crystal-precision updating and animation, but it works passably on my old 500 MHz test machine.

Eventually, it got more elaborate. As the levels got bigger, I added scrolling which checks if the player's posLeft is near the middle of the screen and scrolls the screen using JavaScript's document.body.scrollLeft (scrollTo in Firefox) property. I have hidden the scrollbars in the game frame so only the game can scroll ahead. Moving platforms scroll the player when he's on them and water changes the player's speed. Cannons in the game are easy: the cannon balls are just moving platforms that hurt! For scenery, I added clouds and leaves on the ground that have no interaction with the player.

Inner workings

The file main.htm holds the lives counter, time and lemon counter. It contains the game screen in an IFRAME. It also contains a 2 hidden IFRAMEs. One contains the game engine which updates the game IFRAME and the other contains the level HTML code. The level only loads once into this hidden IFRAME and then I transfer the level anew to the main screen each time the hero dies. When the level loads, its onLoad event tells the top score frame (main.htm) to load the file opiframe1.htm into the other IFRAME. This starts the game updating. The guts of the game are in opiframe1.htm and this is where you will need the script debugger turned on!

So, I've ended up with a mini-platformer which has my own amateur graphics using Paint! Try to finish it and then create your own levels!

Updates!

May 2007 - (v1.41) Fixed level loading bug and tweaked level 3

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

user_pk001
Ireland Ireland
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralGood stuffmemberjquery1_43 May '10 - 6:07 
Considering no jquery was used
GeneralWOW!memberKenny McKee10 Feb '09 - 6:07 
This is by far, the most awesome thing that i've seen someone do with DHTML and CSS. VERY nice work. 5 from me.   How many bytes of text have I typed in my lifetime??? Man, I wish I kept track...
GeneralFantastic!memberPerspx25 Aug '08 - 23:17 
Well done 5 from me   Regards, --Perspx   "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia...
GeneralSimply ExcellentmemberMember 412882425 Aug '08 - 18:16 
Very good work dude !!!   Keep rocking
GeneralWant some sound effectsmemberfstrahberger25 Sep '07 - 1:24 
Hi,   very good - but to make it perfect you should add some sound effects to the game   Here is a short article that explains how to make it: http://www.codeproject.com/useritems/DeeHtml_with_sound.asp
Generalamazing game, minor issuesmemberppppp111116 Jun '07 - 3:09 
hi. this is such a great game, its amazing what you have achieved with dhtml! but i notice on v1.41 that the flag no longer moves up the pole when you walk past it (on IE7 at least). plus the other issue is on level 2 when you are standing on the moving platforms, the movement goes jerky.
GeneralRe: amazing game, minor issuesmemberuser_pk0018 Jun '07 - 0:57 
Thanks for the comments, The flag issue could be to do with the cache in IE7, or the animation timing?? The jerkiness of the platforms in Level2 is due to the sliding effect that the'snow' has. I noticed that it made it very hard to manoever on moving platforms, so I went for a quick fix, and...
GeneralRe: amazing game, minor issuesmemberppppp111118 Jun '07 - 11:35 
well the flags moved up the poles perfectly on IE7 in v1.4, but in v1.41 they never work, so it must have been something that was changed between the 2 versions.
GeneralBloody BrilliantmemberRichardGrimmer31 May '07 - 0:19 
There goes my workday!   "Knock me down, I'll get straight back up again, I'll come back stronger than a powered up pacman" (Lilly Allen / Kaiser Chiefs)
GeneralImpressivememberBen Daniel30 May '07 - 12:48 
Goes to show you what can be done with HTML, javascript and determination. Good job!   Thanks, Ben
GeneralBrilliantmemberbhaskar_priya20 Apr '07 - 6:05 
Awesome..... you the man.... you got my 5
GeneralExcellent WorkmemberSyed Quadri20 Apr '07 - 3:56 
Simply Superb!!! Amaizing piece of work Dude!   Syed Quadri
GeneralExpandosmemberMarc Heiligers20 Mar '07 - 2:12 
Great stuff, really.   Just a note on expandos in FF: They work fine, but you need to access them using domElement.getAttribute(name) and domElement.setAttribute(name, value). This works in IE too, so should simplify your programming.   Have you thought of implementing it using a...
GeneralRe: Expandosmemberuser_pk00123 Mar '07 - 1:57 
Hi ,thanks for the comments. All the expando attributes are in the title tags as of v1.3, its a bit faster too.. I havent used anything like the library you mentioned, this began as an experiment and just snowballed, there was no grand scheme! best of luck PK
GeneralRe: Expandosmemberbaongoc25 May '07 - 9:39 
;'   doi la be kho
GeneralThanks dude, You rock!!!!!memberfengjinzhi1 Mar '07 - 18:10 
Thanks dude, You rock!!!!!
GeneralIncrediblememberTL Wallace27 Feb '07 - 5:32 
So extraordinary as to seem impossible. Great job.
GeneralNice.membervoidbent20 Feb '07 - 9:03 
Very very nice. But it is a pity that Fire Fox is not supported.
GeneralValid HTML / XHTMLmemberDerek Read12 Feb '07 - 15:23 
Following is not a criticism. Please consider it more of an "I wonder how hard it would be to do this" challenge. I loved this article and gave it a 5 for the same reasons everyone else has.   Right in your article you say you're using some non-standard HTML attributes. But it really...
GeneralRe: Valid HTML / XHTMLmemberuser_pk00112 Feb '07 - 23:46 
Derek, Interesting post, believe me, I would like to get it running on non-IE browsers, the crunch point is the setTimeout function--IE's implementation is speedy enough to let it flow, I vaguely remember firefox's being disappointing. Apart from the non-standard HTML this is the main obstacle ...
GeneralRe: Valid HTML / XHTMLmemberDerek Read13 Feb '07 - 7:33 
Ah, I see. Maybe one day I will try to implement something simpler as a test.   I've got quite a few ideas floating around in my head after looking at your engine. I'm not exactly sure what they are yet, but the basic idea with using tables and your positioning and movement schemes are...
GeneralWOW indeedmemberterabyte.si9 Jan '07 - 13:19 
I'm SW developer that's not easily impressed but this is really great work!
GeneralAwesome ScriptingmemberNarayana Dhoss8 Jan '07 - 20:21 
:-DHe u rock. the game was simply outstanding and the way in u which u have seperated the logics and other things. every single thing in this game is beautifully done. well done.   Narayana Dhoss Engineer Software Lason India
GeneralReally a Great Worlkmembersunil4319 Dec '06 - 17:42 
You really did a nice job man.   Keep it up.   search engine marketing solutions   Sunil Ahuja
GeneralSpectacular!membershivashankarp15 Dec '06 - 4:34 
This is spectacular! I love the idea as well as the game.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 30 May 2007
Article Copyright 2006 by user_pk001
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid