我有这段代码可以使用类和对象生成随机图块网格。我希望每当用户按下鼠标时,画布就会自动刷新并生成新的艺术作品。而且我尝试过的 mousepress 功能还没有工作。谢谢您的帮助!
void setup(){
size(400,400);
for (int i=0; i<8; i++){
for (int j=0; j<8; j++){
blk[i][j]= new Block(i*50,j*50);
}
}
}
void draw(){
background(255);
for (int i=0; i<8; i++){
for (int j=0; j<8; j++){
float num = random(0,5);
if (int(num)<3){
blk[i][j].tile();
blk[i][j].line1();
}
else {
blk[i][j].tile();
blk[i][j].line2();
}
}
}
noLoop();
}
void mousePressed(){
}
// Class
class Block{
float x,y;
Block(float _x, float _y){
x=_x;
y=_y;
}
void tile(){
stroke(0);
strokeWeight(2);
square(x,y,50);
}
void line1(){
stroke(0);
strokeWeight(2);
line(x,y,x+50,y+50);
}
void line2(){
stroke(0);
strokeWeight(2);
line(x+50,y,x,y+50);
}
}
当按下鼠标时告诉它重绘():
void mousePressed() {
redraw();
}