/* @pjs preload="cake.png, icec.png, ichigo.png, tarte.png, strawberry.png, tiramisu.png, house.png, applepie.png, gingerbread.png, macaroon.png"; */ final float G = 1.4; //重力 final float S = 1.0; //地面で止める速度 final float H = 0.65; //反発力 //画像 final String[] GAZO = {"cake.png", "icec.png", "ichigo.png", "tarte.png", "strawberry.png", "tiramisu.png", "house.png", "applepie.png", "gingerbread.png", "macaroon.png"}; //星の数 final int STARS = 20; //背景画像 PImage bgimg; //月画像 PImage moonimg; //月画像 PImage moonimg2; //月の周回角度 float angle = 0; class item { PImage img; float x; float y; float speedX; float speedY; boolean jump; int heartTime; float[] oldX; float[] oldY; int frameNumber = 5; float[] heartPlusX; float[] heartPlusY; color[] heartColor = {#98FBFF, #FF98BC, #FFD898, #C298FF, #B1FF98}; //ハートをずらすサイズ final float heartRandom = 60; //お菓子を作る処理 item(float x1, float x2, String file) { img = loadImage(file); x = random(x1, x2); y = height - img.height; speedX = 0; speedY = 0; jump = false; if (x > width - img.width) { x = width - img.width; } heartTime = 0; oldX = new float[frameNumber]; oldY = new float[frameNumber]; heartPlusX = new float[5]; heartPlusY = new float[5]; } //お菓子の座標 void move() { if ( jump ) { //重力の計算 speedY = speedY + G; //座標の計算 x = x + speedX; y = y + speedY; //時間の計算 if ( heartTime > 0 ) { heartTime = heartTime - 1; } //地面に衝突 if ( y >= height - img.height ) { //最初の高さに戻す y = height - img.height; speedY = speedY * - H; heartTime = int( random(1, 10 )); heartPlus(); } //左の壁に衝突 if ( x <= 0 ) { x = 0; speedX = speedX * - H; } //右の壁に衝突 if ( x >= width - img.width ) { x = width - img.width; speedX = speedX * - H; } //速度と高さが一定以下のときに止める if ( (abs(speedY) < S) && (y >= height - img.height * 1.5)) { speedX = 0; speedY = 0; y = height - img.height; jump = false; } } //座標の記憶 oldX[ frameCount % frameNumber ] = x; oldY[ frameCount % frameNumber ] = y; } void click() { if (mousePressed == true) { if ((mouseX > x)&&(mouseX < x + img.width) && (mouseY > height - img.height) &&(mouseY < height) && !jump) { //画像をクリック jump = true; speedX = random(-10, 10); speedY -= random(25, 37); //時間を設定 heartTime = int( random(10, 15 )); heartPlus(); } } } void heartPlus() { //ハートをずらす座標を決定 for (int i = 0; i < heartPlusX.length; i++) { heartPlusX[i] = random(- heartRandom, heartRandom); heartPlusY[i] = random(- heartRandom, heartRandom); } } void display() { //時間が0より大きいときは星を表示 if ( heartTime > 0 ) { if (jump) { for (int i = 0; i< heartPlusX.length; i++ ) { heart(oldX[(frameCount + 3) % frameNumber] + heartPlusX[i] , oldY[(frameCount + 3) % frameNumber] + heartPlusY[i], heartColor[i]); } } } image(img, x, y); } //ハートを書く関数 void heart(float heartX, float heartY, color heartColor) { //ハートの大きさ float R = 0.4; //ハートの色 fill(heartColor); pushMatrix(); translate(heartX + img.width / 2.0, heartY + img.height / 2.0); strokeJoin(ROUND); beginShape(); for (int theta = 0; theta < 360; theta++) { heartX = R * (16 * sin(radians(theta)) * sin(radians(theta)) * sin(radians(theta))); heartY = (-1) * R * (13 * cos(radians(theta)) - 5 * cos(radians(2 * theta)) - 2 * cos(radians(3 * theta)) - cos(radians(4 * theta))); vertex(heartX, heartY); } endShape(CLOSE); popMatrix(); } } item[] items = new item[GAZO.length]; //画像の数だけ配列を作る class star { float x; float y; float speed; float size; star() { x = random(0, width); y = random(0, height); speed = random(0.2, 0.5); size = random(0.2, 0.4); } void move() { //星を動かす x = ( x + speed ) % ( width - 50 * 0.5); } void twinkle() { noStroke(); if (round(random(0, 100)) <= 94) { //星の色 fill(#FDFFA2); pushMatrix(); translate(x, y); scale(size); 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); popMatrix(); } } } star[] stars = new star[STARS]; //月を表示 void moon() { //円の中心 float origin = 250; //円の半径 float r = 270; //周回の速さ float speed = 2.5; angle = angle + speed; pushMatrix(); translate(width / 2, height / 2 + origin); rotate(radians(angle)); image(moonimg, 0, r); popMatrix(); pushMatrix(); translate(width / 2, height / 2 + origin); rotate(radians(angle + 180)); image(moonimg2, 0, r); popMatrix(); } void setup() { size(500, 500); frameRate(20); colorMode(HSB); bgimg = loadImage("2.jpeg"); image(bgimg, 0, 0); moonimg = loadImage("moon.png"); moonimg2 = loadImage("moon2.png"); //アイテムを作る for (int i = 0; i < GAZO.length; i++) { items[i] = new item(width / GAZO.length * i, width / GAZO.length * ( i + 1 ), GAZO[i]); } //星を作る for (int i = 0; i < STARS; i ++) { stars[i] = new star(); } } void draw() { //背景を表示 image(bgimg, 0, 0); fill(#FF71CE, 70); noStroke(); rect( 0, -8, 150, 40); fill(#FFDB4B); textSize(20); text("Click sweets", 10, 20); //月を表示 moon(); //星を表示 for (int i = 0; i < stars.length; i++) { stars[i].move(); stars[i].twinkle(); } //画像がクリックされた時の処理 for (int i = 0; i < GAZO.length; i++) { items[i].click(); } //画像の座標を計算する処理 for (int i = 0; i < GAZO.length; i++) { items[i].move(); } //画像を表示する処理 for (int i = 0; i < GAZO.length; i++) { items[i].display(); } }