Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!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>
- // Define the canvas and context
- const canvas = document.getElementById("game");
- const context = canvas.getContext("2d");
- // Define grid and count
- const grid = 16;
- let count = 0;
- // Define the Snake class
- class Snake {
- constructor() {
- this.x = 160;
- this.y = 160;
- this.dx = grid;
- this.dy = 0;
- this.cells = [];
- this.maxCells = 4;
- this.bullets = [];
- }
- // Update the position of the snake
- update() {
- this.x += this.dx;
- this.y += this.dy;
- if (this.x < 0) {
- this.x = canvas.width - grid;
- } else if (this.x >= canvas.width) {
- this.x = 0;
- }
- if (this.y < 0) {
- this.y = canvas.height - grid;
- } else if (this.y >= canvas.height) {
- this.y = 0;
- }
- this.cells.unshift({ x: this.x, y: this.y });
- if (this.cells.length > this.maxCells) {
- this.cells.pop();
- }
- }
- // Draw the snake on the canvas
- draw() {
- context.fillStyle = "green";
- this.cells.forEach((cell) => {
- context.fillRect(cell.x, cell.y, grid - 1, grid - 1);
- });
- }
- // Check if the snake has eaten an apple
- checkCollision() {
- for (let i = 1; i < this.cells.length; i++) {
- if (this.cells[0].x === this.cells[i].x && this.cells[0].y === this.cells[i].y) {
- this.x = 160;
- this.y = 160;
- this.cells = [];
- this.maxCells = 4;
- this.dx = grid;
- this.dy = 0;
- apple.x = getRandomInt(0, 25) * grid;
- apple.y = getRandomInt(0, 25) * grid;
- }
- }
- }
- // Shoot the apple
- shoot() {
- const bullet = { x: this.x, y: this.y, dx: this.dx, dy: this.dy, size: grid };
- this.bullets.push(bullet);
- }
- }
- // Define the Apple class
- class Apple {
- constructor() {
- this.x = 320;
- this.y = 320;
- }
- // Draw the apple on the canvas
- draw() {
- context.fillStyle = "red";
- context.fillRect(this.x, this.y, grid - 1, grid - 1);
- }
- // Check if the apple has been eaten by the snake
- checkCollision() {
- if (snake.cells[0].x === this.x && snake.cells[0].y === this.y) {
- snake.maxCells++;
- this.x = getRandomInt(0, 25) * grid;
- this.y = getRandomInt(0, 25) * grid;
- }
- }
- }
- // Create the snake and apple objects
- const snake = new Snake();
- const apple = new Apple();
- // Define the getRandomInt function
- function getRandomInt(min, max) {
- return Math.floor(Math.random() * (max - min)) + min;
- }
- // Main game loop
- function loop() {
- requestAnimationFrame(loop);
- // Draw and update bullets
- snake.bullets.forEach((bullet) => {
- context.fillStyle = "white";
- context.fillRect(bullet.x, bullet.y, bullet.size, bullet.size);
- bullet.x += bullet.dx;
- bullet.y += bullet.dy;
- if (
- bullet.x < 0 ||
- bullet.x > canvas.width ||
- bullet.y < 0 ||
- bullet.y > canvas.height
- ) {
- snake.bullets.splice(snake.bullets.indexOf(bullet), 1);
- } else if (bullet.x === apple.x && bullet.y === apple.y) {
- apple.x = getRandomInt(0, 25) * grid;
- apple.y = getRandomInt(0, 25) * grid;
- snake.bullets.splice(snake.bullets.indexOf(bullet), 1);
- }
- });
- // Update the snake's position
- count++;
- if (count < 4) {
- return;
- }
- count = 0;
- context.clearRect(0, 0, canvas.width, canvas.height);
- snake.update();
- snake.draw();
- apple.draw();
- snake.checkCollision();
- apple.checkCollision();
- }
- // Event listener for key presses
- document.addEventListener("keydown", (e) => {
- if (e.which === 37 && snake.dx === 0) {
- snake.dx = -grid;
- snake.dy = 0;
- } else if (e.which === 38 && snake.dy === 0) {
- snake.dy = -grid;
- snake.dx = 0;
- } else if (e.which === 39 && snake.dx === 0) {
- snake.dx = grid;
- snake.dy = 0;
- } else if (e.which === 40 && snake.dy === 0) {
- snake.dy = grid;
- snake.dx = 0;
- } else if (e.which === 32) { // Space bar
- snake.shoot();
- }
- });
- // Start the game loop
- requestAnimationFrame(loop);
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement