📜  处理中的创意编程|设置 1(随机游走器)

📅  最后修改于: 2021-10-18 12:45:20             🧑  作者: Mango

创意编程是一种编程方法,其目标是创建具有表现力和视觉效果的东西,而不是纯粹的功能性东西。这种类型的编程方法用于创建现场艺术作品、图形模拟和可视化算法。有许多用于创造性或可视化编程的工具和库,其中 Processing 是最广泛使用的。 Processing 是一种开源编程语言和 IDE,专为可视化编程目的而构建。处理可在此处免费下载。它也可以作为Python方言(processing.py)和 javascript 框架(p5.js)使用。在本文中,我们将构建一个简单的随机游走程序,它只是一个在画布上随机移动的球。

每个处理草图通常包含两个功能-

  • setup() – 它在开始时被调用一次,通常用于初始化目的。
  • draw() – 默认情况下每秒调用 30 次,使动画的默认帧速率为每秒 30 帧。

草图的实现-
示例代码是使用处理库和处理 IDE 用Java编写的。

Walker w; // Walker object
  
void setup() // Called at the beginning once
{
    size(640, 360); // Declaring size of the output window
    w = new Walker(); // Initializing the new walker object
}
  
void draw() // Called every frame
{
    background(255); // Setting a white background
    w.display(); // Displaying the walker object
}

Walker类的实现-

class Walker // The walker class
{
    PVector location; // A vector object representing the location
  
    Walker() // Constructor to initialize the data member.
    {
        // Initial location of the walker object is
        // set to the middle of the output window.
        location = new PVector(width / 2, height / 2);
    }
  
    void display() // Function to display the walker object
    {
        // Drawing a black circle of radius 10 at location
        fill(0);
        ellipse(location.x, location.y, 10, 10);
    }
}

此时,如果我们运行草图,它只会显示一个位于输出屏幕中心的球-
为了移动 walker 对象,我们将在Walker类中添加一个walk()函数,并在草图中的draw()函数中调用它。我们还在Walker添加了checkEdges()函数以防止Walker对象移出屏幕。我们还必须修改草图以包含我们在Walker类中添加的新函数。我们还可以做一件事,将background()函数移到setup() 。这样,背景不会每次都更新,我们将能够看到路径,Walker 对象留下的痕迹。

草图的修改实现-

// Program to implement random walker
  
Walker w; // Walker object
  
void setup() // Called at the beginning once
{
    size(640, 360); // Declaring size of the output window
    background(255); // Setting a white background
    w = new Walker(); // Initializing the new walker object
}
  
void draw() // Called every frame
{
    w.walk(); // Walking the Walker object
    w.checkEdges(); // Checking for edges of the output screen.
    w.display(); // Displaying the walker object
}

Walker 类的修改实现 –

class Walker // The walker class
{
    PVector location; // A vector object representing the location
  
    Walker() // Constructor to initialize the data member.
    {
        // Initial location of the walker object is
        // set to the middle of the output window.
        location = new PVector(width / 2, height / 2);
    }
  
    void walk()
    {
        // The x and y values of the location
        // vector are incremented by a random value
        // between -5 and 5
        location.x += random(-5, 5);
        location.y += random(-5, 5);
    }
  
    // Function to prevent the Walker to move out of the screen
    void checkEdges()
    {
        if (location.x < 0)
            location.x = 0;
        else if (location.x > width)
            location.x = width;
        if (location.y < 0)
            location.y = 0;
        else if (location.y > height)
            location.y = height;
    }
  
    void display() // Function to display the walker object
    {
        // Drawing a black circle of radius 10 at location
        fill(0);
        ellipse(location.x, location.y, 10, 10);
    }
}