gpt4 book ai didi

javascript - For-Loop 调用时未运行

转载 作者:行者123 更新时间:2023-11-28 12:40:53 25 4
gpt4 key购买 nike

代码:

function SnakeGame() {
"use strict";

/***** Constant & Global Variables ***********************************/
var canvas = $("#canvas")[0];
var ctx = canvas.getContext('2d');
var width = $("#canvas").width();
var height = $("#canvas").height();

var snake, food;

function Snake() {
var startLength = 5; // default for starting size of snake
this.body = [];

this.chgStartLength = function(length) {
startLength = length;
};

this.create = function() {
var i;
for(i=0; i>5; i++) {
this.body.push({x:i, y:0});
}
};
}

var paintCanvas = function() {
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, width, height);
ctx.strokeStyle = 'black';
ctx.strokeRect(0, 0, width, height);
};

var paintFrame = function() {
var i, length = snake.body.length;
for(i=0; i<length; i++) {
var cell = snake.body[i];
ctx.fillStyle = 'black';
ctx.fillRect(cell.x*10, cell.y*10, 10, 10);
ctx.strokeStyle = 'white';
ctx.strokeRect(cell.x*10, cell.y*10, 10, 10);
}
};

this.start = function() {
snake = new Snake();
snake.create();

paintCanvas();
paintFrame();
console.log(snake.body); //for testing
};

}

出于某种原因,即使在执行 start 后,snake.body 也是一个空数组。

最佳答案

for(i=0; i>5; i++) {

我相信>面向的是错误的方向。

for(i = 0; i < 5; i++) {

关于javascript - For-Loop 调用时未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12342898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com