On 12 June 2011 11:28, Sebastian Poręba <[email protected]> wrote: > I'm sorry to say that, but jQuery is not the best choice for game > development. I believe in "go native or go home" here, jQuery hides > from you how complex and slow some operations are. If you need a > framework, use one intended for games, like Mibbu (http://mibbu.eu/).
jQuery is no problem here. It's doing next to nothing in the main event loop. The main overhead it's adding is in initialization, where it's not going to be a problem. > Besides that your code is quite good. You may want to merge "var" > declarations. Also, try to avoid using switch, in javascript it may > fallthrough (http://javascript.about.com/od/hintsandtips/a/ > fallthrough.htm). The necessary break statements are there and there are a couple of places where there is deliberate use of fallthrough, so I don't see a problem with this either. > I'd say something about performance of slice, > unshift and other array functions, but I'm not an expert here. My > advise would be to check jsperf.com every time you want to use any > native function. Often there is a way faster implementation. The native array methods will be plenty fast enough for the numbers used here. Any serious candidate for optimization will be in the drawing code, which will be much the slowest part of the whole thing. > On Jun 11, 8:01 pm, Nick Morgan <[email protected]> wrote: >> Hi all >> >> I've written a basic Snake game in JavaScript. It's available on jsFiddle >> here:http://jsfiddle.net/skilldrick/bg7UF/(it's over 300 lines so thought >> a bit long for copy-pasting) >> (Also on Github if anyone's interested:https://github.com/skilldrick/jsSnake) >> >> I'm going to be using this for a blog tutorial on canvas, so I want >> the code to be as clear and 'correct' as possible. >> >> I'd really appreciate any feedback/constructive criticism you guys have. >> >> Cheers! >> -- >> Nick Morganhttp://skilldrick.co.uk >> @skilldrick My observations, for what they're worth: - Attach the key event listeners to the document rather than the body. The jsFiddle doesn't work as it is in Firefox 4. Even better, attach the event listeners to the canvas, although you'll need to give it a tabindex for that to work. - It doesn't look like you need to draw the border every frame, so I'd suggest you don't. Just draw it once. There are other optimizations you could do to reduce what you draw each frame, but you should do some testing to decide if it's worthwhile first. - The canvas context is a host object, so its moveTo() and lineTo() methods are not obliged to be genuine Function objects and therefore may not have an apply() method. You're probably OK in current browsers and likely to be OK in future browsers, but it's not guaranteed. Otherwise it looks pretty sensible to me. I do agree with Sebastian up to a point about jQuery: it's not doing much for you for this, so I'd be inclined to drop it entirely. It's not as though you're going to be worrying about IE < 9 anyway if you're using canvas. Tim -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
