美文网首页
2019-02-16 P9 练习0.3

2019-02-16 P9 练习0.3

作者: laoyaodao | 来源:发表于2019-02-16 00:05 被阅读0次

创建一个有动态移动趋势的Walker对象。比如,写出一个有50%概率向鼠标所在方向移动的Walker对象。

class Walker{

    int x;

    int y;

    Walker(){

        x = width / 2;

        y = height / 2;

    }

    void display() {

        stroke(0);

        point(x, y);

        //ellipse(x,y,20,20);

    }

    void step() {

        float r = random(1);

        if(r < 0.5){

            if(mouseX > x)

                x++;

            else

                x--;

            if(mouseY > y)

                y++;

            else

                y--;

        }

        else{

            x += int(random(3)) - 1;

            y += int(random(3)) - 1;

        }

    }

}

Walker w;

void setup() {

    size(640, 360);

    w = new Walker();

    background(255);

}

void draw() {

    //background(255);

    w.step();

    w.display();

}

相关文章

网友评论

      本文标题:2019-02-16 P9 练习0.3

      本文链接:https://www.haomeiwen.com/subject/nabveqtx.html