プロが教える店舗&オフィスのセキュリティ対策術

processingプログラミング作成をしています。

mouseファンクション(mouseClickedとか)やkeyファンクション(keyPressed)などのファンクションをつかったときに、クリックやkeyをたたいたら次々変わっていくみたいなプログラムを作成したいんですが、教えてください。

いまは画面を4等分にし、mouseClickedををつかったらランダムに選ばれたスペースを赤く塗り、もう一回mouseをクリックしたら、その赤く塗られたのは消えて、次のまたランダムに選ばれたスペースへと移動する。。それを何回もやるというプログラミングにしたいです。

sizeは600,600でそれぞれのrectは
rect(0,0,300,300)
rect(300,0,600,300)
rect(0,600,300,300)
rect(600,600,300,300)
で区切ってます。ずっと時間かけてやってますが、これをどうやってrandomファンクションにし、それをランダムに選ばれたスペースをマウスクリックしたら赤く塗られるようにするかがもっていくかがどうしてもわかりません。

あと、もう一回クリックしたら前にクリックした場所が消えるというのもわかりません。

これが今までに書いたプログラムです。

いまはif elseをつかっています。

よろしくお願いします。

void setup()
{
size(600,600);
strokeWeight(2);
background(255);
line(0,width/2,600,width/2);
line(height/2,0,height/2,600);
}

void draw()
{

fill(255,0,0);
}

void mouseClicked()
{
if( mouseX <= width/2)
{
fill(255,0,0);
rect(0,0,width/2,height/2);

}
else
{
rect(width/2,0,300,300);
fill(255,0,0);
}
if (mouseX > height/2)
{
rect(0,width/2,height/2,height);
fill(255,0,0);
}
else
{
rect(width/2,height/2,width,height);
fill(255,0,0);
}
}

A 回答 (2件)

こんにちは、みっちょです。



void setup()
{
 size(600, 600);
 strokeWeight(2);
 background(255);
 for (int i=0; i<2; i=i+1) {
  for (int j=0; j<2; j=j+1) {
   rect(i*width/2, j*width/2, (i+1)*width/2, (j+1)*height/2);
  }
 }
}

void draw()
{
}

void mouseClicked()
{
 fill(255, 255, 255);
 for (int i=0; i<2; i=i+1) {
  for (int j=0; j<2; j=j+1) {
   rect(i*width/2, j*width/2, (i+1)*width/2, (j+1)*height/2);
  }
 }
 int x = int(random(2));
 int y = int(random(2));
 fill(255, 0, 0);
 rect(x*width/2, y*width/2, (x+1)*width/2, (y+1)*height/2);
}

こんな感じだとif else無しでいけますね。
いかがでしょうか?
    • good
    • 0

こんにちは。

みっちょです。

processing自体を使ったことがなかったのですが、ご質問の内容を見てみてなんだか面白そうなので使ってみたところ、言われているような事ができました。

processingというものを初めて起動して10分間くらいで仕上げたプログラムなので、何か本当はイケない書き方などやってるかもしれません。とりあえずこんな感じでどうでしょう?という感じで☆

int i;

void setup()
{
 size(600,600);
 strokeWeight(2);
 background(255);
 rect(0,0,width/2,height/2);
 rect(width/2,0,300,300);
 rect(0,width/2,height/2,height);
 rect(width/2,height/2,width,height);
}

void draw()
{
 fill(255,0,0);
}

void mouseClicked()
{
 fill(255,255,255);
 rect(0,0,width/2,height/2);
 rect(width/2,0,300,300);
 rect(0,width/2,height/2,height);
 rect(width/2,height/2,width,height);
 int r = int(random(4));
 fill(255,0,0);
 if(r==0)
 {
  rect(0,0,width/2,height/2);
 }
 if(r==1)
 {
  rect(width/2,0,width,height/2);
 }
 if(r==2)
 {
  rect(0,width/2,width/2,height);
 }
 if(r==3)
 {
  rect(width/2,height/2,width,height);
 }
}
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!