A
1.播放音樂
//(0) 先照Moodle在網路下載 mp3檔
//(1) 安裝 Sketch-Library-Manage Libraries 裝 Sound
import processing.sound.*;
SoundFile sound1, sound2, sound3;
//存檔, 把音樂檔拉進去
void setup(){
size(400,300);
sound1 = new SoundFile(this, "In Game Music.mp3");
sound1.play();
}
void draw(){
}
2.能夠切換音樂
import processing.sound.*;
SoundFile sound1, sound2, sound3;
//存檔, 把音樂檔拉進去
void setup(){
size(400,300);
textSize(50);
fill(255,0,0);
sound1 = new SoundFile(this, "In Game Music.mp3");
sound2 = new SoundFile(this, "Intro Song_Final.mp3");
sound1.play();
}
int stage=1;//1,2,3
void draw(){
background(255);
if(stage==1){//舞台1
text("stage 1", 100,100);
}else if(stage==2){//舞台2
text("stage 2", 100,100);
}
}
void mousePressed(){
if(stage==1){
stage=2;
sound1.stop();
sound2.play();
}else if(stage==2){//舞台1
stage=1;
sound2.stop();
sound1.play();
}
}
3. 簡化
void setup(){
size(400,300);
}
int stage=1;//1:start, 2:playing
void draw(){
background(255,255,0);
fill(255,0,0);
textSize(80);
if(stage==1){//舞台1
text("stage 1", 100,100);
}else if(stage==2){//舞台2
text("stage 2", 100,100);
}
}
void mousePressed(){
if(stage==1) stage=2;
else if(stage==2) stage=1;
}
B
1.移動的水果且點擊會停止
void setup(){
size(400,300);
}
float fruitX=200, fruitY=150;//水果的位置 XY 有小數點,精確
float fruitVX=1, fruitVY=-1;//水果的速度 VX VY
boolean flying=true;//if()好朋友,布林變數 true false 不成立
void draw(){
background(255,255,0);//黃色背景
ellipse(fruitX, fruitY, 50, 50);
if(flying){//如果在飛,水果的位置會改變
fruitX += fruitVX;
fruitY += fruitVY;
}
}
void keyPressed(){
flying=false;
}
2.水果會下墜且能夠消除並更新
//目標: 有個水果可以飛
//按按鍵,可以清除他
void setup(){
size(400,300);
}
float fruitX=200, fruitY=300;//水果的位置 XY 有小數點,精確
float fruitVX=2, fruitVY=-13;//水果的速度 VX VY
boolean flying=true;//if()好朋友,布林變數 true false 不成立
void draw(){
background(255,255,0);//黃色背景
ellipse(fruitX, fruitY, 50, 50);
if(flying){//如果在飛,水果的位置會改變
fruitX += fruitVX;
fruitY += fruitVY;
fruitVY += 0.98/3;//重力加速度
}
}
void keyPressed(){
flying=false;
fruitReset();///重新準備另一水果
}
void fruitReset(){
fruitX=random(100,300);
fruitY=300;//固定高度
fruitVX=random(-2,+2);
fruitVY=-13;
flying=true;
}
3.用class
//目標: class物件: 每個水果都可用物件生出來(值,函式)
class Fruit{
float x, y, vx, vy;
boolean flying;
PApplet sketch;//為了讓random可以用,修改一下
Fruit(PApplet _sketch){ //建構子: 一開始要做的事
sketch = _sketch;//為了讓random可以用,修改一下
reset();
}
void reset(){
x = sketch.random(100.0, 300.0);//為了讓random可以用,修改一下
y = 300;
vx = sketch.random(-2,+2);//為了讓random可以用,修改一下
vy = -13;
flying = true;
}
void updata(){
x += vx;
y += vy;
vy += 0.98/3;//重力加速度
}
}
Fruit fruit;
void setup(){
size(400,300);
fruit = new Fruit(this);//為了讓random可以用,修改一下
}
void draw(){
background(255,255,0);
ellipse(fruit.x, fruit.y, 50, 50);
fruit.updata();
}
void keyPressed(){
fruit.reset();
}






沒有留言:
張貼留言