javascript项目和数组的问题(让它下雨)

时间:2017-12-16 00:58:25

标签: javascript arrays canvas khan-academy

我在这里有点新意。我正在参加可汗学院的编程课程(主要是javascript)而且我有点坚持一个项目。这是一个名为“让雨下雨”的挑战,在那里你应该制作落在画布上的雨滴,并在之后重置回顶部。使用数组,我设法让drop在随机位置生成并通过画布掉落,但由于使用了数组,因此无法让它们重新启动到顶部。抱歉长文;这是我的代码:

var dropX = [40];
var dropY = [0];
var snowX = [40];
var speed = 1.5;
var ex = 0 ;
var i = 0;
var fall = dropY[i];
noStroke(); 


var rain = function (){ 

 var fall = dropY[i];    
 var ok = dropX.length;     
for(var i = 0; i<100; i++) {

    if (fall<2100){
    background(192, 224, 237);
    fill(23, 123, 143);
    ellipse(dropX[i],dropY[i],5,5);
    fill(224, 213, 213);
    rect(snowX[i],dropY[i],5,5);
    dropY[i]+=speed;
    dropX.push(random(0,400)); 
    dropY.push(random(-1650,0)); 
    snowX.push(random(0,400));
}

}
for (i = 0;i<100; i++)    {
    i++;
    fill(45, 136, 189);
    ellipse(dropX[i],dropY[i],5,5);
    fill(255, 255, 255);
    rect(snowX[i],dropY[i],5,5);
}};

draw = function() {
rain();


};

https://www.khanacademy.org/computer-programming/spin-off-of-project-make-it-rain/5960057721651200

据我所知,汗学院代码编辑器使用processing.js。感谢任何帮助或提示!!

1 个答案:

答案 0 :(得分:0)

这些行:

#!/usr/bin/env python3

import turtle

# --- functions ---

def s(n, l):

    if n == 0: # stop conditions

        # draw filled rectangle

        turtle.color('black')
        turtle.begin_fill()
        for _ in range (4):
            turtle.forward(l)
            turtle.left(90)
        turtle.end_fill()

    else: # recursion

        # around center point create 8 smalles rectangles.
        # create two rectangles on every side 
        # so you have to repeat it four times

        for _ in range(4):
            # first rectangle
            s(n-1, l/3)    
            turtle.forward(l/3)

            # second rectangle
            s(n-1, l/3)    
            turtle.forward(l/3)

            # go to next corner
            turtle.forward(l/3)
            turtle.left(90)

        # update screen
        turtle.update()

# --- main ---    

# stop updating screen (to make it faster)
turtle.tracer(0) 

# start
s(4, 400)

# event loop
turtle.done()

所有创建大小为1的数组var dropX = [40]; var dropY = [0]; var snowX = [40]; dropX[0]的值都为snowX[0]40的值为dropY[0]。< / p>

除了你的循环从00而不是100;

这将正确初始化数组:

40