ES6 Game of Snek
How do I make an es6 game of snek?
Stay away from the walls and above all don't eat your own tail!. What is a es6 game of snek? How do you make a es6 game of snek? This script and codes were developed by Nicolas Udy on 10 August 2022, Wednesday.
ES6 Game of Snek - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>ES6 Game of Snek</title> <link rel='stylesheet prefetch' href='css/wxxzbj.css'>
<link rel='stylesheet prefetch' href='https://fonts.googleapis.com/css?family=Baloo'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <main class="snek"> <h1 class="snek__title">SNEK</h1> <section id="snek" class="snek__grid"></section> <div class="snek__info"> <p>Snek length: <output id="length"></output></p> <output id="msg"></output> </div>
</main> <script src="js/index.js"></script>
</body>
</html>
ES6 Game of Snek - Script Codes CSS Codes
.snek { background-image: -webkit-linear-gradient(top left, #b7d1da, #d5d6aa); background-image: linear-gradient(to bottom right, #b7d1da, #d5d6aa); height: 100%; display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-orient: vertical; -webkit-box-direction: normal; -ms-flex-direction: column; flex-direction: column; -webkit-box-pack: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -ms-flex-align: center; align-items: center;
}
.snek__grid { width: 425px; height: 425px; display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-align: stretch; -ms-flex-align: stretch; align-items: stretch; box-shadow: 0 0 0 rgba(255, 0, 0, 0);
}
.snek__col { -webkit-box-flex: 1; -ms-flex: 1 0 0; flex: 1 0 0; display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-orient: vertical; -webkit-box-direction: normal; -ms-flex-direction: column; flex-direction: column; -webkit-box-align: stretch; -ms-flex-align: stretch; align-items: stretch;
}
.snek__col:first-child { margin-left: 1px;
}
.snek__tile { position: relative; -webkit-box-flex: 1; -ms-flex: 1 0 0; flex: 1 0 0; margin-right: 1px; margin-bottom: 1px; background-color: rgba(43, 48, 59, 0.1);
}
.snek__tile:first-child { margin-top: 1px;
}
.snek__tile::before { content: ''; width: 100%; height: 100%; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); opacity: 0;
}
.snek__tile.food::before { border-radius: 100%; background-color: #f6f9fc; opacity: 1;
}
.snek__tile.tail::before { -webkit-transform: translate(-50%, -50%) rotate(45deg); transform: translate(-50%, -50%) rotate(45deg);
}
.snek__tile.head::before, .snek__tile.tail::before { background-color: #646a77; opacity: 1;
}
.snek__tile.dead::before { background-color: #be636a; border-radius: 25%; opacity: 1;
}
.snek__title { color: #646a77; font-size: 2.5rem; font-family: 'Baloo', cursive;
}
.snek__info { width: 425px; display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-pack: justify; -ms-flex-pack: justify; justify-content: space-between;
}
ES6 Game of Snek - Script Codes JS Codes
'use strict';
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Snek = function () { function Snek() { _classCallCheck(this, Snek); this.length = document.getElementById('length'); this.grid = document.getElementById('snek'); this.msg = document.getElementById('msg'); this.direction = null; this.process = null; this.active = true; this.food = null; this.moves = []; this.snek = []; this.keyMap = { '37': [-1, 0], // left '38': [0, -1], // up '39': [1, 0], // right '40': [0, 1] // down }; this.msg.innerHTML = 'Press any arrow key to start'; this.grid.innerHTML = ''; // build grid 25x25 tiles for (var x = 0; x < 21; x++) { var col = document.createElement('div'); col.classList.add('snek__col', 'x'); for (var y = 0; y < 21; y++) { var tile = document.createElement('div'); tile.classList.add('snek__tile', 'y'); tile.dataset.coords = x + ',' + y; col.appendChild(tile); } this.grid.appendChild(col); } // set initial position at center and generate food this.getTile([10, 10]).classList.add('head'); this.snek.push([10, 10]); this.food = this.newFood(); this.ready(); } // returns tile node by coordinates array Snek.prototype.getTile = function getTile(_ref) { var x = _ref[0]; var y = _ref[1]; return this.grid.querySelectorAll('.x')[x].querySelectorAll('.y')[y]; }; // calculate new coordinates based on passed direction array Snek.prototype.getSnekMove = function getSnekMove(_ref2, _ref3) { var currentX = _ref2[0]; var currentY = _ref2[1]; var directionX = _ref3[0]; var directionY = _ref3[1]; return [currentX + directionX, currentY + directionY]; }; // check if the tile is currently a snek tile Snek.prototype.tileIsSnek = function tileIsSnek(_ref4) { var x = _ref4[0]; var y = _ref4[1]; return !!this.snek.find(function (_ref5) { var snekX = _ref5[0]; var snekY = _ref5[1]; return snekX === x && snekY === y; }); }; // check for wall/snek collision Snek.prototype.snekIsDead = function snekIsDead(_ref6) { var x = _ref6[0]; var y = _ref6[1]; var outOfBounds = 0 > x || x > 20 || 0 > y || y > 20; var ateItself = this.tileIsSnek([x, y]); return outOfBounds || ateItself; }; // check if coordinates are food Snek.prototype.snekAteFood = function snekAteFood(_ref7) { var x = _ref7[0]; var y = _ref7[1]; return x === this.food[0] && y === this.food[1]; }; // generate and init food node then return coordinates Snek.prototype.newFood = function newFood() { var newPos = function newPos() { return [Math.random() * 21 | 0, Math.random() * 21 | 0]; }; if (this.food) this.getTile(this.food).classList.remove('food'); var pos = newPos(); while (this.tileIsSnek(pos)) { pos = newPos(); }this.getTile(pos).classList.add('food'); this.length.innerHTML = this.snek.length; return pos; }; Snek.prototype.ready = function ready() { var _this = this; document.addEventListener('keydown', function (_ref8) { var key = _ref8.which; if (36 < key && key < 41 && _this.active) { var newDirection = _this.keyMap[key]; if (!_this.direction) { _this.direction = newDirection; } else { // prevent player from reversing course var ref = _this.moves.length ? _this.moves[0] : _this.direction; if (ref[0] + newDirection[0] && ref[1] + newDirection[1]) _this.direction = newDirection; } // start gameplay if (!_this.process) { _this.msg.innerHTML = ''; _this.process = setInterval(function () { var head = _this.snek[0]; var tail = _this.snek.length > 1 ? _this.snek[_this.snek.length - 1] : false; if (_this.snekIsDead(_this.getSnekMove(head, _this.direction))) { _this.active = false; _this.gameOver(); } else { var headTile = _this.getTile(head); // commit next move for the head tile _this.moves.unshift(_this.direction); // update all coordinates using the corresponding history index _this.snek = _this.snek.map(function (tile, index) { return _this.getSnekMove(tile, _this.moves[index]); }); _this.getTile(_this.snek[0]).classList.add('head'); headTile.classList.remove('head'); if (tail) headTile.classList.add('tail'); if (_this.snekAteFood(_this.snek[0])) { // add length to snek if it ate if (tail) _this.snek.push(tail);else _this.snek.push(head); _this.food = _this.newFood(); } else if (tail) { // remove the last tile _this.getTile(tail).classList.remove('tail'); } } }, 300); } } }); }; Snek.prototype.gameOver = function gameOver() { var _this2 = this; var msg = [[1, 4], [1, 5], [1, 6], [1, 7], [1, 12], [1, 13], [1, 14], [1, 15], [2, 3], [2, 8], [2, 11], [2, 16], [3, 3], [3, 6], [3, 8], [3, 11], [3, 16], [4, 4], [4, 6], [4, 7], [4, 12], [4, 13], [4, 14], [4, 15], [6, 4], [6, 5], [6, 6], [6, 7], [6, 8], [6, 11], [6, 12], [6, 13], [6, 14], [6, 15], [7, 3], [7, 6], [7, 16], [8, 3], [8, 6], [8, 16], [9, 4], [9, 5], [9, 6], [9, 7], [9, 8], [9, 11], [9, 12], [9, 13], [9, 14], [9, 15], [11, 3], [11, 4], [11, 5], [11, 6], [11, 7], [11, 8], [11, 11], [11, 12], [11, 13], [11, 14], [11, 15], [11, 16], [12, 4], [12, 5], [12, 11], [12, 14], [12, 16], [13, 4], [13, 5], [13, 11], [13, 14], [13, 16], [14, 3], [14, 4], [14, 5], [14, 6], [14, 7], [14, 8], [14, 11], [14, 16], [16, 3], [16, 4], [16, 5], [16, 6], [16, 7], [16, 8], [16, 11], [16, 12], [16, 13], [16, 14], [16, 15], [16, 16], [17, 3], [17, 6], [17, 8], [17, 11], [17, 14], [18, 3], [18, 6], [18, 8], [18, 11], [18, 14], [19, 3], [19, 8], [19, 12], [19, 13], [19, 15], [19, 16]]; var reset = function reset() { _this2.reset(_this2.grid, reset); }; clearInterval(this.process); this.msg.innerHTML = 'Click to start over'; msg.forEach(function (coords) { _this2.getTile(coords).classList.add('dead'); }); this.grid.addEventListener('click', reset); }; Snek.prototype.reset = function reset(grid, _reset) { grid.removeEventListener('click', _reset); game = new Snek(); }; return Snek;
}();
var game = new Snek();
Developer | Nicolas Udy |
Username | udyux |
Uploaded | August 10, 2022 |
Rating | 4 |
Size | 7,851 Kb |
Views | 38,456 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Inverted border image | 3,808 Kb |
CSSkateboard loading screen | 4,482 Kb |
CSS Kiwi | 4,883 Kb |
MiniMarkdown.JS | 7,647 Kb |
Reset | 4,318 Kb |
Automatic SVG Pie-Charts | 4,466 Kb |
Pure CSS mountains | 4,178 Kb |
CSS only Select | 4,871 Kb |
Pure CSS Gallery hover effect | 4,802 Kb |
Simple Stopwatch | 4,799 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Knockout Lists | Marinru | 2,531 Kb |
Improve | Gavra | 1,652 Kb |
Responsive Minimal Blog Layout | Hackthevoid | 5,261 Kb |
Menu | Vivi_Lai | 1,210 Kb |
Calendar | Miroot | 2,033 Kb |
Z-index demo | Kblh | 1,534 Kb |
SVG email test v2.0 | M_J_Robbins | 2,090 Kb |
A Pen by Bryan Rojas | Bryanrojas | 1,873 Kb |
Under construction | GhostRider | 1,642 Kb |
Polygon Logo in CSS | Kai | 3,412 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!