处理中的创造性编程|集1(随机游走者)

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

null

每个加工草图通常由两个功能组成-

  • 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对象,我们将添加一个 walk() 作用于 Walker 上课并在教室里叫它 draw() 在草图中的作用。我们还添加了一个 checkEdges() 作用于 Walker 防止 Walker 对象从屏幕中移出。我们还必须修改草图,以包括我们在 Walker 班我们还可以做一件事,移动 background() 内部功能 setup() 这样,背景不会每次都更新,我们就能看到步行者留下的痕迹。

草图的改进实现-

// 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);
}
}


本文由 苏米克·拉克什 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享