The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> I want to use a class for this JS code
fxckpain
post May 28 2020, 10:25 AM
Post #1





Group: Members
Posts: 3
Joined: 15-May 20
Member No.: 27,343



Hi I'm trying to do a Snake game code and I get this code online:

var canvas = document.getElementById('game');
var context = canvas.getContext('2d');
var grid = 16;
var count = 0;
var score=0;
var max=0;

var snake = {
x: 160,
y: 160,

// Velocità del serpente, si sposta di un blocco per x o y
dx: grid,
dy: 0,

// Tiene traccia dei movimenti del corpo del serpente
cells: [],

// Lunghezza del serpente che aumenta quando mangia una mela
maxCells: 4
};
var apple = {
x: 320,
y: 320
};

// Prende un numero randomico da un range impostato
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
// game loop
function loop() {
requestAnimationFrame(loop);

if (++count < 4) {
return;
}
count = 0;
context.clearRect(0,0,canvas.width,canvas.height);
// Muove il serpente in base alla sua velocità
snake.x += snake.dx;
snake.y += snake.dy;
// Effetto PacMan in orizzontale
if (snake.x < 0) {
snake.x = canvas.width - grid;
}
else if (snake.x >= canvas.width) {
snake.x = 0;
}

// Effetto PacMan in verticale
if (snake.y < 0) {
snake.y = canvas.height - grid;
}
else if (snake.y >= canvas.height) {
snake.y = 0;
}
// Tiene traccia dei movimenti del serpente ( Dove è stato )
snake.cells.unshift({x: snake.x, y: snake.y});

if (snake.cells.length > snake.maxCells) {
snake.cells.pop();
}
// Creazione della mela
context.fillStyle = 'red';
context.fillRect(apple.x, apple.y, grid-1, grid-1);
// Creazione del serpente per cella
context.fillStyle = 'green';
snake.cells.forEach(function(cell, index) {

// Per vedere quante celle è lungo il serpente ho diminuito la grandezza di 1px
context.fillRect(cell.x, cell.y, grid-1, grid-1);
// Il serpente che mangia la mela
if (cell.x === apple.x && cell.y === apple.y) {
snake.maxCells++;
score+=10;
//Punteggio massimo
document.getElementById('score').innerHTML=score;

apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;
}

for (var i = index + 1; i < snake.cells.length; i++)
{

// Controlla la collisione del corpo del serpente ( Se si scontra con se stesso restarta il gioco )
if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y)
{

if(score>max)
{
max=score;
}
snake.x = 160;
snake.y = 160;
snake.cells = [];
snake.maxCells = 4;
snake.dx = grid;
snake.dy = 0;
score=0;
apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;
document.getElementById('high').innerHTML=max;
}
}
}

);

}
// Keyboard Events per muovere con le freccie
document.addEventListener('keydown', function(e) {
// Questa funzione permette di evitare alcuni mal funzionamenti
// Se schiacci per esempio la freccia sinistra mentre il serpente
// sta gia andando a sinistra non dovrebbe succedere niente e lo stesso vale
// se schiacci la freccia destra evitando così la collisione (QUESTO VALE ANCHE PER LE ALTRE DIREZIONI )

// Freccia sinistra
if (e.which === 37 && snake.dx === 0) {
snake.dx = -grid;
snake.dy = 0;
}
// Freccia in su
else if (e.which === 38 && snake.dy === 0) {
snake.dy = -grid;
snake.dx = 0;
}
// Freccia destra
else if (e.which === 39 && snake.dx === 0) {
snake.dx = grid;
snake.dy = 0;
}
// Freccia in giù
else if (e.which === 40 && snake.dy === 0) {
snake.dy = grid;
snake.dx = 0;
}
});
// Start
requestAnimationFrame(loop);






I want to make a main class for all this code. What can I do?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th March 2024 - 01:47 AM