Javascript For循环在调用时未运行
代码:Javascript For循环在调用时未运行,javascript,for-loop,Javascript,For Loop,代码: function SnakeGame() { "use strict"; /***** Constant & Global Variables ***********************************/ var canvas = $("#canvas")[0]; var ctx = canvas.getContext('2d'); var width = $("#canvas").width(); var heig
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
};
}
函数蛇形游戏(){
“严格使用”;
/*****常量和全局变量***********************************/
var canvas=$(“#canvas”)[0];
var ctx=canvas.getContext('2d');
变量宽度=$(“#画布”).width();
变量高度=$(“#画布”).height();
蛇、食物;
函数Snake(){
var startedength=5;//snake起始大小的默认值
this.body=[];
this.chgStartLength=函数(长度){
长度=长度;
};
this.create=function(){
var i;
对于(i=0;i>5;i++){
this.body.push({x:i,y:0});
}
};
}
var paintCanvas=函数(){
ctx.fillStyle='白色';
ctx.fillRect(0,0,宽度,高度);
ctx.strokeStyle=‘黑色’;
ctx.冲程(0,0,宽度,高度);
};
var paintFrame=函数(){
变量i,长度=snake.body.length;
对于(i=0;i
我认为,
朝着错误的方向
for(i = 0; i < 5; i++) {
(i=0;i<5;i++)的{
我认为,
朝着错误的方向
for(i = 0; i < 5; i++) {
(i=0;i<5;i++)的{
谢谢……非常简单。非常感谢!非常感谢……非常简单。非常感谢!