• 締切済み

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

この俺が書いたプログラムを見てわかるとおり、今は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); } } よろしくお願いします。

  • Java
  • 回答数1
  • ありがとう数2

みんなの回答

  • sekibunx
  • ベストアンサー率61% (8/13)
回答No.1

元のソースコードを極力流用するなら... 普通はx1やらy1やらをそれぞれ配列で用意して,forで処理すると思います. //x1 = x1; //y1 = y1; //speed1 = speed1; //d1 = d1; fill(255,0,0); ellipse(x1,y1,d1,d1); if(y1 <= height) { y1 = y1 + speed1; //d = d1; } else { speed1 = random(1,5); d1 = random(5,40); y1 = 0; x1 = random(0,width); } //x2 = x2; //y2 = y2; //speed2 = speed2; //d2 = d2; fill(0,255,0); ellipse(x2,y2,d2,d2); if(y2<=height) { y2 = y2 + speed2; //d = d2; } else { speed2 = random(2,4); y2 = 0; d2 = random(5,40); x2 = random(0,width); } //x3 = x3; //y3 = y3; //speed3 = speed3; //d3 = d3; fill(0,0,255); ellipse(x3,y3,d3,d3); if(y3<=height) { y3 = y3 + speed3; //d = d3; } else { speed3 = random(1,9); y3 = 0; d3 = random(5,40); x3 = random(0,width); } 元のソースコードでうまく動かなかった原因ですが, >x1 = x1; >y1 = y1; >speed1 = speed1; >d1 = d1; ここらへんですね. x = x1; y = y1; speed = speed1; d = d1; の誤りです. わざわざx,y,d,speedに代入する必要もないので, 使用しない様に直しました. ほかにも >y= y + speed1; >d = random(5,40); >y = 0; >x = random(0,width); あたりが間違っていますが...

関連するQ&A

  • 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プログラミング作成をしています。 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); } }

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

  • プログラミングの課題です。

    input.bmpというビットマップファイル(色数は24ビット)とbackground.bmpというファイルを開き,クロマキー合成をし,ビットマップファイルoutput.bmpに出力させる。ただしimput.bmpが、青:100以上 緑:150以下 赤:70以下なら背景とみなしてよい。 #include <stdio.h> int main() { FILE *fp1,*fp2,*fp3; char pixeldata[54]; int width,height; int i,j; fp1=fopen("input.bmp","rb"); if(fp1==NULL) return(1); //fp1のヘッダー情報の読み込み fp2=fopen("background.bmp","rb"); if(fp2==NULL) return(1); //fp2のヘッダー情報の読み込み fp3=fopen("output.bmp","wb"); if(fp3==NULL) return(1); //fp3のヘッダー情報の書き込み fread(pixeldata,1,54,fp1); fread(pixeldata,1,54,fp2); fwrite(pixeldata,1,54,fp3); printf("input.bmpの縦は何ピクセルですか?"); scanf("%d",&height); printf("input.bmpの横は何ピクセルですか?"); scanf("%d",&width); for(i=0;i<height*width*3;i++){ //画素情報の読み込み(画像サイズ分繰返し) fgetc(fp1); //1画素分のRGBの情報をfp1から読み込み fgetc(fp2); //1画素分のRGBの情報をfp2から読み込み if(){ //fp1から読み込んだ画素が青い fputc(,fp3); //fp3へfp2で読み込んだ1画素分の情報を書き込み } else{//fp1から読み込んだ画素が青くない fputc(,fp3); //fp3へfp1で読み込んだ1画素分の情報を書き込み } } fclose(fp1);fclose(fp2);fclose(fp3); return(0); } みたいな感じで作りましたが、後半がどうやったらいいかわかりません。 どうしたらいいかアドバイスや解説お願いします。

  • プログラミング(JAVA)について

    2つほど質問があります。どなたか回答していただける方がいたらお願いします。 1、以下のじゃんけんゲームのプログラムを作成したのですがfor文を用いてコードを短くするにはどうすればいいですか?      class jyanken { public static void main(String[] args) { int x = 0, y = 0; if (args[0].equals("グー")) { x = 0; } else if (args[0].equals("チョキ")) { x = 1; } else if (args[0].equals("パー")) { x = 2; } else { System.out.println("エラー"); } y = (int)(Math.random() * 10.0) % 3; if (x == 0) { if (y == 0) { System.out.println("あいこ"); } else if (y == 1) { System.out.println("勝ち"); } else if (y == 2) { System.out.println("負け"); } } else if (x == 1) { if (y == 1) { System.out.println("あいこ"); } else if (y == 2) { System.out.println("勝ち"); } else if (y == 0) { System.out.println("負け"); } } else if (x == 2) { if (y == 2) { System.out.println("あいこ"); } else if (y == 0) { System.out.println("勝ち"); } else if (y == 1) { System.out.println("負け"); } } } } 2、もう1パターンでじゃんけんプログラムを作成しようと考えているのですが。上手くできないので参考のファイルを掲示してもらえると嬉しいです。 機能としては、for文とif文を使用して。プログラムの起動と同時に、コンピュータがグー、チョキ、パーをランダムに出す(プレーヤに見せない) 次に人がグー、チョキ、パー何を出すかをキーボードで入力して最後にプログラムが出したものとコンピュータがランダムに出したものと比較し、勝負を表示する。といった感じです。

  • ASで降らせた雪のマスクの掛け方について。

    下記のサイトのASを使用させていただき、雪の降るFlashを作成したのですが、 参照サイト : http://www.webdesignlibrary.jp/2006/06/flash_snow.php このスクリプトで降らせた雪を、例えば星型の図形をマスクにして、 その範囲内で雪が降るようにしたいと思い、 色々と丸一日掛けて調べたのですが、全く上手く行きません。 とりあえず、色々と調べてみた結果、 【 xxx.setMask 】 を使えば良いのだろうか・・?、、、と言う所までは 行き着いたのですが、スクリプトを、どう改造?してみても、全く思う結果が得られません。 【 _root.setMask 】 で、ステージ全体を、目的の図形MCでマスクする事で、 思う結果に近しいカタチは出来たのですが、_root.setMask でステージをマスクしてしまうと、 例えば、マスク外に何か画像処理を施したくても、マスク外の画像は全て消えてしまいますし、 その画像の大きさ込みでマスクを作成すると、その画像の上にも雪が降ってしまうので、 完全に行き詰ってしまっています。 ASで降らせた雪だけをマスクする方法はありますでしょうか? どなたか、ご存知の方がいらっしゃいましたらば、 是非にご教示願えますと、大変ありがたいです。  何卒よろしくお願い申し上げますm(_ _)m 以下、使用AS ------------------------------------------------ width = 550; height = 400; total = 200; for (var t = 0; t != total; t++) { var mc = _root.attachMovie("snowflake", "snowflake"+t, _root.getNextHighestDepth()); mc._x = (Math.random()*(width+20))-10; mc._y = (Math.random()*(height+20))-10; mc.yspeed = (Math.random()*1.75)+0.25; mc.speed = (Math.random()*3)+2; mc._xscale = mc._yscale=(mc.speed+mc.yspeed)*10; mc.onEnterFrame = function() { var angle = Math.atan2(_root._xmouse-(width/2), _root._ymouse)+1.5707963267949; this._y += Math.sin(angle)*this.speed+this.yspeed; this._x += Math.cos(angle)*this.speed; if (this._x>width+10) { this._x = -10; } else if (this._x<0-10) { this._x = width+10; } if (this._y>height+10) { this._y = -10; } else if (this._y<0-10) { this._y = height+10; } }; } ------------------------------------------------ 【 AS使用環境 】 Adobe Flash CS3, アクションスクリプト2.0

    • ベストアンサー
    • Flash
  • どうプログラミングすればよいか教えてください。

    HTMLは <body> <div id="wrapper"> <div id="header"></div> <div id="left"></div> <div id="middle"></div> <div id="right"></div> <div id="footer"></div> </div> </body> スタイルシートは <style type="text/css"> * { padding: 0px; margin: 0px; } #wrapper { width: 900px; margin-right: auto; margin-left: auto; } #header { width: 100%; height: 200px; background: red; } #left { width: 20%; height: 400px; background: blue; float: left; } #middle { width: 60%; height: 400px; background: orange; float: left; left: 20%; } #right { width: 20%; height: 400px; background: yellow; float: left; left: 80%; } #footer { width: 100%; height: 100px; background: green; clear: both; } </style> 上記のプログラムだとmiddleエリアに文字を沢山記入していくとエリアの縦の長さが 固定されていてエリア自体の範囲は延長されず文字だけが増えていきます。 文字の量に応じてleftエリア、middleエリア、rightエリアの 縦の長さが延長されるようになるにはどうプログラムすればいいでしょうか。 要するに オレンジのエリアに文字を記入すると掲載画像のように文字だけが表示されて 青、オレンジ、黄色のエリアは固定されたままです。 これを、どうすれば文字の量に応じて青、オレンジ、黄色のエリアが延長されて 表示されるか教えてください。

    • ベストアンサー
    • CSS
  • Rubyプログラミングの課題について☆

    大学のレポートで下のような課題が出て、実際プログラムを自分で作って実行してみたのですが、全く無反応でいくら考えても原因がわかりません(T_T)批判されそうな質問かもしれないですが、留年かかっているので親切な方、助言お願いします。。 ●下の動作をするプログラム(a.rb)を作れ  %a.rb "+[1,20] =>21 %a.rb "*[-1,25,4] =>-100 ●作ってみたプログラム↓ class Array def list_ope d=0 #最終的に返す値 s=0 #状態番号 x="" #一時的に数値を入れる op="" #[+,-,*,/] sgn=1 #マイナスの符号のあるなし err=999999 self.each{|i| case s #とりあえず、[+,-,*,/]をopに代入 when 0 op=i s=1 when 1 if /\[/ =~ i  s=2 else s=err; bleak end when 2 sgn=1; x=0; if /[1-9]/ =~ i #1~9の時 x+=i; s=3 elsif /-/ =~ i #マイナスの時 sgn=-1; s=3 else s=err; bleak end when 3 if /[0-9]/ =~ i #0~9の時 x+=i; elsif /,/ =~ i #コンマの時 x=sgn*x.to_i; s=4 elsif /\]/ =~ i #]の時 x=sgn*x.to_i; s=5 else s=err; bleak end when 4 #コンマの時の処理 if /\+/ =~op d=d+x elsif /-/ =~ op d=d-x elsif /\*/ =~ op d=d*x elsif / =~ op d=d/x end s=2 when 5 #]のときの処理 if /\+/ =~op d=d+x elsif /-/ =~ op d=d-x elsif /\*/ =~ op d=d*x elsif / =~ op d=d/x end s=6 #終了状態 end } if s==6 return d #dを返す else print "error!" end end end

  • HPに写真を載せる方法

    HPに写真を載せるに当たって、アクセスのたび4枚の写真を順番に表示させたいのです。 写真をA~Dとした場合、ランダムならば <SCRIPT Language="JavaScript"> <!-- var imgMax = 4 var imgRand = Math.floor(Math.random() * imgMax); if(imgRand == 0) document.write('<img src="A.jpg" width=380 height=285>'); else if(imgRand == 1) document.write('<img src="B.jpg" width=380 height=285>'); else if(imgRand == 2) document.write('<img src="C.jpg" width=380 height=285>'); else if(imgRand == 3) document.write('<img src="D.jpg" width=380 height=285>'); //--> </SCRIPT>  ここまでは判ったのですが・・・・ A⇒B⇒C⇒D Aに戻る に、変更するには どうしたらよいのでしょうか? 初心者で、専門用語は判りませんが宜しくお願い致します。

  • コードの最適化について質問です。以下の2つのコードにおいて、最適化が可能なポイントを教えていただけませんか?

    *gccコンパイラを通します。 ///code1/// void filter_optimized(int width, int height, int numcol, int rw, int gw, int bw) { int x, y, z; for (z=0; z<numcol; z++) for (x=0; x<height; x++) for (y=0; y<width; y++) if (z==0) c[x][y][z] = bw*((bw*a[x][y][0]+gw*a[x][y][1]+rw*a[x][y][2])/(rw+gw+bw)); else if(z==1) c[x][y][z] = gw*((bw*a[x][y][0]+gw*a[x][y][1]+rw*a[x][y][2])/(rw+gw+bw)); else c[x][y][z] = rw*((bw*a[x][y][0]+gw*a[x][y][1]+rw*a[x][y][2])/(rw+gw+bw)); return; } ///code2/// void edge_optimized(int width, int height, int numcol) { int x, y, z; int tmp; for (y = 0; y < width; y++) for (x = 0; x < height; x++) for (z = 0; z < numcol; z++) b[x][y][z] = (a[x][y][0]+a[x][y][1]+a[x][y][2])/3; for (y = 0; y < width; y++) for (x = 0; x < height; x++) for (z = 0; z < numcol; z++) { tmp = computeGx(width, height, x, y, z, b); if (tmp < 0) tmp = -tmp; if (tmp > 255) tmp = 255; c[x][y][z] = tmp; tmp = computeGy(width, height, x, y, z, b); if (tmp < 0) tmp = -tmp; if (tmp + c[x][y][z] > 255) c[x][y][z] = 255; else c[x][y][z] += tmp; c[x][y][z] = 255 - c[x][y][z]; if ((x==0)||(y==0)||(x==height-1)||(y==width-1)) c[x][y][z] = 255; } return; }

専門家に質問してみよう