Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


                <canvas id="canvas" width="400" height="400"></canvas>





                const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const width = 400;
const height = 400;
const cw = 10;
let snake;
let game_loop = null;
let dir;

function drawBoard(){
  ctx.fillStyle = "white";
  ctx.fillRect(0, 0, width, height);
  ctx.strokeStyle = "black";
  ctx.strokeRect(0, 0, width, height);

function paintCell(x, y){
  ctx.fillStyle = "blue";
  ctx.fillRect(x*cw, y*cw, cw, cw);
  ctx.strokeStyle = "white";
  ctx.strokeRect(x*cw, y*cw, cw, cw);

function createSnake()
  let length = 10;
  snake = [];
  for(let i = length-1; i>=0; i--)
      snake.push({x:i, y:0});

function drawSnake()
    for(let i=0; i<snake.length; i++)
        paintCell(snake[i].x, snake[i].y);

function moveSnake()
  var newX = snake[0].x;
  var newY = snake[0].y;
  if(dir == "right") newX++;
  else if(dir == "left") newX--;
  else if(dir == "up") newY--;
  else if(dir == "down") newY++;
  var tail = snake.pop(); //pops out the last cell
	tail.x = newX; 
  tail.y = newY;
	snake.unshift(tail); //puts back the tail as the first cell

function paint(){
    	//restart game

function init(){
    dir = "right";
  	if(game_loop != null) clearInterval(game_loop);
		game_loop = setInterval(paint, 200);
function changeDirection(event){
  const key = event.keyCode;
	//We will add another clause to prevent reverse gear
  if(key == "37" && dir != "right"){
    dir = "left";
  } else if(key == "38" && dir != "down"){
    dir = "up";
  } else if(key == "39" && dir != "left"){
    dir = "right";
  } else if(key == "40" && dir != "up"){
    dir = "down";

document.addEventListener("keydown", changeDirection);

function hasGameEnded(){
  let hx = snake[0].x;
  let hy = snake[0].y;
  if(hx < 0){
    return true;
  } else if(hx >= width/cw){
    return true;
  } else if(hy < 0){
    return true;
  } else if(hy >= height/cw){
    return true;
  for(let i = 0; i < snake.length; i++)
		if(snake[i].x == hx && snake[i].y == hy)
		  return true;  
  return false;