o set a new direction.

setDirection checks to see if the new direction is valid. This is to stop going back on yourself, e.g. pressing left when you're travelling right. If the new direction is valid, then it sets nextDirection to the new direction. This is then used in advance so we know where the new head position should be.

Adding an apple

Now the snake needs something to eat, so let's give it an apple.

There's not much to this, except we're drawing a circle for the first time. To draw a circle in canvas, you need to start a new path, then draw a circle, and then fill the path. To start the path, call beginPath() on the context. To draw the circle call arc() on the context. arc takes 6 arguments, the x coordinate of the centre, the y coordinate, the starting angle, the finishing angle, and a boolean indicating whether to draw clockwise or not. The angle is measured in radians, and 360° is 2 * pi radians.

Finishing it off

Now that you know the basics you can view the source code of the final snake game and see what's
going on. It adds the technicalities of collision detection and score-keeping.

(For those playing at home, remember to click into the canvas area so that it can capture your keyboard events. It's presented here in an iframe which cannot receive events from the outside page.)


Advertise here with BSA

Learning Canvas: Making a Snake Game is a post from CSS-Tricks

]]> http://css-tricks.com/9876-learn-canvas-snake-game/feed/ 29 Sprite Cowhttp://www.spritecow.com/ http://css-tricks.com/9878-sprite-cow/#comments Wed, 22 Jun 2011 03:16:07 +0000 Chris Coyier http://css-tricks.com/?p=9878

Making a really good CSS sprite image usually isn't trivial work. There are all kinds of tools to help with it. My favorite of them has been SpriteMe. I describe my typical workflow for using that here. Sprite Cow might be my new favorite though, can't wait to try it. With it, you design your own sprite (♥) and you use the sweet Sprite cow interface to get precise CSS background positions for the different parts of it.…


Advertise here with BSA

Sprite Cow is a post from CSS-Tricks

]]>
Making a really good CSS sprite image usually isn't trivial work. There are all kinds of tools to help with it. My favorite of them has been SpriteMe. I describe my typical workflow for using that here. Sprite Cow might be my new favorite though, can't wait to try it. With it, you design your own sprite (♥) and you use the sweet Sprite cow interface to get precise CSS background positions for the different parts of it.

Direct Link to ArticlePermalink


Advertise here with BSA

Sprite Cow is a post from CSS-Tricks

]]>
http://css-tricks.com/9878-sprite-cow/feed/ 0
//AN_Xml: