Heavy Snake

Developer
Size
6,358 Kb
Views
26,312

How do I make an heavy snake?

For reddit.com/r/javascriptstudygrou. What is a heavy snake? How do you make a heavy snake? This script and codes were developed by Steven on 31 July 2022, Sunday.

Heavy Snake Previews

Heavy Snake - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Heavy Snake</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <!-- Instruction bar ruthlessly stolen from https://www.reddit.com/user/senocular -->
<div id="instructions"> Collect Melons, Avoid Spikes, Arrows to Move, 'r' to reset. <a href="#" onclick="this.parentNode.remove()">OK</a>
</div>
<h1>Heavy Snake</h1>
<h2 id="level"></h2>
<canvas id="scene"></canvas>
<canvas id="actor"></canvas>
<canvas id="particles"></canvas> <script src="js/index.js"></script>
</body>
</html>

Heavy Snake - Script Codes CSS Codes

html, body { height: 100%; margin: 0; text-align: center; background: rgba(50, 150, 200, 1); overflow: hidden;
}
h1 { margin-top: 0px; margin-bottom: 0px; font-size: 48px; color:white;
}
h2 { margin-top: -5px; margin-bottom: 0px; font-size: 25px; color: white;
}
canvas { border: solid 0.5px gray; margin-top: 0px; padding-bottom: -21px; margin-bottom: -21px; position: absolute;
}
#scene { /* background: url(http://volv.org/images/grass.jpg) */ /* background: rgba(50, 150, 200, 0.5); */
}
#instructions { position: absolute; top: 100px; left: 50%; box-sizing: border-box; width: 440px; padding: 0.5em; margin-left: -220px; border: 2px solid black; border-radius: 8px; background-color: #555; text-align: center; font-size: 14px; font-family: "Trebuchet MS", Helvetica, sans-serif; color: white; box-shadow: 5px 5px 5px 0px rgba(0,0,0,0.25); z-index: 40;
}
#instructions a { color: white; font-weight: bold;
}

Heavy Snake - Script Codes JS Codes

// Tileset credits - http://opengameart.org/users/bleddyuffles
// Game inspiration - http://www.lessmilk.com/
// Watermelon - http://opengameart.org/content/watermelos-piece
// Spikes - http://opengameart.org/content/spikey-stuff
// Created for https://www.reddit.com/r/javaScriptStudyGroup/
// 0 - Air, 3 - Grass tuft (Can walk through),
// 1 - Ground, 2 - Ground with grass,
// 4 - Watermelon, 7 - Watermelon + Tuft
// 5 - Spike, 6 - Spike + Tuft
// 8 - Slant Up-Right, 9 - Slant Up-left. Barriers
// 10 - Secret Wall (Regular Ground Graphic), Walkable.
var levels = [ { coords: [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 2, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 2, 0], [0, 2, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 0], [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0], [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0], [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0], [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0], [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0], [0, 2, 0, 3, 0, 0, 0, 0, 7, 3, 3, 0, 3, 2, 0], [0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], playerPosition: [ [3, 7], [3, 8], [3, 9], [3, 10], [3, 11], ] }, { coords: [ [0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0], [0, 2, 0, 0,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2, 2, 2, 0, 2, 0, 2, 2, 0, 2, 2, 0], [0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 0, 2, 2, 0], [0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2, 0, 2, 2, 0], [0, 2, 0, 2, 2, 0, 2, 2, 0, 0, 0, 0, 2, 2, 0], [0, 2, 0, 4, 0, 0, 2, 2, 2, 2, 2, 0, 2, 2, 0], [0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0], [0, 2, 2, 0, 2, 0, 0, 2, 2, 0, 2, 2, 0, 2, 0], [0, 2, 2, 0, 2, 7, 0, 2, 2, 0, 7, 0, 0, 2, 0], [0, 2, 2, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], playerPosition: [ [3, 9], [3, 10], [3, 11], ] }
];
(() => { let game = { width: 0, height: 0, tSize: 0, ready: true, // For Input over: false, levelNo: 0, player: { dir: "up" }, tiles: {}, scene: { elem: document.getElementById("scene"), ctx: document.getElementById("scene").getContext("2d") }, actor: { elem: document.getElementById("actor"), ctx: document.getElementById("actor").getContext("2d") }, particles: { elem: document.getElementById("particles"), ctx: document.getElementById("particles").getContext("2d") }, init(level) { let canvases = document.querySelectorAll("canvas") Array.from(canvases).forEach(item => { // Resive all canvas' item.height = window.innerHeight * .85; item.width = item.height; // window.innerWidth * .9; item.style.left = `${(window.innerWidth-item.width)/2}px`; // Center item.style.display = "block"; }); game.width = game.scene.elem.width; game.height = game.scene.elem.height; game.loadLevel(level); game.over = false; game.ready = true; game.superNotReady = false; // Massively hacky - Stops movement after win/loss game.doText(); window.addEventListener("keydown", game.handleKeyDown); }, loadLevel(level) { if (game.over) { // Game over. Redo map from initial coords game.level.coords = []; game.level.initial.forEach((each, i, arr) => { game.level.coords.push([...game.level.initial[i]]) }); } else { // First Load. game.level = level; game.level.initial = []; game.level.coords.forEach((each, i, arr) => { game.level.initial.push([...game.level.coords[i]]) }); } game.melonCount = 0; // Try removing next line and see what happens. game.scene.ctx.imageSmoothingEnabled = false; // Kills anti aliasing game.tSize = game.width / game.level.coords.length; // How big is a tile. Square maps. game.player.position = Array.from(game.level.playerPosition); game.drawLevel(); // Draw game.drawSnake(); }, drawLevel() { let tSize = game.tSize; let grassTiles = game.tiles.grassTiles; let fruit = game.tiles.fruit; let spike = game.tiles.spike; let coords = game.level.initial; for (let i = 0; i < coords.length; i++) { for (let j = 0; j < coords[i].length; j++) { if (coords[j][i] === 1) { // Grassy Ground game.scene.ctx.drawImage(grassTiles, 17, 17, 16, 16, i * tSize, j * tSize, tSize, tSize); continue; } if (coords[j][i] === 10) { // Fake Ground shhh game.scene.ctx.drawImage(grassTiles, 0, 17, 16, 16, i * tSize, j * tSize, tSize, tSize); continue; } if (coords[j][i] === 2) { // Ground game.scene.ctx.drawImage(grassTiles, 0, 17, 16, 16, i * tSize, j * tSize, tSize, tSize); continue; } if (coords[j][i] === 3) { // Tuft // Drawn on particle layer. For now game.particles.ctx.drawImage(grassTiles, 17, 0, 16, 16, i * tSize, j * tSize, tSize, tSize); continue; } if (coords[j][i] === 4) { // Watermelon game.scene.ctx.drawImage(fruit, 0, 0, 274, 274, i * tSize, j * tSize, tSize, tSize); game.melonCount++; continue; } if (coords[j][i] === 5) { // Spike game.scene.ctx.drawImage(spike, 0, 0, 40, 40, i * tSize, j * tSize, tSize, tSize); continue; } if (coords[j][i] === 6) { // Spike & Tuft game.particles.ctx.drawImage(grassTiles, 17, 0, 16, 16, i * tSize, j * tSize, tSize, tSize); game.scene.ctx.drawImage(spike, 0, 0, 40, 40, i * tSize, j * tSize, tSize, tSize); continue; } if (coords[j][i] === 7) { // Watermelon & Tuft game.particles.ctx.drawImage(grassTiles, 17, 0, 16, 16, i * tSize, j * tSize, tSize, tSize); game.scene.ctx.drawImage(fruit, 0, 0, 274, 274, i * tSize, j * tSize, tSize, tSize); game.melonCount++; continue; } if (coords[j][i] === 8) { // Slant Right game.scene.ctx.drawImage(grassTiles, 17, 34, 16, 16, i * tSize, j * tSize, tSize, tSize); continue; } if (coords[j][i] === 9) { // Slant Left game.scene.ctx.drawImage(grassTiles, 0, 34, 16, 16, i * tSize, j * tSize, tSize, tSize); continue; } } } }, drawSnake(fill = "rgba(100, 255, 0, 1)") { let tSize = game.tSize; let seg = game.player.position; game.actor.ctx.clearRect(0, 0, game.width, game.height); game.actor.ctx.fillStyle = fill; game.actor.ctx.fillRect(seg[0][0] * tSize, seg[0][1] * tSize, tSize - 2, tSize - 2); game.actor.ctx.fillStyle = "black"; // Gets a hard won face // Not sure I like his tongue. if (game.player.dir === "left") { game.actor.ctx.fillRect((seg[0][0] * tSize) + tSize * .2, (seg[0][1] * tSize) + tSize * 0.2, tSize * .2, tSize * .2); game.actor.ctx.fillRect((seg[0][0] * tSize) + tSize * .2, (seg[0][1] * tSize) + tSize * 0.6, tSize * .2, tSize * .2); game.actor.ctx.fillStyle = "red"; game.actor.ctx.fillRect((seg[0][0] * tSize) - tSize * .2, (seg[0][1] * tSize) + tSize * 0.45, tSize * .5, tSize * .1); } if (game.player.dir === "up") { game.actor.ctx.fillRect((seg[0][0] * tSize) + tSize * .2, (seg[0][1] * tSize) + tSize * 0.2, tSize * .2, tSize * .2); game.actor.ctx.fillRect((seg[0][0] * tSize) + tSize * .6, (seg[0][1] * tSize) + tSize * 0.2, tSize * .2, tSize * .2); game.actor.ctx.fillStyle = "red"; game.actor.ctx.fillRect((seg[0][0] * tSize) + tSize * .45, (seg[0][1] * tSize) - tSize * 0.2, tSize * .1, tSize * .5); } if (game.player.dir === "right") { game.actor.ctx.fillRect((seg[0][0] * tSize) + tSize * .6, (seg[0][1] * tSize) + tSize * 0.2, tSize * .2, tSize * .2); game.actor.ctx.fillRect((seg[0][0] * tSize) + tSize * .6, (seg[0][1] * tSize) + tSize * 0.6, tSize * .2, tSize * .2); game.actor.ctx.fillStyle = "red"; game.actor.ctx.fillRect((seg[0][0] * tSize) + tSize * .6, (seg[0][1] * tSize) + tSize * 0.45, tSize * .5, tSize * .1); } if (game.player.dir === "down") { game.actor.ctx.fillRect((seg[0][0] * tSize) + tSize * .2, (seg[0][1] * tSize) + tSize * 0.6, tSize * .2, tSize * .2); game.actor.ctx.fillRect((seg[0][0] * tSize) + tSize * .6, (seg[0][1] * tSize) + tSize * 0.6, tSize * .2, tSize * .2); game.actor.ctx.fillStyle = "red"; game.actor.ctx.fillRect((seg[0][0] * tSize) + tSize * .45, (seg[0][1] * tSize) + tSize * 0.8, tSize * .1, tSize * .5); } game.actor.ctx.fillStyle = fill; for (let i = 1; i < seg.length; i++) { game.actor.ctx.fillRect(seg[i][0] * tSize, seg[i][1] * tSize, tSize - 2, tSize - 2); } if (!game.over) setTimeout(game.checkPosition, 100); }, handleKeyDown(e) { if (!game.ready) return; // Too soon for next input if (game.superNotReady) return; // Massively hacky let player = game.player.position; let move = false; let moveTo = []; if (e.keyCode === 82) { // r for reset game.over = true; game.init(levels[game.levelNo]); game.drawSnake() } if (e.keyCode === 37) { // Left moveTo = [player[0][0] - 1, player[0][1]]; if (game.moveCheck(moveTo)) { player.unshift(moveTo); game.player.dir = "left"; move = true; } } if (e.keyCode === 38) { // Up moveTo = [player[0][0], player[0][1] - 1]; if (game.moveCheck(moveTo)) { player.unshift(moveTo); game.player.dir = "up"; move = true; } } if (e.keyCode === 39) { // Right moveTo = [player[0][0] + 1, player[0][1]]; if (game.moveCheck(moveTo)) { player.unshift(moveTo); game.player.dir = "right"; move = true; } } if (e.keyCode === 40) { // Down moveTo = [player[0][0], player[0][1] + 1]; if (game.moveCheck(moveTo)) { player.unshift(moveTo); game.player.dir = "down"; move = true; } } if (move) { game.ready = false; // Limit input game.hitFruit(moveTo); // Check for fruit hit (head on) game.drawSnake(); } }, hitFruit([x, y]) { let tSize = game.tSize; if (game.level.coords[y][x] === 4 || game.level.coords[y][x] === 7) { // Hit a fruit keep end of tail game.scene.ctx.clearRect(x * tSize, y * tSize, tSize, tSize); // Remove Fruit if (game.level.coords[y][x] === 7) { // Redraw Tuft game.particles.ctx.drawImage(game.tiles.grassTiles, 17, 0, 16, 16, x * tSize, y * tSize, tSize, tSize); } game.level.coords[y][x] = 0; game.melonCount--; } else { // Didn't hit - remove end of tail game.player.position.pop(); } }, moveCheck([x = 0, y = 0]) { // Valid move? Cant move into self or walls let validMove = true; let testBlock = game.level.coords[y][x]; if (testBlock === 1 || testBlock === 2 || testBlock === 8 || testBlock === 9 || testBlock === undefined) { // Walls validMove = false; } game.player.position.forEach((each) => { // Self if (each[0] === x && each[1] === y) { validMove = false; } }) return validMove; }, checkPosition() { // Logic/ Do gravity, find out if dead, check can Move? Check Win? if (game.superNotReady) return; // Massively hacky - Stops movement after win/loss let player = game.player.position; // Collected all melons let win = (game.melonCount === 0); if (win) { game.gameWin(); return; } let head = player[0]; // No valid move (Suffocate by snake) let posToCheck = [ // Up, Down, Left, Right // Added boundary checks [head[0], (head[1] + 1 < game.width) ? head[1] + 1 : game.width], [head[0], (head[1] - 1 >= 0) ? head[1] - 1 : 0], [(head[0] + 1 < game.width) ? head[0] + 1 : game.width, head[1]], [(head[0] - 1 >= 0) ? head[0] - 1 : 0, head[1]] // [head[0], head[1] + 1], // [head[0], head[1] - 1], // [head[0] + 1, head[1]], // [head[0] - 1, head[1]] ]; let suffocate = posToCheck.every((surround) => { if (game.level.coords[surround[1]] === undefined) { game.gameOver(); // Fell } return (game.level.coords[surround[1]][surround[0]] === 1 || // Ground game.level.coords[surround[1]][surround[0]] === 2 || // Ground With Grass game.level.coords[surround[1]][surround[0]] === 8 || // Slant Right game.level.coords[surround[1]][surround[0]] === 9 || // Slant Left player.some((each) => { // Other bits of snake return (each[0] === surround[0] && each[1] === surround[1]) })); }) if (suffocate) { game.gameOver(); return; } // Spiked let spiked = player.some((each) => { // Check at least one block is on a spike return (game.level.coords[each[1]][each[0]] === 5 || game.level.coords[each[1]][each[0]] === 6); // Can stand on fruit }) if (spiked) { game.gameOver(); return; } // 'Gravity' let floating = !player.some((each) => { // Check at lreast one block has ground under it if (game.level.coords[each[1]+1] === undefined) return; return (game.level.coords[each[1] + 1][each[0]] === 1 || game.level.coords[each[1] + 1][each[0]] === 2 || game.level.coords[each[1] + 1][each[0]] === 8 || game.level.coords[each[1] + 1][each[0]] === 9 || game.level.coords[each[1] + 1][each[0]] === 4 || game.level.coords[each[1] + 1][each[0]] === 7); // Can stand on fruit }) if (floating) { player.forEach((each, i, arr) => { if (game.level.coords[each[0]][each[1]+1] === undefined) { game.gameOver(); // Fell } arr[i] = [each[0], each[1] + 1]; // Move each block down 1 }); game.drawSnake(); // Redraw return; } game.ready = true; // Accept input again }, gameOver() { game.ready = false; game.over = true; game.superNotReady = true; // Massively hacky game.drawSnake("rgba(255, 0, 0, 1)") setTimeout(game.drawSnake, 200); setTimeout(game.drawSnake, 400, "rgba(255, 0, 0, 1)"); setTimeout(game.drawSnake, 600); setTimeout(game.drawSnake, 800, "rgba(255, 0, 0, 1)"); setTimeout(game.init, 1000, game.level) // Restart }, gameWin() { game.ready = false; game.over = false; game.superNotReady = true; // Massively hacky game.melonCount = 1000; game.drawSnake("rgba(255, 255, 0, 1)") // Reset level for next time round game.level.coords = []; game.level.initial.forEach((each, i, arr) => { game.level.coords.push([...game.level.initial[i]]) }); // Flash, win setTimeout(game.drawSnake, 200); setTimeout(game.drawSnake, 400, "rgba(255, 255, 0, 1)"); setTimeout(game.drawSnake, 600); setTimeout(game.drawSnake, 800, "rgba(255, 255, 0, 1)"); game.levelNo = (game.levelNo < levels.length-1) ? game.levelNo + 1 : 0; setTimeout(game.init, 1000, levels[game.levelNo]) // Next Level }, doText() { game.particles.ctx.fillStyle = "white"; game.particles.ctx.textAlign = "center"; document.getElementById("level").innerHTML = `Lvl ${game.levelNo+1} / ${levels.length}`; } } let canvases = document.querySelectorAll("canvas") Array.from(canvases).forEach(item => { // Resize all canvas' item.style.display = "none"; }); // Should stop random image failing let tracker = imgLoad() game.tiles.grassTiles = new Image(); game.tiles.grassTiles.onload = tracker; game.tiles.grassTiles.src = "http://volv.org/images/gsnake/grassTiles.png"; game.tiles.fruit = new Image(); game.tiles.fruit.onload = tracker; game.tiles.fruit.src = "http://volv.org/images/gsnake/watermelon.png"; game.tiles.spike = new Image(); game.tiles.spike.onload = tracker; game.tiles.spike.src = "http://volv.org/images/gsnake/spike.png"; function imgLoad() { // Load all assets. Then Start let count = 0; return () => { if (++count === 3) { game.init(levels[game.levelNo]) //game.init(levels[6]) //window.focus() } } }
// WHY U NO WORK
// function imgLoadTrack(noImages) { // Load all assets. Then Start
// let count = 0;
// return () => {
// if (++count === noImages) {
// game.init(levels[game.levelNo])
// //game.init(levels[6])
// //window.focus()
// }
// }
// }
// function addImage(where, src, name, callback) {
// where[name] = new Image();
// where[name].onload = callback();
// where[name].src = src;
// }
// let loadTracker = imgLoadTrack(3)
// addImage(game.tiles, "http://volv.org/images/gsnake/grassTiles.png", "grassTiles", loadTracker);
// addImage(game.tiles, "http://volv.org/images/gsnake/watermelon.png", "fruit", loadTracker);
// addImage(game.tiles, "http://volv.org/images/gsnake/spike.png", "spike", loadTracker); //setTimeout(game.init, 500, level) // Previous Ghetto image loader.. for posterity
})();
Heavy Snake - Script Codes
Heavy Snake - Script Codes
Home Page Home
Developer Steven
Username volv
Uploaded July 31, 2022
Rating 3
Size 6,358 Kb
Views 26,312
Do you need developer help for Heavy Snake?

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!

Steven (volv) Script Codes
Create amazing love letters with AI!

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!