A Snake Game in JavaScript


This Could Be Better

The code below implements a simple “Snake” game in JavaScript. To see it in action, copy it into an .html file and open that file in a web browser that runs JavaScript. Or, for an online version, visit https://thiscouldbebetter.neocities.org/snakegame.html.

Use the A and D keys to turn the snake left and right. Collect the red “food” squares to increase the length of the snake, and thus the score. The game ends if the head of the snake runs into the outside wall of the playfield or part of the snake’s body.


 // main function main() { var level0 = new Level ( new Coords(32, 32) // sizeInCells ); Globals.Instance.initialize ( 200, // millisecondsPerTimerTick new Coords(128, 128), // viewSizeInPixels level0 ); } // classes function Coords(x, y) { this.x = x; this.y = y; } { Coords.prototype.add = function(other) { this.x += other.x; this.y += other.y; return this…

View original post 87 more words

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s