• 締切済み

プログラミング初心者です

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

みんなの回答

回答No.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無しでいけますね。 いかがでしょうか?

回答No.1

こんにちは。みっちょです。 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);  } }

関連するQ&A

  • プログラミングの課題、手伝ってください!!

    この俺が書いたプログラムを見てわかるとおり、今は1つのボールにしか、ファンクションがきいていない状況になってます。 これを3つのボール個々に大きさが代わったり、速さが変わったり、でてくるロケーションが変わったりするファンクションをきかせたいんです。 int width =600, height = 600; float d = random(5,40); float d1 = random(5,40); float d2 = random(5,40); float d3 = random(5,40); float x = random(0,width); float x1 = random(0,width); float x2 = random(0,width); float x3 = random(0,width); float y =0; float y1 =0; float y2 =0; float y3 =0; float speed1 = random(1,5); float speed2 = random(2,4); float speed3 = random(1,9); void setup() { size(width,height); background(255); smooth(); noStroke(); } void draw() { background(255); x1 = x1; y1 = y1; speed1 = speed1; d1 = d1; fill(255,0,0); ellipse(x,y,d,d); if(y<=height) { y= y + speed1; d= d1; } else { speed1 = random(1,5); d = random(5,40); y = 0; x = random(0,width); } x2 = x2; y2 = y2; speed2 = speed2; d2 = d2; fill(0,255,0); ellipse(x,y,d,d); if(y<=height) { y= y + speed2; d = d2; } else { speed2 = random(2,4); y = 0; d = random(5,40); x = random(0,width); } x3 = x3; y3 = y3; speed3 = speed3; d3 = d3; fill(0,0,255); ellipse(x,y,d,d); if(y<=height) { y= y + speed3; d= d3; } else { speed3 = random(1,9); y = 0; d = random(5,40); x = random(0,width); } } よろしくお願いします。

  • processingのプログラムの書き方について。

    課題で、どこからマウスをプレスしても20個もの四角を(0,0)まで数珠つなぎみたいにするプログラムをかいているんですが全然わかりません。 そのため大きさなどは瞬時に計算をするからmouse x, mouse yをつかいます だからrect(mouseX,mouseY,mouseX,mouseY)になると思うんですが。。 とにかく全部わかりません。教えてください。 自分が書いたのはこんなのですが、全然違います。 forループ、mouseX, mouseYを使うのはわかります int s = 600; int n = 20; int i = mouseX; void setup() { size(s,s); background(255); noStroke(); rectMode(CENTER); } void draw() { if(mousePressed) { for(int i=0; i<s; i++); { fill(255,0,0); rect(mouseX,mouseY,mouseX,mouseY); } } }

  • ActionScript3.0 初心者です

    ActionScript3.0 初心者です。 ムービークリップballに対して、以下のようなスクリプトを書いて、 ロールオーバーすると徐々に大きく、 アウトすると徐々に小さくなるようにしています。 しかし、初めはうまくいくのですが、 オーバー、アウトの回数を重ねるごとに、 縮尺の間隔が短くなってしまいます。 常に同じ間隔で縮尺させるにはどうすればいいのでしょうか? よろしくお願いします。 ball.addEventListener (MouseEvent.MOUSE_OVER, r_over); ball.addEventListener (MouseEvent.MOUSE_OUT, r_out); var c:int; var nDeceleration:Number = 0.1; function r_over(event:MouseEvent):void { c = 400; ball.addEventListener(Event.ENTER_FRAME, ballsize); function ballsize(event:Event):void { if(ball.width==c){ ball.removeEventListener(Event.ENTER_FRAME,ballsize); } else{ ball.width +=(c - ball.width) *nDeceleration; ball.height +=(c - ball.height) *nDeceleration; } } } function r_out(event:MouseEvent):void { c = 150; ball.addEventListener(Event.ENTER_FRAME, ballsize); function ballsize(event:Event):void { if(ball.width==c){ ball.removeEventListener(Event.ENTER_FRAME,ballsize); } else{ ball.width +=(c - ball.width) *nDeceleration; ball.height +=(c - ball.height) *nDeceleration; } } }

  • processingのマウス操作について

    現在processingを使って、画像のような図を作り、マウスでクリックしたところだけに色がついて、もう一度クリックすると色が消えるというプログラミングを作りたいと思っているのですが、なかなかうまくできません。 以下現在打ち込んでいるものです // Click within the image to change // the value of the rectangle after // after the mouse has been clicked int value = 255; int rect_size = 50; int num = 300 / rect_size; int x; void setup(){ size(300,300); } void draw(){ fill(value); for (int i = #ffffff;i < num;i++){ for (int j = 0;j < num;j++){ if (j%2 == 0){ x = i*rect_size*2; } else { x = i*rect_size*2+rect_size; } rect(x,j*rect_size,rect_size,rect_size); } } } void mouseClicked() { if (value == 255) { value = #aaaaaa; } else { value = 255; } } 使用言語はすべて英語です。 おそらく、drawのところでパターンとしてチェック模様を書いてしまっているのと、1マスだけに限定するという風な命令を書けていないからだと思うのですが、先生に訊いてもよくわかりません。 ご存じの方はご指導ご鞭撻のほどどうぞよろしくお願いします。

  • processingのカーソルと画像の距離

    processingでクリスマスカードを作るのですが、どうしてもカーソルと雪だるまの距離があいてしまいます。この距離をあけないようにするにはどうしたらいいでしょうか?わかる方がいらっしゃいましたらどうぞよろしくお願いします。 void setup() { size(600,600); smooth(); noStroke(); } int flag = 0; void draw() { background(10,50,100); fill(0); rect(0,450,600,280); int x=50,y=30; fill(#006e54); triangle(300,50,400,200,200,200); triangle(300,50+x,420,280,180,280); triangle(300,50+2*x,420+y,280+2*x,180-y,280+2*x); fill(190,103,31); rect(275,280+2*x,50,150); textSize(45); fill(255,0,0); text("Merry X'mas!",170,580); fill(random(255),random(255),random(255)); translate(249,5); beginShape(); vertex(50 , 50 - 20); vertex(50 - 12 , 50 + 15); vertex(50 + 18 , 50 - 8); vertex(50 - 18 , 50 - 8); vertex(50 + 12 , 50 + 15); endShape(CLOSE); fill(random(255),random(255),random(255)); ellipse(20,90,30,30); fill(random(255),random(255),random(255)); ellipse(100,130,30,30); fill(random(255),random(255),random(255)); ellipse(10,170,30,30); fill(random(255),random(255),random(255)); ellipse(55,220,30,30); fill(random(255),random(255),random(255)); ellipse(130,250,30,30); fill(random(255),random(255),random(255)); ellipse(90,350,30,30); fill(random(255),random(255),random(255)); ellipse(30,300,30,30); fill(random(255),random(255),random(255)); ellipse(10,360,30,30); fill(random(255),random(255),random(255)); ellipse(160,320,30,30); fill(random(255),random(255),random(255)); ellipse(-40,330,30,30); if(mousePressed) { flag=1; } else { flag=0; } if(flag==1) { snowman(); } else { textSize(20); fill(255); text("Please press mouse to create a snowman!",-150,20); } } void snowman() { int speed=2; int x=mouseX; int y=mouseY; x+=random(-speed,speed);//let snowman move randomly // x=constrain(x,0,500); y+=random(-speed,speed); y=constrain(y,0,600); fill(255); //make a little snowman ellipse(x,y-50,50,50); ellipse(x,y,100,100); fill(0); ellipse(x-10,y-55,10,10); ellipse(x+10,y-55,10,10); fill(#ff0000); triangle(x-10,y-50,x+10,y-50,x,y-30); }

  • プログラミングProcessing ピンポンゲーム

    Processingでピンポンゲームを作っています。作成したプログラムは void setup(){ size(400,300); } float x=10; float y=10; float dx=1; float dy=2; int count=0; float r_w=50.0; float a_w=15.0; float a_h=15.0; boolean checkHit(float x,float y){ if(y+a_h<250)return false; if(x+a_w>=mouseX&&x<=mouseX+r_w){ return true; }else{ return false; } } void draw(){ x=x+dx;y=y+dy; if(x+a_w>=400){ dx=-1; }else if(x<0){ dx=1; } if(y+a_w>300){ x=0; y=0; dx=1; dy=2; count=0; }else if(y<0){ dy=2; } background(0,0,128); rect(x,y,a_w,a_w); rect(mouseX,250,r_w,3); text(count,10,300); if(checkHit(x,y)){ dy=-2; count=count+1; } } このプログラムだとどうしてもラケットの下にボールが潜り込んだ時に ボールが跳ね返されてしまうバグが発生してしまいます。 問題はboolean checkHitの部分にあると思うのですが、 どのように調節すればよいか分かりません。 わかる方教えてください<(__)>

  • プログラミングについて

    初めまして、初めて質問させていただきます。 いきなりですが最近プログラミングにはまり、独学でCプロをやり始めた物なのですが、以下のようなプログラムをCプロで打ち込んでコンパイルしたところ int main(void) { int i,j,height,length; scanf("%d",&height); if(height % 2 ==0) { putchar("invalid"); { return 0; } } scanf("%d",&length); if(height>=1 && height<=100) for(i=1;i<=height;i++){ for(j=1;j<=length;j++) if(i ==1 || i==height) { putchar('e'); } else if( i !=height/2+1){ putchar('e'); for(j=1;j<length;j++) putchar('.'); } else{ for(j=1;j<=length/2+1;j++) putchar('e'); for(j=length/2+1;j<length;j++) putchar('.'); } putchar('\n'); } return(0); } ――――――――――― Main.c:5:1: warning: implicitly declaring library function 'scanf' with type 'int (const char *restrict, ...)' [-Wimplicit-function-declaration] scanf("%d",&height); ^ Main.c:5:1: note: include the header <stdio.h> or explicitly provide a declaration for 'scanf' Main.c:8:5: warning: implicit declaration of function 'putchar' is invalid in C99 [-Wimplicit-function-declaration] putchar("invalid"); ^ 2 warnings generated. ――――――――― と上のようなエラー内容が出てしまい、丸一日自分で模索しても何故こんなエラーが出て来てしまうのか分からない為、教えて頂けるとありがたいです。

  • 初心者です。 コンパイルのエラー

    import java.awt.*; import javax.swing.*; public class R11Sample1 extends JFrame { Rect r1 = new Rect(Color.red, 100, 100, 80, 60); Rect r2 = new Rect(new Color (0.5f, 1f, 0f, 0.7f), 150, 120, 60, 90); Oval = new Oval(Color.blue, 60, 50, 10, 10); JPanel panel = new JPanel() { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; r1.draw(g2); r2.draw(g2); } }; public R11Sample1() { setSize(400, 350); setDefaultCloseOperation(EXIT_ON_CLOSE); getContentPane().add(panel); } public static void main(String[] args) { new R11Sample1().setVisible (true); } } class Rect { Paint pat; int xpos, ypos, width, height; public Rect(Paint p, int x, int y, int w, int h) { pat = p; xpos = x; ypos = y; width = w; height = h; } public void draw(Graphics2D g) { g.setPaint(pat); g.fillRect(xpos-width/2, ypos-height/2, width, height); } } class Oval { Paint pat; int xpos, ypos, radius; public Oval(Paint p, int x, int y, int width, int height) { pat = p; xpos = x; ypos = y; width = w; height = h; } public void draw(Graphics2D g) { g.setPaint(pat); g.fillOval(xpos-width/2, ypos-height/2, width, height); } } これでコンパイルすると、 Identifierがありません といわれました。 どこを直せばいいのでしょうか。 また、全体的に間違ったところがあったら教えてください。

    • ベストアンサー
    • Java
  • flashCS4を使っています。初心者です。

    flashCS4を使っています。初心者です。 シンボルをクリックした時にマウスカーソルを変えたいのですが、どのようなスクリプトを書いたらいいのでしょうか。 今は、 stage.addEventListener(MouseEvent.MOUSE_MOVE,mouse_move); function mouse_move(event:MouseEvent){ yubi1.x =stage.mouseX; yubi1.y =stage.mouseY; } Mouse.hide(); というスクリプトで、カーソルに「yubi1」という画像を使っています。 このyubi1をクリック時にyubi2にしたいのです。 どうかよろしく御願いします。

    • ベストアンサー
    • Flash
  • 突然ですが、DirectX9について質問させて頂きます。

    突然ですが、DirectX9について質問させて頂きます。 ウィンドウモードとフルスクリーンモードを切り替える プログラムを組んでいますが、なぜか、フルスクリーンモード からウィンドウモードに復帰すると画面サイズが変化してしまいます。 以下がその問題となる関数です。 void SetWindowStyle( HWND hWnd, bool g_WindowMode ) {     if( g_WindowMode )     {         SetWindowLong( hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW | WS_VISIBLE );         MoveWindow( hWnd, 0, 0, 0, 0, false );         SetClientSize( hWnd, WINDOW_WIDTH, WINDOW_HEIGHT ); // 以下に記載     }     else     {         SetWindowLong( hWnd, GWL_STYLE, WS_POPUP | WS_VISIBLE );         MoveWindow( hWnd, 0, 0, 800, 600, true );     } } 以下はウィンドウのサイズを再設定する関数です。 void SetClientSize( HWND hWnd, int width, int height ) {     RECT wnd_Rect, clt_Rect;     int WindowSize_Width, WindowSize_Height;     GetWindowRect( hWnd, &wnd_Rect );     GetClientRect( hWnd, &clt_Rect );     // ウィンドウのサイズを求める     WindowSize_Width = ( wnd_Rect.right - 1 ) - ( wnd_Rect.left - 1 );     WindowSize_Height = ( wnd_Rect.bottom - 1 ) - ( wnd_Rect.top - 1 );     // ウィンドウのサイズを変更     MoveWindow(    hWnd,             wnd_Rect.left,             wnd_Rect.top,             width + (WindowSize_Width-clt_Rect.right),             height + (WindowSize_Height-clt_Rect.bottom),             true     ); } SetClientSize関数はアプリ起動時に一度実行され、800x600に変更されます。 この時点では問題はありません。 しかし、フルスクリーンモード→ウィンドウモードと切り替えると、 800x576と値がおかしくなります。 結果的に800x600になる予定だったのですが…。 同じ関数を使用しているのに、なぜサイズが変化してしまうのでしょう? どなたかこの問題を解決する方法をご存知ないでしょうか? よろしくお願いします。

専門家に質問してみよう