1 Commits
Author SHA1 Message Date
David Pacak 3252feee0a Added my shit 2020-12-18 16:57:51 +00:00
2 changed files with 64 additions and 44 deletions
-1
View File
@@ -1 +0,0 @@
.idea
+64 -43
View File
@@ -17,8 +17,8 @@
} }
.tile { .tile {
width: calc(100vmin/30); width: calc(100vmin/10);
height: calc(100vmin/30); height: calc(100vmin/10);
border: 1px solid black; border: 1px solid black;
box-sizing: border-box; box-sizing: border-box;
} }
@@ -37,14 +37,11 @@
</style> </style>
<script> <script>
const fieldSize = 30;
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
const game = document.getElementById("game"); const game = document.getElementById("game");
const tileTemplate = document.getElementById("tile-template"); const tileTemplate = document.getElementById("tile-template");
for (let x = 0; x < 10; x++) {
for (let x = 0; x < fieldSize; x++) { for (let y = 0; y < 10; y++) {
for (let y = 0; y < fieldSize; y++) {
const tile = tileTemplate.content.cloneNode(true); const tile = tileTemplate.content.cloneNode(true);
tile.querySelector(".tile").classList.add(y + "-" + x); tile.querySelector(".tile").classList.add(y + "-" + x);
game.appendChild(tile); game.appendChild(tile);
@@ -68,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")) {
@@ -87,62 +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");
} }
}, 100); }, 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
let direction = {x: 0, y: 0} let paused = false;
let length = 1; let justAte = false;
function getRand(x) {
fruit = randomPlace(); return Math.floor(Math.random() * x);
function vectorAdd(a,b) {
return {x: a.x+b.x, y: a.y+b.y}
} }
function randomPlace() {
return {x: Math.round(Math.random() * (fieldSize-1)), y: Math.round(Math.random() * (fieldSize-1))};
}
function gameUpdate() { function gameUpdate() {
const oldSnake = snake[0]; if(paused){
const newSnake = vectorAdd(oldSnake, direction); return;
newSnake.x = (fieldSize + newSnake.x) % fieldSize }
newSnake.y = (fieldSize + newSnake.y) % fieldSize
snake.unshift(newSnake);
snake = snake.slice(0, length);
for (let i = 1; i < snake.length; i++) { if (fruit === null) {
if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) { fruit = {x: getRand(10), y: getRand(10)};
length = 1; }
snake = [{x:0, y:0}]; //console.log(dir);
fruit = randomPlace();
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;
} }
} }
if (fruit.x === snake[0].x && fruit.y === snake[0].y) { switch (dir) {
length++; case 1:
fruit = randomPlace(); 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() {
direction = {x: -1, y: 0}; dir = 3;
} }
function rightPressed() { function rightPressed() {
direction = {x: 1, y: 0}; dir = 1;
} }
function upPressed() { function upPressed() {
direction = {x: 0, y: -1}; dir = 0;
} }
function downPressed() { function downPressed() {
direction = {x: 0, y: 1}; dir = 2;
} }
function escPressed() {
paused = !paused
}
}); });
</script> </script>
</head> </head>