Uživatel:Stevek55: Porovnání verzí

Skočit na navigaci Skočit na vyhledávání
Odebráno 3 126 bajtů ,  10. 1. 2022
Obsah stránky nahrazen textem „Jsem Pražák..“
(Založena nová stránka s textem „Jsem Pražák..<syntaxhighlight lang="html"> <!DOCTYPE html> <html> <head> <title></title> <style> html, body { height: 100%; margin: 0; }…“)
 
(Obsah stránky nahrazen textem „Jsem Pražák..“)
značky: nahrazeno editace z Vizuálního editoru
Jsem Pražák..<syntaxhighlight lang="html">
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
html, body {
height: 100%;
margin: 0;
}
 
body {
background: blue;
display: flex;
align-items: center;
justify-content: center;
}
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas width="600" height="600" 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,
cells: [],
maxCells: 4
};
var apple = {
x: 320,
y: 320
};
 
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
 
 
function loop() {
requestAnimationFrame(loop);
 
if (++count < 4) {
return;
}
 
count = 0;
context.clearRect(0,0,canvas.width,canvas.height);
 
snake.x += snake.dx;
snake.y += snake.dy;
 
if (snake.x < 0) {
snake.x = canvas.width - grid;
}
else if (snake.x >= canvas.width) {
snake.x = 0;
}
if (snake.y < 0) {
snake.y = canvas.height - grid;
}
else if (snake.y >= canvas.height) {
snake.y = 0;
}
 
snake.cells.unshift({x: snake.x, y: snake.y});
 
if (snake.cells.length > snake.maxCells) {
snake.cells.pop();
}
 
 
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;
}
}
});
}
 
 
document.addEventListener('keydown', function(e) {
// 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;
}
});
 
 
requestAnimationFrame(loop);
</script>
</body>
</html>
</syntaxhighlight>
34

editací

Navigační menu