(very quick one : ALL browsers with Canvas violate the spec in one or many ways, see tech notes of a very nice Canvas game engine called "canvex")
(off to enjoy flowers of the beautiful Swiss spring - walking flowers at that)
Fixed. The code is still as generic as before and it's a bit cleaner.
argo navis: Of course, but some parts of the specification, including fillStyle, are pretty solid and haven't changed for many months. Also the fail conditions of fillStyle are really simple and common.
quote:wrayal said:
Who else are we going to see scripts from? I'm hoping to get a nice little one done soon (just inside the deadline if I flog myself...), but nothing too serious, hopefully there'll be something more fun from me next month!Liorean? Argo?
Well, most of your 20 liners seem to have something to do with graphics. I'm not a graphics guy. In fact, I know pretty much nothing about rendering and image manipulation. I can write a state machine parsing code into a tree structure (probably needing more than 20 lines unless the grammar is dead simple, though , and a code generator transforming it to byte code, and an interpreter executing the byte code, but I wouldn't know where to start, what to do or how to do it when it comes to rendering something. And even if you had a challenge that fit me, it's not likely I would have the time to cook something up and polish it.
Is there a good/quick way for sorting/ordering polygons in such a simple scene? Right now I just sum the z-s and sort by that but it doesn't work too well... I know very little about z buffering and 3D is all too new to me ...
liorean: well then this is a great opportunity to get more familiar with some js graphics :P but I don't think it says anywhere in the rules that is has to be visual ...
It's a half-assed Boulder Dash implementation in 26 lines. I didn't exactly get a hang of how you guys count lines yet, so I may be miscounting. I'm not familiar with all the JavaScript code obshortification techniques that you guys know about. How can I improve this to shave 6 lines?
I used a single GIF image I found on a website for the graphics. They are the original Boulder Dash sprites. I may crop & reorder them to optimize the CSS a bit. Also, the colors are a bit too dark...
Hum, since an expression can be executed in the case statements, I'm tempted to count them too.
But I'm almost sure it can be shrinked to 20 lines. It might take to reorder the sprites and use proper array for the map instead of juggling with className and slices.
The week I got my first laptop, I made a Boulder Dash while commuting in the trains it was pretty cool way to spend time. Will try to put my hands on it. The code was not so big, about 4-5 kb for the markup + game logic and a few more kb for a few levels.
quote:Arthurio said:
Use inline if instead of normal if statement.
condition?statement_a: statement_b;
or
variable = condition?expression_a: expression_b;
That's not quite how it works, I'm afraid. The conditional expression always looks like this:
code:
conditionexpr ? truthypathexpr : falsypathexpr
If you try to use statements of any kind in any of the expressions, you will find the code gives a syntax error.
Arthurio: which is the normal behavior of the ternary operator, as Liorean said:
code:
condition?expressionIfTrue:expressionIfFalse;
Of course expressionIfTrue and expressionIfFalse can include function calls and even code blocks, but it must be a single statement/expression ( which can include , ).
Arthurio: Thanks for the suggestion about the ternary conditional operator instead of ifs. Is there a way I could capture mouse position without wasting too many lines?
poi: I've ditched the switch-case and replaced it with a for-in, for your temptation to count the case labels
I don't know how switching to a normal array would reduce the # of lines; it would eliminate the "className" references but the # of lines would be the same. Am I perhaps missing something?
Re-ordering the sprites would also just help optimize the CSS and I wouldn't gain any JS lines from that.
Implementing BD clones is a hobby of mine as well I have so far had one 68000 implementation on Amiga, one on Delphi and another (more complete) one in JS. Seems like the first thing I do when I get my hands on a new language is to implement a BD clone, instead of a "Hello World!"
The code is now down to 15 lines and now I'm tempted to add more elements
Features:
* Boulders and diamonds obey gravity
* Boulders and diamonds slide over walls and other boulders or diamonds
* Boulders prefer to slide left when balancing on another boulder or diamond
* Diamonds prefer to slide right when balancing on another boulder or diamond
* Butterflies are left-hand followers
* Rockford has a mind of his own
Where element is an overlayed element on top of the area you want to capture the mouse.
HTH
As for my saying about using an array instead of classNames, I don't know for sure. It's more a gut feeling. OTO the functions hash table technique is really compact.
*: wouldn't be great if there was a standard body for JavaScript instead of browser vendords adding whatever they want with no discussion. I'm really looking forward to ES4
MaGnA: Nice optimizations.I see you're using the JS 1.6+* Array.prototype.every method which is a Mozilla invention.
And on ES4 track, and supported by WebKit.
quote:Array.prototype.every = Array.prototype.every||function( callback, that ){ for( var i=0; i<this.length && callback.call( that||this, this[i] ); i++ ); return i==this.length;}
This at least makes the script work in Opera 9.5, but thows in IE7. Will try to figure why.
Strange. It works well enough in IE7 when trying to analyse it like this:
code:
var a=[];
alert(1+' '+Array.prototype.every);
Array.prototype.every = function( callback, that ){
alert('every!\n'+this.length);
for( var i=0; i<this.length && callback.call( that||this, this[i] ); i++ );
return i==this.length;
}
alert(2+' '+Array.prototype.every);
[0,1,2,3,4,5].every(function(x,y){alert([x,y]);a.push(x+':'+y);return true;});
a.join('\r');
quote:*: wouldn't be great if there was a standard body for JavaScript instead of browser vendords adding whatever they want with no discussion.
JavaScript being Mozilla's version of ECMAScript, I'd say Mozilla is the standardising organisation. They're driving the ES4 process, too, so expect JavaScript1.6-1.9 additions to be included in the ES4 standard library.
The lack of every method was not the only breaking in IE7. IE7 also chocked on undeclared variables. See my previous post.
Allright, Mozilla == Netscape == the creators of LiveScript aka Javascript. Sort of.
Beside they are not the only driving ES4, which is a good thing. More people playing the game means better, proper, standards. But well, I'm longing for ES4.
Liorean: Sorry I realized my " This at least makes the script work in Opera 9.5, but thows in IE7. Will try to figure why. " was misleading. I meant that the entire script threw, not the Array.prototype.every method itself. My bad.
I see you're using the JS 1.6+* Array.prototype.every method which is a Mozilla invention.
poi: Thanks for the compliments! Aww... I didn't realize that every() wasn't a widely supported method
Extending the prototype like you did is a good idea! I may employ it if I have any lines left to make my application x-browser.
And thanks for the mouse event code; I'll try to incorporate that
I have added two more features without increasing the line count. Rockford now blinks, taps his foot and looks towards where he's going. I've also added the firefly. I'm still at 16 lines (if everybody agrees to my counting method):
poi: I've incorporated your every() and (uninitialized = expr) fixes. The code size gone up to 19 lines now. I guess I won't bother trying to add mouse support now
Arthurio: looks like you're having fun. Keep going
MaGnA: Moot points, but the Array.prototype is an extension to non-Gecko browsers, not an IE fix. Also I thought the declaration itself didn't count, just like function delcarations don't count. The default|| part is just there to keep the native/previous implementation if there is one. Therefore I counted it as 2 lines. If it is so, you have the 2 lines required to add mouse support
This way the content to preload is not visible but browser still load the resources in the children elements.
BTW thanks to your page I finally realized the logic of the monsters. Never really thought about it before. The monsters were moving randomly in my boulder dash. Just found the source code. Yuk the files are from 2002 and the code is fugly
poi: Just as I was planning to add keyboard support... Tsk tsk tsk... Even if I add VR helmet support, mine won't approach the coolness of what you've got here. Awesome last minute entry!
As for the image caching you've suggested, I had tried a similar thing but to no avail. Maybe I did something wrong...
And yeah, I had figured that those monsters just follow this simple algorithm:
if (can move left)
move left, also turning towards left
else if (can move forward)
move forward
else
turn towards right, in place
Congrats again! We wouldn't expect anything less form you, poi.
(BTW it's hogging FF. Are you rendering even when there's no new keypress?)
Poi -- Very compact and quite speedy too -- wish we could get rid of that fisheye, though! (I'd poke it myself, but I never quite have the time XD) I need to download Opera to try your other entry...
Magna -- beautiful work! It's very cute and runs great in Safari.