用for循环创造艺术图像
void setup()
{
size(500,500);
}
void draw()
{
background(0,0,0);
noStroke();
for(int i =width;i>0;i-=20)
{
fill(i/2,i/2,i/2);
rect(width/2-i/2,height/2-i/2,i,i);
}
}
随机数
void setup()
{
size(500,500);
}
void draw()
{
background(125,125,125);
for(int i =0; i<5;i++)
{
float randX =random(0,width);
float randY =random(0,height);
fill(255,0,255);
rect(randX,randY,40,40);
}
}
方块的位置是随机数。
arrays数组
int [] backgroundColors =new int [3];
void setup()
{
size(500,500);
backgroundColors[0] =(int)random(0,255);
backgroundColors[1] =(int)random(0,255);
backgroundColors[2] =(int)random(0,255);
String [] names ={"wang","pu","qing"};
println(names[(int)random(0,2)]);
}
void draw()
{
background(backgroundColors[0],backgroundColors[1],backgroundColors[2]);
}
数组与随机数的小练习
直接在println()函数中输入数组名就可以输出数组。
println相关
void setup()
{
size(500,500);
int [] Arraynum ={0,34,128,2};
int num1 =1;
int num2=11;
println(Arraynum);
println("array:"+Arraynum);
println(num1,num2);
println(num1+" "+num2);
}
void draw()
{
background(0,0,0);
}
println(num1,num2);
println(num1+" "+num2);
相同效果。
println(数组名)会显示所有的数组
println(“array:”+数组)会显示内存指针(地址)