Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > fr.comp.lang.javascript > #3255
| Newsgroups | fr.comp.lang.javascript |
|---|---|
| Subject | HELP Code Snake |
| From | valham <nospam_valerianhamel@hotmail.fr.invalid> |
| Organization | !No_Organization! |
| Message-ID | <NJGdnflSLq3xzzzCnZ2dnUU798zNnZ2d@giganews.com> (permalink) |
| Date | 2020-11-03 06:51 -0600 |
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);
}
}
Back to fr.comp.lang.javascript | Previous | Next — Next in thread | Find similar
HELP Code Snake valham <nospam_valerianhamel@hotmail.fr.invalid> - 2020-11-03 06:51 -0600 Re: HELP Code Snake Olivier Miakinen <om+news@miakinen.net> - 2020-11-03 16:29 +0100
csiph-web