Skip to content

Instantly share code, notes, and snippets.

@iankit3
Last active March 17, 2019 12:28
Show Gist options
  • Save iankit3/e712979fe6e706406992032210941743 to your computer and use it in GitHub Desktop.
Save iankit3/e712979fe6e706406992032210941743 to your computer and use it in GitHub Desktop.
Snake Game in Javascrip JS Bin// source https://jsbin.com/vihegiw
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Snake Game in Javascript">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style id="jsbin-css">
#canvas{
border : 2px solid green;
width : 400px;
height : 400px;
position : relative;
}
.block , .food{
width :20px;
height :20px;
background : yellow;
position : absolute;
border : 1px solid green;
}
</style>
</head>
<body>
<div id="canvas" tabindex="-1">
</div>
<button id="clear_btn" onClick="clearRects()">Clear</button>
<button onClick="startGame()">START</button>
<button onClick="stopGame()">STOP</button>
<script id="jsbin-javascript">
const canvas = document.getElementById("canvas");
let canvasP = null , Food = {};
//SPEED 1-10 -> slow-fast
let LEFT = 0 , TOP = 0 , DIR = "RIGHT" , SPEED = 6;
function bindEvents(){
document.addEventListener("keyup",(ev) => {
if(ev.keyCode == "38"){ //UP
DIR = "UP"
}else if(ev.keyCode == "40"){ //down
DIR = "DOWN"
}else if(ev.keyCode == "37"){ //left
DIR = "LEFT"
}else if(ev.keyCode == "39"){ //right
DIR = "RIGHT"
}
})
}
function drawASq(arg){
if(arg == undefined){//BASE-CASE
if(LEFT == Food.left && TOP == Food.top){
canvas.getElementsByClassName("food")[0].remove();
canvasP.appendChild(drawASq("true"));
Food = getFoodPosition();
drawFood(Food);
}
}
if(LEFT >= 400){
LEFT = 0;
}else if(LEFT < 0){
LEFT = 380;
}
if(TOP >= 400){
TOP = 0;
}else if( TOP < 0){
TOP = 380;
}
let block = document.createElement("div");
block.classList.add("block");
switch(DIR){
case "UP" : {
block.style.top = TOP+"px";
block.style.left = LEFT+"px";
TOP -= 20;
break;
}
case "DOWN" : {
block.style.top = TOP+"px";
block.style.left = LEFT+"px";
TOP += 20;
break;
}
case "RIGHT" : {
block.style.left = LEFT+"px";
block.style.top = TOP+"px";
LEFT += 20;
break;
}
case "LEFT" : {
block.style.left = LEFT+"px";
block.style.top = TOP+"px";
LEFT -= 20;
break;
}
}
return block;
}
let intervalRef = null;
function initGame(){
intervalRef = setInterval(() => {
removeRect();
drawRects(1);
},(10 - SPEED )*100)
}
function removeRect(){
canvasP.children[0].remove();
}
function clearRects(){
stopGame();
canvasP.remove();
}
function stopGame(){
LEFT = 0 ; TOP = 0 ; DIR = "RIGHT";
canvas.getElementsByClassName("food")[0].remove();
clearInterval(intervalRef);
}
function drawRects(num){
for(var i=0;i<num ;i++){
canvasP.appendChild(drawASq());
}
}
function getFoodPosition(){
//400 is our LIMIT so 20*20
return {
top : parseInt(Math.random() * 20) * 20,
left : parseInt(Math.random() * 20) * 20
}
}
function drawFood(){
let block = document.createElement("div");
block.classList.add("food");
block.style.background = "red";
block.style.left = Food.left+"px";
block.style.top = Food.top+"px";
canvas.appendChild(block);
}
function startGame(){
if(canvas.children[0] == undefined || canvasP == null){
canvasP = canvas.appendChild(document.createElement("div"))
drawRects(4);
}
bindEvents();
Food = getFoodPosition()
drawFood()
initGame();
}
</script>
<script id="jsbin-source-css" type="text/css">
#canvas{
border : 2px solid green;
width : 400px;
height : 400px;
position : relative;
}
.block , .food{
width :20px;
height :20px;
background : yellow;
position : absolute;
border : 1px solid green;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">const canvas = document.getElementById("canvas");
let canvasP = null , Food = {};
//SPEED 1-10 -> slow-fast
let LEFT = 0 , TOP = 0 , DIR = "RIGHT" , SPEED = 6;
function bindEvents(){
document.addEventListener("keyup",(ev) => {
if(ev.keyCode == "38"){ //UP
DIR = "UP"
}else if(ev.keyCode == "40"){ //down
DIR = "DOWN"
}else if(ev.keyCode == "37"){ //left
DIR = "LEFT"
}else if(ev.keyCode == "39"){ //right
DIR = "RIGHT"
}
})
}
function drawASq(arg){
if(arg == undefined){//BASE-CASE
if(LEFT == Food.left && TOP == Food.top){
canvas.getElementsByClassName("food")[0].remove();
canvasP.appendChild(drawASq("true"));
Food = getFoodPosition();
drawFood(Food);
}
}
if(LEFT >= 400){
LEFT = 0;
}else if(LEFT < 0){
LEFT = 380;
}
if(TOP >= 400){
TOP = 0;
}else if( TOP < 0){
TOP = 380;
}
let block = document.createElement("div");
block.classList.add("block");
switch(DIR){
case "UP" : {
block.style.top = TOP+"px";
block.style.left = LEFT+"px";
TOP -= 20;
break;
}
case "DOWN" : {
block.style.top = TOP+"px";
block.style.left = LEFT+"px";
TOP += 20;
break;
}
case "RIGHT" : {
block.style.left = LEFT+"px";
block.style.top = TOP+"px";
LEFT += 20;
break;
}
case "LEFT" : {
block.style.left = LEFT+"px";
block.style.top = TOP+"px";
LEFT -= 20;
break;
}
}
return block;
}
let intervalRef = null;
function initGame(){
intervalRef = setInterval(() => {
removeRect();
drawRects(1);
},(10 - SPEED )*100)
}
function removeRect(){
canvasP.children[0].remove();
}
function clearRects(){
stopGame();
canvasP.remove();
}
function stopGame(){
LEFT = 0 ; TOP = 0 ; DIR = "RIGHT";
canvas.getElementsByClassName("food")[0].remove();
clearInterval(intervalRef);
}
function drawRects(num){
for(var i=0;i<num ;i++){
canvasP.appendChild(drawASq());
}
}
function getFoodPosition(){
//400 is our LIMIT so 20*20
return {
top : parseInt(Math.random() * 20) * 20,
left : parseInt(Math.random() * 20) * 20
}
}
function drawFood(){
let block = document.createElement("div");
block.classList.add("food");
block.style.background = "red";
block.style.left = Food.left+"px";
block.style.top = Food.top+"px";
canvas.appendChild(block);
}
function startGame(){
if(canvas.children[0] == undefined || canvasP == null){
canvasP = canvas.appendChild(document.createElement("div"))
drawRects(4);
}
bindEvents();
Food = getFoodPosition()
drawFood()
initGame();
}
</script></body>
</html>
#canvas{
border : 2px solid green;
width : 400px;
height : 400px;
position : relative;
}
.block , .food{
width :20px;
height :20px;
background : yellow;
position : absolute;
border : 1px solid green;
}
const canvas = document.getElementById("canvas");
let canvasP = null , Food = {};
//SPEED 1-10 -> slow-fast
let LEFT = 0 , TOP = 0 , DIR = "RIGHT" , SPEED = 6;
function bindEvents(){
document.addEventListener("keyup",(ev) => {
if(ev.keyCode == "38"){ //UP
DIR = "UP"
}else if(ev.keyCode == "40"){ //down
DIR = "DOWN"
}else if(ev.keyCode == "37"){ //left
DIR = "LEFT"
}else if(ev.keyCode == "39"){ //right
DIR = "RIGHT"
}
})
}
function drawASq(arg){
if(arg == undefined){//BASE-CASE
if(LEFT == Food.left && TOP == Food.top){
canvas.getElementsByClassName("food")[0].remove();
canvasP.appendChild(drawASq("true"));
Food = getFoodPosition();
drawFood(Food);
}
}
if(LEFT >= 400){
LEFT = 0;
}else if(LEFT < 0){
LEFT = 380;
}
if(TOP >= 400){
TOP = 0;
}else if( TOP < 0){
TOP = 380;
}
let block = document.createElement("div");
block.classList.add("block");
switch(DIR){
case "UP" : {
block.style.top = TOP+"px";
block.style.left = LEFT+"px";
TOP -= 20;
break;
}
case "DOWN" : {
block.style.top = TOP+"px";
block.style.left = LEFT+"px";
TOP += 20;
break;
}
case "RIGHT" : {
block.style.left = LEFT+"px";
block.style.top = TOP+"px";
LEFT += 20;
break;
}
case "LEFT" : {
block.style.left = LEFT+"px";
block.style.top = TOP+"px";
LEFT -= 20;
break;
}
}
return block;
}
let intervalRef = null;
function initGame(){
intervalRef = setInterval(() => {
removeRect();
drawRects(1);
},(10 - SPEED )*100)
}
function removeRect(){
canvasP.children[0].remove();
}
function clearRects(){
stopGame();
canvasP.remove();
}
function stopGame(){
LEFT = 0 ; TOP = 0 ; DIR = "RIGHT";
canvas.getElementsByClassName("food")[0].remove();
clearInterval(intervalRef);
}
function drawRects(num){
for(var i=0;i<num ;i++){
canvasP.appendChild(drawASq());
}
}
function getFoodPosition(){
//400 is our LIMIT so 20*20
return {
top : parseInt(Math.random() * 20) * 20,
left : parseInt(Math.random() * 20) * 20
}
}
function drawFood(){
let block = document.createElement("div");
block.classList.add("food");
block.style.background = "red";
block.style.left = Food.left+"px";
block.style.top = Food.top+"px";
canvas.appendChild(block);
}
function startGame(){
if(canvas.children[0] == undefined || canvasP == null){
canvasP = canvas.appendChild(document.createElement("div"))
drawRects(4);
}
bindEvents();
Food = getFoodPosition()
drawFood()
initGame();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment