Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- temp=0
- System: You are a helpful assistant
- > write me a snake game as a single html file.
- Sure, here's a simple implementation of the classic Snake game using HTML, CSS, and JavaScript. This example assumes you have basic knowledge of HTML, CSS, and JavaScript.
- ```html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Snake Game</title>
- <style>
- body {
- display: flex;
- justify-content: center;
- align-items: center;
- height: 100vh;
- margin: 0;
- background-color: #f0f0f0;
- }
- canvas {
- border: 1px solid #000;
- }
- </style>
- </head>
- <body>
- <canvas id="gameCanvas" width="400" height="400"></canvas>
- <script>
- const canvas = document.getElementById('gameCanvas');
- const context = canvas.getContext('2d');
- const snake = [];
- const food = [];
- const direction = { x: 0, y: 0 };
- const snakeLength = 1;
- const gameOver = false;
- function drawSnake() {
- context.clearRect(0, 0, canvas.width, canvas.height);
- for (let i = snake.length - 1; i >= 0; i--) {
- context.beginPath();
- context.moveTo(snake[i].x, snake[i].y);
- context.lineTo(snake[i].x, snake[i].y);
- context.stroke();
- }
- }
- function drawFood() {
- context.beginPath();
- context.arc(food[0].x, food[0].y, 5, 0, Math.PI * 2);
- context.fill();
- }
- function update() {
- context.clearRect(0, 0, canvas.width, canvas.height);
- drawSnake();
- drawFood();
- context.save();
- context.beginPath();
- context.moveTo(direction.x, direction.y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- context.lineTo(direction.x + snake[0].x, direction.y + snake[0].y);
- Ctrl+C pressed, exiting...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement