当我要绘制蛇时,我试图用js编写一个简单的蛇游戏,盒子的尺寸错误,并且在带有一些警告框的情况下,我发现计算(5 * 100)+9导致5009,但仅当在同一行上执行另一次计算时。
<!DOCTYPE HTML>
<html>
<style>
canvas {
background-color: #000;
}
</style>
<body>
<canvas id="Canvas" width=1000px height=1000px>
</canvas>
</body>
<script>
var canvas = document.getElementById("Canvas");
var ctx = canvas.getContext("2d");
var sX = 5; //snake head x
var sY = 5; //snake head y
var sDir = 1 //0n1e2s3w, snake direction
//while(1){
ctx.clearRect(0, 0, canvas.width, canvas.height); //get context
ctx.fillStyle = "#0f0";
ctx.fillRect((sX * 100) + 1, (sY * 100) + 1, (sX * 100) + 9, (sY * 100) + 9); //draw snake part, here is the issue
alert((sX * 100) + 9 + " " + sX + " " + (sX * 100) + 9); //debug statements
alert((sX * 100) + 9);
//}
</script>
</html>