Compare commits

..

1 Commits

Author SHA1 Message Date
David Pacak
3252feee0a Added my shit 2020-12-18 16:57:51 +00:00

View File

@ -27,13 +27,13 @@
background-color: green; background-color: green;
} }
.tile.fruit {
background-color: gold;
}
.tile.head { .tile.head {
background-color: red; background-color: red;
} }
.tile.fruit {
background-color: gold;
}
</style> </style>
<script> <script>
@ -65,10 +65,13 @@
upPressed(); upPressed();
} else if (e.key === "ArrowDown") { } else if (e.key === "ArrowDown") {
downPressed(); downPressed();
} else if (e.key === "Escape") {
escPressed();
} }
}); });
window.setInterval(() => { window.setInterval(() => {
gameUpdate(); gameUpdate();
for (const tile of document.querySelectorAll(".body, .head, .fruit")) { for (const tile of document.querySelectorAll(".body, .head, .fruit")) {
@ -84,24 +87,83 @@
if (fruit !== null) { if (fruit !== null) {
document.getElementsByClassName(fruit.x + "-" + fruit.y)[0].classList.add("fruit"); document.getElementsByClassName(fruit.x + "-" + fruit.y)[0].classList.add("fruit");
} }
}, 1000); }, 300);
// this is where your implementation starts // this is where your implementation starts
let dir = 1; //orientation variable, 0 = up, 1 = right, 2 = down, 3 = left
function gameUpdate() { let paused = false;
let justAte = false;
function getRand(x) {
return Math.floor(Math.random() * x);
} }
function gameUpdate() {
if(paused){
return;
}
if (fruit === null) {
fruit = {x: getRand(10), y: getRand(10)};
}
//console.log(dir);
if (fruit.x === snake[0].x && fruit.y === snake[0].y) {
justAte = true;
fruit = null;
snake.unshift({...snake[0]});
}
if (!justAte) {
for (let i = snake.length - 1; i > 0; i--){
snake[i].x = snake[i-1].x;
snake[i].y = snake[i-1].y;
}
}
switch (dir) {
case 1:
snake[0].x += 1
break;
case 2:
snake[0].y += 1
break;
case 3:
snake[0].x -= 1
break;
case 0:
snake[0].y -= 1
break;
}
for (let index = snake.length - 1; index > 0; index--) {
const element = snake[index];
if (snake[0].x === element.x && snake[0].y === element.y){
alert('You suck at snake');
window.location.replace('https://www.youtube.com/watch?v=dQw4w9WgXcQ&list=PLahKLy8pQdCM0SiXNn3EfGIXX19QGzUG3');
}
}
snake[0].x = (snake[0].x + 10) % 10;
snake[0].y = (snake[0].y + 10) % 10;
justAte = false;
}
function leftPressed() { function leftPressed() {
dir = 3;
} }
function rightPressed() { function rightPressed() {
dir = 1;
} }
function upPressed() { function upPressed() {
dir = 0;
} }
function downPressed() { function downPressed() {
dir = 2;
} }
function escPressed() {
paused = !paused
}
}); });
</script> </script>
</head> </head>