1. 播放音樂
(1) 安裝Sound模組
Code:
import processing.sound.*; // 匯入音樂播放的套件
SoundFile player; // 宣告音樂播放器
void setup() {
// 初始化音樂播放器,並傳入音檔檔名
player = new SoundFile(this, "Wish_You_d_Come_True.mp3");
player.play(); // 播放音檔
}
void draw() {
}
2. 模擬氣球
Code:
PImage imgBallon;
ArrayList<Float> ballonX = new ArrayList<Float>();
ArrayList<Float> ballonY = new ArrayList<Float>();
ArrayList<Float> ballonSize = new ArrayList<Float>();
int nowN = 0; // 存共產生了多少氣球
void setup() {
size(500, 400);
imgBallon = loadImage("ballon.png");
}
void draw() {
background(255);
// 畫全部的氣球
for (int i=0; i<nowN; i++) {
image(imgBallon, ballonX.get(i), ballonY.get(i),
ballonSize.get(i), ballonSize.get(i)/2*3);
// 讓氣球往上飄
ballonY.set(i, ballonY.get(i)-1);
if (ballonY.get(i)-1 < 0) ballonY.set(i, 0f);
}
if (mousePressed) setBallon(nowN-1, ballonSize.get(nowN-1)*1.05);
}
// 設定氣球大小的函數
void setBallon(int i, float size) {
ballonSize.set(i, size);
ballonX.set(i, mouseX - ballonSize.get(i)/2);
ballonY.set(i, mouseY - ballonSize.get(i)/2*3);
print(ballonSize.size());
print(ballonX.size());
print(ballonY.size());
println();
}
void mousePressed() {
if (mouseButton == LEFT) {
ballonSize.add(10f);
ballonX.add(0f);
ballonY.add(0f);
setBallon(nowN, 10f);
nowN ++;
} else if (mouseButton == RIGHT) {
// 點右鍵讓氣球爆炸
for (int i=0; i<nowN; i++) {
if (mouseX > ballonX.get(i) && mouseX < ballonX.get(i)+ballonSize.get(i)
&& mouseY > ballonY.get(i) && mouseY < ballonY.get(i)+ballonSize.get(i)) {
ballonSize.set(i, 0f);
}
}
}
}


沒有留言:
張貼留言