Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

HELP Code Snake

1 réponse
Avatar
valham
Bonjour, je suis désespéré ça fait des heures que j'essaie de trouver d'où vient mon erreur sur ce petit fichier de programmation d'un Snake.. merci d'avance pour vos lumières !
window.onload = function()
{
var canvasWidth = 900;
var canvasHeight = 600;
var blockSize = 30;
var ctx;
var delay = 100;
var snakee;
var applee;
var widthInBlocks = canvasWidth/blockSize;
var heightInBlocks = canvasHeight/blockSize;

init();

function init()
{
var canvas = document.createElement('canvas');
canvas.width = canvasWidth;
canvas.height = canvasHeight;
canvas.style.border = "e;1px solid"e;;
document.body.appendChild(canvas);
ctx = canvas.getContext('2d');
snakee = new Snake([[6,4], [5,4], [4,4]], "e;right"e;);
applee = new Apple([10,10]);
refreshCanvas();
}

function refreshCanvas()
{
snakee.advance();
if(snakee.checkCollision)
{
// GAME OVER
}
else
{
ctx.clearRect(0,0,canvasWidth, canvasHeight);
snakee.draw();
applee.draw();
setTimeout(refreshCanvas,delay);
}

}

function drawBlock(ctx, position)
{
var x = position[0] * blockSize;
var y = position[1] * blockSize;
ctx.fillRect(x ,y , blockSize, blockSize);
}

function Snake(body, direction)
{
this.body = body;
this.direction = direction;
this.draw = function()
{
ctx.save();
ctx.fillStyle= "e;#ff0000"e;;
for(var i = 0; i < this.body.lenght; i++);
{
drawBlock(ctx, this.body[i]);
}
ctx.restore();
};
this.advance= function()
{
var nextPosition = this.body[0].slice();
switch(this.direction)
{
case "e;left"e;:
nextPosition[0] -= 1;
break;
case "e;right"e;:
nextPosition[0] += 1;
break;
case "e;down"e;:
nextPosition[1] += 1;
break;
case "e;up"e;:
nextPosition[1] -= 1;
break;
default:
throw("e;Invalid Direction"e;)
}
this.body.unshift(nextPosition);
this.body.pop(); /* POP efface dernier élément */
};
this.setDirection = function(newDirection)
{
var allowedDirections;
switch(this.direction)
{
case "e;left"e;:
case "e;right"e;:
allowedDirections = ["e;up"e;, "e;down"e;];
break;
case "e;down"e;:
case "e;up"e;:
allowedDirections = ["e;left"e;, "e;right"e;]
break;
default:
throw("e;Invalid Direction"e;)
}
if(allowedDirections.indexOf(newDirection) > -1)
{
this.direction = newDirection;
}
};
this.checkCollision = function ()
{
var wallCollision = false;
var snakeCollision = false;
var head = this.body[0];
var rest = this.body.slice(1);
var snakeX = head[0];
var snakeY = head[1];
var minX = 0;
var minY = 0;
var maxX = widthInBlocks - 1;
var maxY = widthInBlocks - 1;
var isNotBetweenHorizontalWalls = snakeX < minX || snakeX > maxX;
var isNotBetweenVerticalWalls = snakeY < minY || snakeY > maxY;

if(isNotBetweenHorizontalWalls || isNotBetweenVerticalWalls)
{
wallCollision = true;
}

for(var i = 0; i < rest.length ; i++)
{
if(snakeX === rest[i][0] && snakeY === rest[i][1])
{
snakeCollision = true;
}
}

return wallCollision || snakeCollision;

};

}
function Apple(position)
{
this.position = position;
this.draw = function ()
{
ctx.save();
ctx.fillStyle = "e;#33cc33"e;;
ctx.beginPath();
var radius = blockSize/2;
var x = position[0]*blockSize + radius;
var y = position[1]*blockSize + radius;
ctx.arc(x,y, radius, 0, Math.PI*2, true) /*dessin cercle */
ctx.fill();
ctx.restore();
}

}


document.onkeydown = function handleKeyDown(e)
{
var key = e.keyCode;
var newDirection;
switch(key)
{
case 37:
newDirection = "e;left"e;;
break;
case 38:
newDirection = "e;up"e;;
break;
case 39:
newDirection = "e;right"e;;
break;
case 40:
newDirection = "e;down"e;;
break;
default:
return; /* ne continue pas la fonction */
}
snakee.setDirection(newDirection);
}
}

1 réponse

Avatar
Olivier Miakinen
Bonjour,
Le 03/11/2020 13:51, valham a écrit :
Bonjour, je suis désespéré ça fait des heures que j'essaie de trouver d'o͹ vient
mon erreur sur ce petit fichier de programmation d'un Snake..

Quelle erreur ?
merci d'avance
pour vos lumières !
window.onload = function()
{
[... code supprimé pour alléger ma réponse ...]
}

Tu donnes le code, mais tu ne dis ni quel comportement tu obtiens, ni quel
comportement tu t'attends Í  obtenir (et donc en quoi les deux diffèrent).
Cordialement,
--
Olivier Miakinen