期中作品試做
※拼圖
程式碼:
int [][]puzzle=new int[10][10];int [][]touch=new int[10][10];
boolean bG=false;
void setup(){
size(400,800);
}
void draw(){
background(255); ///底色
if(bG){
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(puzzle[j][i]==0)fill(255);
else fill(128); //畫答案
rect(100+20*j,100+20*i,20,20);
}
}
}
else{
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(touch[j][i]==0)fill(200);
else if(puzzle[j][i]==0&& touch[j][i]==1)fill(255,0,0); ///錯誤 顯示紅色
else if(puzzle[j][i]==1&& touch[j][i]==1)fill(0,255,0); ///正確 顯示綠色
rect(100+20*j,100+20*i,20,20);
}
}
}
}
void mouseDragged(){ ///滑鼠拖曳
int i=int((mouseY-100)/20);
int j=int((mouseX-100)/20);
if(bG){
puzzle[j][i]=1;
}
else{
touch[j][i]=1;
}
}
void keyPressed(){
if(key=='g'){ ///案g可以畫答案圖
bG=!bG;
}
}
沒有留言:
張貼留言