• 締切済み

プログラミング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の部分にあると思うのですが、 どのように調節すればよいか分かりません。 わかる方教えてください<(__)>

みんなの回答

  • D-Matsu
  • ベストアンサー率45% (1080/2394)
回答No.1

ボールのY座標チェックを「ラケットより上」だけではなく「ラケットより下」に対しても行うようにすればいいでしょう。

関連するQ&A

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

  • セグメンテーション違反

    穴掘り方というやり方で迷路を作っています。 とりあえずすこしだけ作ってみて実行したらセグメンテーション違反が起こってしまいました。 解説お願いします。 次のサイトを参考にしています。ttp://www.ced.is.utsunomiya-u.ac.jp/lecture/2009/prog/p3/kadai4/5.html #include<stdio.h> #include<stdlib.h> #include<time.h> #define A 51 #define B 51 /*51ラ51マスの迷路*/ void initialize(int *x,int *y,int map[A][B]);  /*最初のステージ作成と座標決定*/ int dig(int *x,int *y,int map[A][B]);   /*道を作る*/ int main(void) { int x,y;   /*現在の座標*/ int map[A][B];    initialize(&x,&y,map); dig(&x,&y,map); map[1][0] = 2; map[A-2][B-1] = 2; for(y=0;y<B;y++){ for(x=0;x<A;x++){ if( map[x][y] == 0){ printf(" "); }else if( map[x][y] == 1){ printf("■"); }else if( map[x][y] == 2){ printf("..") ; } } printf("\n"); } } void initialize(int *x,int *y,int map[A][B]) { int i,h; for(i=0;i<A;i++){ for(h=0;h<B;i++){ map[i][h]=1; } } do{ *x=rand()%A; *y=rand()%B; }while(*x!=0 && *x!=A-1 && *y!=0 && *y!=B-1); } int dig(int *x,int *y,int map[A][B]) { int r,c,dx,dy,count=0; do{ r = rand()%4; switch(r){     /*道を進める方向を決める*/ case 0: dx = 0; dy = -1; break; case 1: dx = -1; dy = 0; break; case 2: dx = 0; dy = 1; break; case 3: dx = 1; dy = 0; break;    } if(*x+dx*2 <= 0 || *y+dy*2 <= 0 || *x+dx*2 >= A-1 || *y+dy*2 >= B-1 || map[*x+dx*2][*y+dy*2] == 0){ c = 0; count++; if(count ==4){     /*4方向とも進めなかったらループを抜ける*/ break; } }else if(map[*x+dx*2][*y+dy*2] == 1){ map[*x+dx][*y+dy] = 0; *x = *x + dx*2; *y = *y + dy*2; c =1; } }while(c==0); }

  • クリックされた地点が2点の線分上かの判定

    bool CheckOnline(int x1,int y1,int x2,int y2,int MouseX,int MouseY){ int range = 10; //許容範囲 double a; double b; if(x2 == x1){ if( abs( MouseX -x1)<range ){ if( abs(MouseY -abs(y2-y1)) <range) return true; } }else{ a=(y2-y1)/(x2-x1); b= y1 - a*x1; if( abs(MouseY - (a * MouseX +b)) <range ){ if( abs(MouseX - ((MouseY-b)/a)) <range ) return true; } } return false; } ぴったりでなく少し誤差があっても線上と判定したいです。 上記の方法ではうまく行きませんでした。 一応、y=ax+bのaとbを求めて、マウスのxとマウスのyを代入 その結果がrange以内かどうかでやってみようとした結果です。

  • 「初心者です」-Xlint: deprecation

    import java.awt.*; import java.applet.*; public class ani_ball extends Applet implements Runnable { Thread th_mvball = null; int X,Y; double x1,y1; double x2,y2; double dx,dy; Graphics g; public void init() { this.setBackground(new Color(150,245,255)); this.X = 250; this.Y = 250; this.setSize(X,Y); this.x1 = this.x2 = this.X/2; this.y1 = this.y2 = this.Y/2; this.dx = 3; this.dy = 2; } public void paint(Graphics g) { g.setColor(Color.red); g.fillOval((int)(this.x2-3),(int)(this.y2-3),6,6); this.x1 = this.x2; this.y1 = this.y2; } public void start() { if(th_mvball == null) { th_mvball = new Thread(this); th_mvball.start(); } } public void run() { while(true) { try { this.move(); this.repaint(); Thread.sleep(10); } catch(InterruptedException e ) { this.stop(); } } } public void move() { if( y2 > Y ) { y2 = 2*Y - y1 - dy; dy = -dy; } else if ( y2 < 0) { y2 = -y1 - dy; dy = -dy; } else if ( x2 > X) { x2 = 2*X - x1 -dx; dx = -dx; } else if ( x2 < 0) { x2 = -x1 - dx; dx = -dx; } if( x2 < 0 ) { x2 = -x2 ; dx = -dx; } else if ( y2 < 0) { y2 = -y2; dy = -dy; } else if ( x2 > X) { x2 = 2*X - x2; dx = -dx; } else if ( y2 > Y) { y2 = 2*Y - y2; dy = -dy; } x2 = x1 + dx; y2 = y1 + dy; } public void stop() { if(th_mvball!=null) { th_mvball stop(); th_mvball=null; } } } ↑のプログラムをコンパイルすると、「-Xlint: deprecation オプションを指定して再コンパイルしてください」とエラーが出ます。 エラーの対処法、またはプログラムの訂正すべき箇所を教えてください。 よろしくお願いします。

    • ベストアンサー
    • Java
  • プログラミング初心者です

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

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

    この俺が書いたプログラムを見てわかるとおり、今は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のカーソルと画像の距離

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

  • ボールが壁に当たって跳ね返るプログラムを・・

    スタートボタンを押すと一個のボールが現れ、 picture1の中を動き回り、壁に当たると跳ね返る、 スクリーンセイバーのようなプログラムを作って いるのですが「ボールが壁に当たって跳ね返る」 部分がどうしても上手くいきません。この部分を どなたか教えてください。お願いしますm(_ _)m (見やすいように線を引きました。最後の方が「跳ね返りの部分です。それ以外の部分は、文の長さ制限にひっかかるため省いてあるところがあります。) Private Sub Command1_Click() x = Int(Rnd * 3900) y = Int(Rnd * 3900) r = 100 c = vbRed Timer1.Enabled = True Timer1.Interval = 200 Picture1.Circle (x, y), r, vbRed End Sub ----------------------------------------- Private Sub Timer1_Timer() Picture1.FillColor = Picture1.BackColor Picture1.Circle (x, y), r, Picture1.BackColor dx = 100 dy = dx x = x + dx If x < 0 Then x = 0 And dx = 0 - dx If x > Picture1.Width Then x = Picture1.Width And dx = 0 - dx End If End If y = y + dy If y < 0 Then y = 0 And dy = 0 - dy If y > Picture1.Height Then y = Picture1.Height And dy = 0 - dy End If End If Picture1.FillColor = vbRed Picture1.Circle (x, y), r, vbRed End Sub

  • Processingでのキーの同時入力

    Processingでゲームを作ろうとしているのですがキーボードの同時入力がうまくいきません。 例えば下記のようなキー入力に応じて黒い四角が動くプログラムを実行しました。方向キーを一つだけ押したときはサクサク動いてくれるのですが、→と↓を同時に押しても斜めに進んでくれません。また、→と←を押した場合もどちらか片方の方に動いてしまいます。 このような問題を解決したいので、Processingでキーボードの同時入力を判定するにはどのようにすればいいのか教えてください。 float x, y; void setup(){ size(400, 400); noStroke(); smooth(); } void draw() { background(255,255,255); fill(0,0,0); rect(x,y,20, 20); if (keyPressed && key == CODED){ if (keyCode == LEFT){ x -= 2; } if (keyCode == RIGHT){ x += 2; } if (keyCode == UP){ y -= 2; } if (keyCode == DOWN){ y += 2; } } }

  • Cプログラムで15パズルを作ってみたのですがうまく動作しません。何処が

    Cプログラムで15パズルを作ってみたのですがうまく動作しません。何処が間違っているのかずっと考えているのですがいまだに解決策が見つかりません。ヒントでもいいのでお願します。 #include <stdio.h> int init(void); void show(void); int chk_cmp(void); char input(void); int move(char cmd); #define N 4 int panel[N][N] = { { 1, 2, 3, 4}, { 5, 6, 7, 8}, { 9, 10, 11, 0}, {13, 14, 15, 12} }; int x, y; int main(void) { printf("これは15パズルです。\n" "左上から右に向かって「1」から「15」が並ぶよう,\n" "「0」を動かしてください。\n" "操作はテンキーで行います。( 8(上),4(左),6(右),2(下) )\n"); if( !init() ) { printf("パネルの初期化に失敗しました。「0」のパネルがありません。\n"); return 1; } while(1) { show(); if( chk_cmp() ) { printf("完成です!\n"); break; } while(1) { if( move(input()) ) { break; } else { printf("そっちには動かせません。\n"); } } } return 0; } int init(void) { int i,j; for(i=0;i<=N-1;i++){ for(j=0;j<=N-1;j++){ if(panel[i][j]==0){ x=j; y=i; return 1; } } } return 0; } void show(void) { int i,j; printf("---------------\n"); for(i=0;i<=N-1;i++){ for(j=0;j<=N-1;j++){ printf("%3d",panel[i][j]); } printf("\n"); } printf("---------------\n\n"); } int chk_cmp(void) { int i,j; for(i=0;i<=N-1;i++){ for(j=0;j<=N-1;j++){ if(i==N-1&&j==N-1){ if(panel[i][j]!=0){ return 0; } }else{ if(panel[i][j]!=N*i+j+1){ return 0; } } } } return 1; } char input(void) { int comand; while(1){ scanf("%d",&comand); if(comand==8||comand==4||comand==6||comand==2){ break; } printf("8(上),4(左),6(右),2(下)を入力してください。"); } return comand; } int move(char cmd) { int dx=0, dy=0; if(cmd==8){dy=-1;}//上 if(cmd==4){dx=-1;}//左 if(cmd==6){dx=1;}//右 if(cmd==2){dy=1;}//下 if(x+dx>=0&&x+dx<=N-1&&y+dy>=0&&y+dy<=N-1){ panel[y][x]==panel[y+dy][x+dx]; panel[y+dy][x+dx]==0; y+=dy; x+=dx; return 1; } else{return 0;} }

専門家に質問してみよう