SHOWING: First 60
0.297078.bitmap#18,082,725text1SAT_P2PKH OP_IF "ord" OP_1 "text/html;charset=utf8" OP_0 <<!DOCTYPE html>
<html>
<head>
<title>Snake</title>
<style>
html, body {
height: 100%;
margin: 0;
}
body {
background: black;
display: flex;
align-items: center;
justify-content: center;
}
canvas {
border: 1px solid orange;
}
</style>
</head>
<body>
<h1>Snake</h1>
<div id="score">
<p>Score: <span id="current-score">0</span></p>
<p>Highscore: <span id="high-score">0</span></p>
<canvas width="400" height="400" id="game"></canvas>
<script>
var canvas = document.getElementById('game');
var context = canvas.getContext('2d');
var grid = 16;
var count = 0;
var snake = {
x: 160,
y: 160,
// snake velocity. moves one grid length every frame in either the x or y direction
dx: grid,
dy: 0,
// keep track of all grids the snake body occupies
cells: [],
// length of the snake. grows when eating an apple
maxCells: 4
};
var apple = {
x: 320,
y: 320
};
// get random whole numbers in a specific range
// @see https://stackoverflow.com/a/1527820/2124254
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
// game loop
function loop() {
requestAnimationFrame(loop);
// slow game loop to 15 fps instead of 60 (60/15 = 4)
if (++count < 4) {
return;
}
count = 0;
context.clearRect(0,0,canvas.width,canvas.height);
// move snake by it's velocity
snake.x += snake.dx;
snake.y += snake.dy;
// wrap snake position horizontally on edge of screen
if (snake.x < 0) {
snake.x = canvas.width - grid;
}
else if (snake.x >= canvas.width) {
snake.x = 0;
}
// wrap snake position vertically on edge of screen
if (snake.y < 0) {
snake.y = canvas.height - grid;
}
else if (snake.y >= canvas.height) {
snake.y = 0;
}
// keep track of where snake has been. front of the array is always the head
snake.cells.unshift({x: snake.x, y: snake.y});
// remove cells as we move away from them
if (snake.cells.length > snake.maxCells) {
snake.cells.pop();
}
// draw apple
context.fillStyle = 'orange';
context.fillRect(apple.x, apple.y, grid-1, grid-1);
// draw snake one cell at a time
context.fillStyle = 'violet';
snake.cells.forEach(function(cell, index) {
// drawing 1 px smaller than the grid creates a grid effect in the snake body so you can see how long it is
context.fillRect(cell.x, cell.y, grid-1, grid-1);
// snake ate apple
if (cell.x === apple.x && cell.y === apple.y) {
snake.maxCells++;
// canvas is 400x400 which is 25x25 grids
apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;
}
// check collision with all cells after this one (modified bubble sort)
for (var i = index + 1; i < snake.cells.length; i++) {
// snake occupies same space as a body part. reset game
if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
snake.x = 160;
snake.y = 160;
snake.cells = [];
snake.maxCells = 4;
snake.dx = grid;
snake.dy = 0;
apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;
}
}
});
}
// listen to keyboard events to move the snake
document.addEventListener('keydown', function(e) {
// prevent snake from backtracking on itself by checking that it's
// not already moving on the same axis (pressing left while moving
// left won't do anything, and pressing right while moving left
// shouldn't let you collide with your own body)
// left arrow key
if (e.which === 37 && snake.dx === 0) {
snake.dx = -grid;
snake.dy = 0;
}
// up arrow key
else if (e.which === 38 && snake.dy === 0) {
snake.dy = -grid;
snake.dx = 0;
}
// right arrow key
else if (e.which === 39 && snake.dx === 0) {
snake.dx = grid;
snake.dy = 0;
}
// down arrow key
else if (e.which === 40 && snake.dy === 0) {
snake.dy = grid;
snake.dx = 0;
}
});
// start the game
requestAnimationFrame(loop);
</script>
</body>
</html>> OP_ENDIF#16,618,793text<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
html, body {
height: 100%;
margin: 0;
}
body {
background: black;
display: flex;
align-items: center;
justify-content: center;
}
canvas {
border: 1px solid white;
}
</style>
</head>
<body>
<canvas width="400" height="400" id="game"></canvas>
<script>
var canvas = document.getElementById('game');
var context = canvas.getContext('2d');
var grid = 16;
var count = 0;
var snake = {
x: 160,
y: 160,
// snake velocity. moves one grid length every frame in either the x or y direction
dx: grid,
dy: 0,
// keep track of all grids the snake body occupies
cells: [],
// length of the snake. grows when eating an apple
maxCells: 4
};
var apple = {
x: 320,
y: 320
};
// get random whole numbers in a specific range
// @see https://stackoverflow.com/a/1527820/2124254
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
// game loop
function loop() {
requestAnimationFrame(loop);
// slow game loop to 15 fps instead of 60 (60/15 = 4)
if (++count < 4) {
return;
}
count = 0;
context.clearRect(0,0,canvas.width,canvas.height);
// move snake by it's velocity
snake.x += snake.dx;
snake.y += snake.dy;
// wrap snake position horizontally on edge of screen
if (snake.x < 0) {
snake.x = canvas.width - grid;
}
else if (snake.x >= canvas.width) {
snake.x = 0;
}
// wrap snake position vertically on edge of screen
if (snake.y < 0) {
snake.y = canvas.height - grid;
}
else if (snake.y >= canvas.height) {
snake.y = 0;
}
// keep track of where snake has been. front of the array is always the head
snake.cells.unshift({x: snake.x, y: snake.y});
// remove cells as we move away from them
if (snake.cells.length > snake.maxCells) {
snake.cells.pop();
}
// draw apple
context.fillStyle = 'red';
context.fillRect(apple.x, apple.y, grid-1, grid-1);
// draw snake one cell at a time
context.fillStyle = 'green';
snake.cells.forEach(function(cell, index) {
// drawing 1 px smaller than the grid creates a grid effect in the snake body so you can see how long it is
context.fillRect(cell.x, cell.y, grid-1, grid-1);
// snake ate apple
if (cell.x === apple.x && cell.y === apple.y) {
snake.maxCells++;
// canvas is 400x400 which is 25x25 grids
apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;
}
// check collision with all cells after this one (modified bubble sort)
for (var i = index + 1; i < snake.cells.length; i++) {
// snake occupies same space as a body part. reset game
if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
snake.x = 160;
snake.y = 160;
snake.cells = [];
snake.maxCells = 4;
snake.dx = grid;
snake.dy = 0;
apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;
}
}
});
}
// listen to keyboard events to move the snake
document.addEventListener('keydown', function(e) {
// prevent snake from backtracking on itself by checking that it's
// not already moving on the same axis (pressing left while moving
// left won't do anything, and pressing right while moving left
// shouldn't let you collide with your own body)
// left arrow key
if (e.which === 37 && snake.dx === 0) {
snake.dx = -grid;
snake.dy = 0;
}
// up arrow key
else if (e.which === 38 && snake.dy === 0) {
snake.dy = -grid;
snake.dx = 0;
}
// right arrow key
else if (e.which === 39 && snake.dx === 0) {
snake.dx = grid;
snake.dy = 0;
}
// down arrow key
else if (e.which === 40 && snake.dy === 0) {
snake.dy = grid;
snake.dx = 0;
}
});
// start the game
requestAnimationFrame(loop);
</script>
</body>
</html>#15,276,389text<!DOCTYPE html>
<html>
<head>
<title>BTC Snake</title>
<style>
#game-board {
width: 400px;
height: 400px;
background-color: #333;
position: relative;
}
.snake-block {
width: 20px;
height: 20px;
background-color: #fff;
position: absolute;
}
#food {
width: 20px;
height: 20px;
background-color: #f00;
position: absolute;
}
#menu {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
display: none;
}
#menu button {
margin: 10px;
padding: 10px 20px;
font-size: 16px;
}
</style>
</head>
<body>
<h1>Snake</h1>
<div id="game-board"></div>
<div id="menu">
<h2>Menu</h2>
<button onclick="startGame()">Start</button>
<button onclick="pauseGame()">Pause</button>
<button onclick="resetGame()">Reset</button>
</div>
<script>
// Spielvariablen
var gameBoard = document.getElementById('game-board');
var menu = document.getElementById('menu');
var blockSize = 20;
var widthInBlocks = gameBoard.offsetWidth / blockSize;
var heightInBlocks = gameBoard.offsetHeight / blockSize;
var score = 0;
var gameLoop;
var gamePaused = false;
// Snake
var snake = [
{x: widthInBlocks / 2, y: heightInBlocks / 2}
];
// Essen
var food = {
x: Math.floor(Math.random() * widthInBlocks),
y: Math.floor(Math.random() * heightInBlocks)
};
// Spiel starten
function startGame() {
menu.style.display = 'none';
gameLoop = setInterval(function() {
if (!gamePaused) {
moveSnake();
drawSnake();
checkCollision();
}
}, 100);
}
// Spiel pausieren
function pauseGame() {
gamePaused = !gamePaused;
}
// Spiel zurücksetzen
function resetGame() {
clearInterval(gameLoop);
snake = [{x: widthInBlocks / 2, y: heightInBlocks / 2}];
score = 0;
food = {
x: Math.floor(Math.random() * widthInBlocks),
y: Math.floor(Math.random() * heightInBlocks)
};
gamePaused = false;
menu.style.display = 'block';
}
// Schlange bewegen
function moveSnake() {
var head = {
x: snake[0].x + 1,
y: snake[0].y
};
snake.unshift(head);
if (head.x === food.x && head.y === food.y) {
score++;
createFood();
} else {
snake.pop();
}
}
// Schlange zeichnen
function drawSnake() {
gameBoard.innerHTML = '';
snake.forEach(function(block) {
var snakeBlock = document.createElement('div');
snakeBlock.className = 'snake-block';
snakeBlock.style.left = block.x * blockSize + 'px';
snakeBlock.style.top = block.y * blockSize + 'px';
gameBoard.appendChild(snakeBlock);
});
var foodBlock = document.createElement('div');
foodBlock.id = 'food';
foodBlock.style.left = food.x * blockSize + 'px';
foodBlock.style.top = food.y * blockSize + 'px';
gameBoard.appendChild(foodBlock);
}
// Kollision überprüfen
function checkCollision() {
var head = snake[0];
// Kollision mit den Wänden
if (head.x < 0 || head.x === widthInBlocks || head.y < 0 || head.y === heightInBlocks) {
gameOver();
}
// Kollision mit dem Körper
for (var i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
gameOver();
}
}
}
// Spiel beenden
function gameOver() {
clearInterval(gameLoop);
alert('Game Over! Score: ' + score);
resetGame();
}
// Essen erstellen
function createFood() {
food = {
x: Math.floor(Math.random() * widthInBlocks),
y: Math.floor(Math.random() * heightInBlocks)
};
// Überprüfen, ob das Essen auf der Schlange generiert wurde
for (var i = 0; i < snake.length; i++) {
if (food.x === snake[i].x && food.y === snake[i].y) {
createFood();
break;
}
}
}
// Steuerung
document.addEventListener('keydown', function(event) {
var direction = event.keyCode;
// Pfeiltaste links
if (direction === 37 && snake[0].x !== snake[1].x + 1) {
snake[0].x -= 1;
}
// Pfeiltaste oben
else if (direction === 38 && snake[0].y !== snake[1].y + 1) {
snake[0].y -= 1;
}
// Pfeiltaste rechts
else if (direction === 39 && snake[0].x !== snake[1].x - 1) {
snake[0].x += 1;
}
// Pfeiltaste unten
else if (direction === 40 && snake[0].y !== snake[1].y - 1) {
snake[0].y += 1;
}
});
</script>
</body>
</html>#15,276,236text<!DOCTYPE html>
<html>
<head>
<title>Snake</title>
<style>
#game-board {
width: 400px;
height: 400px;
background-color: #333;
position: relative;
}
.snake-block {
width: 20px;
height: 20px;
background-color: #fff;
position: absolute;
}
#food {
width: 20px;
height: 20px;
background-color: #f00;
position: absolute;
}
</style>
</head>
<body>
<h1>Snake</h1>
<div id="game-board"></div>
<script>
// Spielvariablen
var gameBoard = document.getElementById('game-board');
var blockSize = 20;
var widthInBlocks = gameBoard.offsetWidth / blockSize;
var heightInBlocks = gameBoard.offsetHeight / blockSize;
var score = 0;
// Snake
var snake = [
{x: widthInBlocks / 2, y: heightInBlocks / 2}
];
// Essen
var food = {
x: Math.floor(Math.random() * widthInBlocks),
y: Math.floor(Math.random() * heightInBlocks)
};
// Spiel starten
var gameLoop = setInterval(function() {
moveSnake();
drawSnake();
checkCollision();
}, 100);
// Schlange bewegen
function moveSnake() {
var head = {
x: snake[0].x + 1,
y: snake[0].y
};
snake.unshift(head);
if (head.x === food.x && head.y === food.y) {
score++;
createFood();
} else {
snake.pop();
}
}
// Schlange zeichnen
function drawSnake() {
gameBoard.innerHTML = '';
snake.forEach(function(block) {
var snakeBlock = document.createElement('div');
snakeBlock.className = 'snake-block';
snakeBlock.style.left = block.x * blockSize + 'px';
snakeBlock.style.top = block.y * blockSize + 'px';
gameBoard.appendChild(snakeBlock);
});
var foodBlock = document.createElement('div');
foodBlock.id = 'food';
foodBlock.style.left = food.x * blockSize + 'px';
foodBlock.style.top = food.y * blockSize + 'px';
gameBoard.appendChild(foodBlock);
}
// Kollision überprüfen
function checkCollision() {
var head = snake[0];
// Kollision mit den Wänden
if (head.x < 0 || head.x === widthInBlocks || head.y < 0 || head.y === heightInBlocks) {
gameOver();
}
// Kollision mit dem Körper
for (var i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
gameOver();
}
}
}
// Spiel beenden
function gameOver() {
clearInterval(gameLoop);
alert('Game Over! Score: ' + score);
}
// Essen erstellen
function createFood() {
food = {
x: Math.floor(Math.random() * widthInBlocks),
y: Math.floor(Math.random() * heightInBlocks)
};
// Überprüfen, ob das Essen auf der Schlange generiert wurde
for (var i = 0; i < snake.length; i++) {
if (food.x === snake[i].x && food.y === snake[i].y) {
createFood();
break;
}
}
}
// Steuerung
document.addEventListener('keydown', function(event) {
var direction = event.keyCode;
// Pfeiltaste links
if (direction === 37 && snake[0].x !== snake[1].x + 1) {
snake[0].x -= 1;
}
// Pfeiltaste oben
else if (direction === 38 && snake[0].y !== snake[1].y + 1) {
snake[0].y -= 1;
}
// Pfeiltaste rechts
else if (direction === 39 && snake[0].x !== snake[1].x - 1) {
snake[0].x += 1;
}
// Pfeiltaste unten
else if (direction === 40 && snake[0].y !== snake[1].y - 1) {
snake[0].y += 1;
}
});
</script>
</body>
</html>#15,276,229text