o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
crea un gioco snake in html,javascript e css
- User
crea un gioco snake in html,javascript e css
- Assistant
crea un gioco snake in html,javascript e css
Certo! Ecco un semplice gioco Snake creato utilizzando HTML, CSS e JavaScript. Questo esempio ti permetterà di comprendere come funzionano le basi del gioco e potrai personalizzarlo ulteriormente secondo le tue esigenze.
<canvas>.Copia e incolla il seguente codice in un file con estensione .html (es. snake.html) e aprilo nel tuo browser per giocare.
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<title>Gioco Snake</title>
<style>
body {
background-color: #222;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
color: #fff;
font-family: Arial, sans-serif;
}
#gameCanvas {
background-color: #000;
border: 2px solid #fff;
}
#score {
position: absolute;
top: 20px;
left: 20px;
font-size: 24px;
}
</style>
</head>
<body>
<div id="score">Punteggio: 0</div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scale = 20; // Dimensione di ogni segmento del serpente e del cibo
const rows = canvas.height / scale;
const columns = canvas.width / scale;
let snake;
let food;
// Classe Serpente
class Snake {
constructor() {
this.x = Math.floor(columns / 2) * scale;
this.y = Math.floor(rows / 2) * scale;
this.xSpeed = scale * 1;
this.ySpeed = 0;
this.total = 0;
this.tail = [];
}
draw() {
ctx.fillStyle = "#00FF00";
for (let i=0; i<this.tail.length; i++) {
ctx.fillRect(this.tail[i].x, this.tail[i].y, scale, scale);
}
ctx.fillRect(this.x, this.y, scale, scale);
}
update() {
for (let i=0; i<this.tail.length -1; i++) {
this.tail[i] = this.tail[i+1];
}
this.tail[this.total -1] = { x: this.x, y: this.y };
this.x += this.xSpeed;
this.y += this.ySpeed;
// Controllo delle pareti (wrap-around)
if (this.x >= canvas.width) {
this.x = 0;
}
if (this.y >= canvas.height) {
this.y = 0;
}
if (this.x < 0) {
this.x = canvas.width - scale;
}
if (this.y < 0) {
this.y = canvas.height - scale;
}
}
changeDirection(direction) {
switch(direction) {
case 'Up':
if (this.ySpeed === 0) {
this.xSpeed = 0;
this.ySpeed = -scale * 1;
}
break;
case 'Down':
if (this.ySpeed === 0) {
this.xSpeed = 0;
this.ySpeed = scale * 1;
}
break;
case 'Left':
if (this.xSpeed === 0) {
this.xSpeed = -scale *1;
this.ySpeed = 0;
}
break;
case 'Right':
if (this.xSpeed === 0) {
this.xSpeed = scale *1;
this.ySpeed = 0;
}
break;
}
}
eat(food) {
if (this.x === food.x && this.y === food.y) {
this.total++;
return true;
}
return false;
}
checkCollision() {
for (let i=0; i<this.tail.length; i++) {
if (this.x === this.tail[i].x && this.y === this.tail[i].y) {
this.total = 0;
this.tail = [];
alert("Game Over! Il tuo punteggio è: " + score);
score = 0;
document.getElementById('score').innerText = "Punteggio: " + score;
}
}
}
}
// Classe Cibo
class Food {
constructor() {
this.x;
this.y;
this.pickLocation();
}
pickLocation() {
this.x = (Math.floor(Math.random() * columns -1) +1) * scale;
this.y = (Math.floor(Math.random() * rows -1) +1) * scale;
}
draw() {
ctx.fillStyle = "#FF0000";
ctx.fillRect(this.x, this.y, scale, scale);
}
}
let score = 0;
function init() {
snake = new Snake();
food = new Food();
score = 0;
document.getElementById('score').innerText = "Punteggio: " + score;
loop();
}
function loop() {
if (typeof gameLoop !== "undefined") clearInterval(gameLoop);
gameLoop = setInterval(() => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
food.draw();
snake.update();
snake.draw();
if (snake.eat(food)) {
score++;
document.getElementById('score').innerText = "Punteggio: " + score;
food.pickLocation();
}
snake.checkCollision();
}, 100); // Velocità del gioco (100 ms)
}
window.addEventListener('keydown', function(e) {
const direction = e.key.replace('Arrow', '');
snake.changeDirection(direction);
});
// Avvia il gioco
init();
</script>
</body>
</html>
<canvas> viene utilizzato per disegnare graficamente il serpente e il cibo.Snake gestisce la posizione, il movimento, la crescita e le collisioni del serpente.Food gestisce la posizione del cibo in modo casuale sulla griglia.localStorage.Spero che questo ti aiuti a creare e comprendere il gioco Snake! Se hai domande o necessiti di ulteriori personalizzazioni, non esitare a chiedere.
Not yet included in a published release.