質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Processing

Processingは、オープンソースプロジェクトによるCGのためのプログラミング言語です。Javaをベースにしており、グラフィック機能に特化しています。イメージの生成やアニメーションなど、視覚的なフィードバックを簡単に得ることが可能です。

Q&A

解決済

1回答

949閲覧

processing if文動作時にロゴを増やしたい

taki_rentaro

総合スコア6

Processing

Processingは、オープンソースプロジェクトによるCGのためのプログラミング言語です。Javaをベースにしており、グラフィック機能に特化しています。イメージの生成やアニメーションなど、視覚的なフィードバックを簡単に得ることが可能です。

0グッド

0クリップ

投稿2021/12/15 06:13

###前提・実現したいこと

https://www.youtube.com/watch?v=5mGuCdlCcNM&t=24105s
上記動画のようなものを作りたいと考えていて、ロゴが四隅に当たった時(上下の衝突判定が同時に行われた際)にロゴを画面中央に新たに1つ描画したい。

PImage img; // 画像 画像サイズはwidth262,height120 float x_pos,y_pos; // 画像の初期位置 float x_speed = 5; // 画像の移動速度(横) float y_speed = 5; // 画像の移動速度(縦) float r,g,b; // 反射時の色変更 int n = 1; // ロゴの数 float x_direction = random(-1,1); // 右か左か float y_direction = random(-1,1); // 上か下か void setup(){ size(1280,720); frameRate(60); img = loadImage("DVD_logo2.png"); x_pos = width / 2; y_pos = height / 2; } void draw(){ /* 角に当たった時にロゴを一個増やし現在のロゴ数を+1する仕様にする */ background(0); //背景の初期化 DVD_image(); //DVD_image関数呼び出し int s = millis() / 1000; //経過時間(仮) x_pos = x_pos + ( x_speed * x_direction ); y_pos = y_pos + ( y_speed * y_direction ); //壁にぶつかっていたら-1をdirectionに入れる。 if (x_pos > width-(img.width / 2) || x_pos < (img.width / 2)) { x_direction *= -1; tint(r,g,b); } if (y_pos > height-(img.height / 2) || y_pos < (img.height / 2)) { y_direction *= -1; tint(r,g,b); } //角に当たった時の処理  if (x_pos < (img.width / 2) && y_pos < (img.height / 2) ) { //左上 n += 1; DVD_image(); } if (x_pos < (img.width / 2) && y_pos > height - (img.height / 2) ){ //左下 n += 1; DVD_image(); } if (x_pos > width - (img.width / 2) && y_pos < (img.height / 2) ){ //右上 n += 1; DVD_image(); } if (x_pos > width - (img.width / 2) && y_pos > height - (img.height / 2) ){ //右下 n += 1; DVD_image(); } //ロゴのカウント textAlign(CENTER); textSize(24); fill(255,255,255,150); text("now DVDlogos : " + n + "", width/2, 25); //タイマー text("now time : " + s + "", width/2, 50); } void DVD_image(){ //画像の描画及び画像のRGB値 imageMode(CENTER); image(img, x_pos, y_pos); r = random(80,255); g = random(80,255); b = random(80,255); }

//角に当たった時の処理 のif文に関数を呼び出す処理を足せばよいものだと思っていたのですが、どうやら違ったようなのでお教えいただけないでしょうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ロゴを画面中央に新たに1つ描画したい。

どんどんロゴが増えていくということですよね?
だったらx_pos等の変数も、個数分必要になりますよね?

こういった場合は、クラスとArrayListを使用するとすっきり書けます。
ArrayList of objects / Examples / Processing.org

Processing

1PImage img; 2ArrayList<Logo> logos = new ArrayList<Logo>(); 3 4 5void setup() { 6 size(1280, 720); 7 // frameRate(60); 8 img = loadImage("https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail.jpg"); 9 10 // 偶然角に当たるのを待ってらんないのでw 11 //Logo l = new Logo(); 12 //l.vx = 4.4225655130783; 13 //l.vy = 2.3325767474042; 14 //logos.add(l); 15 16 logos.add(new Logo()); 17} 18 19void draw() { 20 background(0); 21 22 int hitCount = 0; 23 for (Logo logo : logos) { 24 logo.update(); 25 26 if (logo.hitCorner()) { 27 hitCount++; 28 } 29 30 logo.draw(); 31 } 32 33 for (int i = 0; i < hitCount; i++) { 34 logos.add(new Logo()); 35 } 36 37 38 textAlign(CENTER); 39 textSize(24); 40 fill(255, 255, 255, 150); 41 text("now DVDlogos : " + logos.size(), width / 2, 25); 42 43 int s = millis() / 1000; 44 text("now time : " + s, width / 2, 50); 45} 46 47 48class Logo { 49 float x, y; 50 float vx, vy; 51 color c = #FFFFFF; 52 53 Logo() { 54 x = width / 2; 55 y = height / 2; 56 57 float a = random(TWO_PI); 58 vx = cos(a) * 5; 59 vy = sin(a) * 5; 60 } 61 62 void update() { 63 x += vx; 64 y += vy; 65 66 if (width - (img.width / 2) < x || x < (img.width / 2)) { 67 vx *= -1; 68 changeColor(); 69 } 70 if (height - (img.height / 2) < y || y < (img.height / 2)) { 71 vy *= -1; 72 changeColor(); 73 } 74 } 75 76 boolean hitCorner() { 77 if (x < (img.width / 2) && y < (img.height / 2)) { 78 return true; 79 } 80 if (x < (img.width / 2) && height - (img.height / 2) < y) { 81 return true; 82 } 83 if (width - (img.width / 2) < x && y < (img.height / 2)) { 84 return true; 85 } 86 if (width - (img.width / 2) < x && height - (img.height / 2) < y) { 87 return true; 88 } 89 90 return false; 91 } 92 93 void draw() { 94 imageMode(CENTER); 95 tint(c); 96 image(img, x, y); 97 } 98 99 void changeColor() { 100 colorMode(HSB, 360, 100, 100); 101 c = color(random(360), 100, 100); 102 colorMode(RGB, 255, 255, 255); 103 } 104}

処理を書く場所が変わった程度ですが、わからない点があればコメントしてください。

投稿2021/12/15 09:19

TN8001

総合スコア9401

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問