Snake test

NB: NOT my own code!

by Laurens Maneschijn

HTML

<!--
NOTE: none of this is my work!
All html/css/js code is a copy paste from:
  http://cssdeck.com/labs/classic-snake-game-with-html5-canvas
It did not work there and I just copy pasted it here to see if it works and how.
- replaced all 'http' with 'https' to fix security blocks,
- fixed onclick (init was not known from html onclick attribute)
- fixed loading (100% did not always trigger, added fallback start showButton() on timeout)

related; also see:
  https://jsfiddle.net/jalbertbowdenii/DJPUD/
-->
<a target="_blank" 
  href="http://cssdeck.com/labs/classic-snake-game-with-html5-canvas"
  style="z-index:999;font-size:12px;"
  >original source</a>

<canvas id="canvas"></canvas>

<div id="reMenu">
	<h1 id="snake2">Snake</h1>
	<p id="info2">Game Over</p>
	<a href="javascript: void(0)" id="restart">Restart</a> 	
	<a href="#" id="tweet" target="_blank" rel="nofollow">Tweet My Score</a> 
</div>

<div id="menu">
	<h1 id="snake">Snake</h1>
	<p id="info">by <a target="_blank" rel="nofollow" href="https://twitter.com/SolitaryDesigns">Kushagra Agarwal</a></p>
	<a href="javascript: void(0)" id="start">Start</a>
	<p id="loading">Loading...</p>
</div>

<p id="score">Score: 0</p>

<!-- Audio -->
<audio id="main_music" loop>
	<source src="https://dl.dropbox.com/u/26141789/canvas/snake/main.mp3" type="audio/mp3" />
	<source src="https://dl.dropbox.com/u/26141789/canvas/snake/main.ogg" type="audio/ogg"/>
</audio>

<audio id="gameOver">
	<source src="https://dl.dropbox.com/u/26141789/canvas/snake/go.mp3" type="audio/mp3" />
	<source src="https://dl.dropbox.com/u/26141789/canvas/snake/go.ogg" type="audio/ogg"/>
</audio>

<audio id="food">
	<source src="https://dl.dropbox.com/u/26141789/canvas/snake/food.mp3" type="audio/mp3" />
	<source src="https://dl.dropbox.com/u/26141789/canvas/snake/food.ogg" type="audio/ogg"/>
</audio>

CSS

@import url("https://fonts.googleapis.com/css?family=Press+Start+2P");

body {padding: 0; margin: 0; overflow: hidden;}
#canvas {background: black;}

#menu, #reMenu {
	position: absolute;
	z-index: 1;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background: black;
}

#reMenu {background: transparent;}

h1, p, a {
	position: relative;  
	z-index: 1; 
	font: 72px "Press Start 2P", cursive;
	color: white;
	text-align: center;
	margin: 0;
}

a{
	font-size : 16px; 
	margin: 0;
	display: inline-block;
	position: relative;
	left: 0;
}

h1 {margin-top: 5%;}

p {
	font-size: 16px;
	margin-top: 10px;
	padding-left: 200px;
}

a#start, a#restart, a#tweet, p#loading {
	font-size: 24px;
	display: block;
	width: 120px;
	top: 30%;	
	left: 0; 
	border-top: 5px solid white;
	border-bottom: 5px solid white;
	padding: 10px;
	text-decoration: none;
	margin: 0 auto; 
}

a#restart {width: 165px; top: 20%}

a#tweet {top: 25%; width: 342px;}

#score {
	bottom: 10px;
	position: absolute;
	z-index: 1;
	padding-left: 10px;
	font-size: 16px;
}

p#loading {
	border: none;
	font-size: 16px;
	top: 20%;
	width: 200px;
}

a#start{top: 100%;}

#info2 {padding : 0;}

JavaScript

//Preloading audio stuff
var mainMusic, foodMusic, goMusic, file, files, counter, restart, start, loading;
mainMusic = document.getElementById("main_music");
foodMusic = document.getElementById("food");
goMusic   = document.getElementById("gameOver");
restart   = document.getElementById("restart");
start     = document.getElementById("start");
loading   = document.getElementById("loading");

files = [mainMusic, foodMusic, goMusic];
counter = 0;
// fix: set event handlers here so it knows about init() and reset() scope:
restart.onclick = function(){reset();}
start.onclick = function(){init();}

for(var i = 0; i < files.length; i++) {
	file = files[i];
	file.addEventListener("loadeddata", function() {
		counter++;
		var percent = Math.floor((counter/files.length)*100);
		loading.innerHTML = "Loading " + percent + "%";
		if(percent == 100) showButton();
	});
}
// TODO fix: set src AFTER setting event handlers, so the event handlers always trigger:
// odd, setting src later does not seem to work.
var file_sources, file_source, datasrc;
file_sources = document.getElementsByTagName('source');
for(var i = 0; i < file_sources.length; i++) {
  file_source = file_sources[i];
  datasrc = file_source.getAttribute('data-src');
//datasrc = file_source.dataset['src'];
  if(datasrc){
    file_source.src = datasrc;
  }
}

function showButton() {
	start.style.top = "30%";
	loading.style.top = "100%";
}

//Initializing Canvas
var canvas = document.getElementById("canvas"),
		ctx = canvas.getContext("2d"),
		
		//Full width and height
		w = window.innerWidth,
		h = window.innerHeight;
	
canvas.height = h;
canvas.width = w;

var reset, scoreText,menu, reMenu, score = 0;

function init() {
	mainMusic.play();
	menu.style.zIndex = "-1";
	
	var snake,
			size = 10,
			speed = 25,
			dir,
			game_loop,
			over = 0,
			hitType;
	
	//Custom funny gameover messages
	var msgsSelf = [];
	msgsSelf[0] = "There's plenty of food. Don't eat yourself!";
	msgsSelf[1] = "Is your body tastier than the...