延續上周,寫出一個可以把音樂播放出來的程式
聲音播放和切換場景
第一個程式:week08_1_play_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(){
}
總結一下:
設定字型sizetextSize(50);
字型顏色fill(255,0,0);
設定變數SoundFile sound1,sound2;
撥放音樂sound1.play();
暫停音樂sound1.stop();
讀取音檔 sound1=new SoundFile(this,"xxx.mp3");
設定變數切換場景int stage
設定切換場景&停止及撥放音樂mousePressed + if-else
設定兩個舞台,在舞台上寫上stage編號
ex:text( "stage 1",100,100); //在畫面上100,100的地方印出stage 1
第二個程式:
寫出一個程式,可以切換舞台&音樂
import processing.sound.*;
SoundFile sound1,sound2,sound3;
//(2)程式存檔,把音樂檔拉進來
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){
stage=1;
sound2.stop();
sound1.play();
}
}
第三個程式:week08_3_simple_stage1_stage2
由於第二程式過於複雜,這邊把切換舞台的程式精簡並單獨寫出來
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;
}
第四個程式:week08_4_fruit_flying_keyboard_pressed
做一個類似水果忍者的遊戲,只要水果飛起來,使用按鍵就可以使它消除掉
設定float變數 fruitX/Y控制水果的位置,加速度float fruitVX=1,fruitVY=-1;
boolean則是表示控制現在使否往上飛:boolean flying=true;
keyPressed函式表示當按下時停止,flying=false
//目標:有個水果可以飛起來!!!
//按按鍵可以把它消掉
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;
}
第五個程式:week08_5_fruit_reset
稍微改變一下水果的落下速度,並且在水果落下去之後會有另一發水果跳上來(keyPressed函式裡面放入fruitReset())
//目標:有個水果可以飛起來!!!
//按按鍵可以把它消掉
void setup(){
size(400,300);
}
float fruitX=200,fruitY=150;//水果的位置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);//讓X隨機100~300之間的位置生成
fruitY=300;//固定高度
fruitVX=random(-2,+2);//隨機切換往左還是往右飛
fruitVY=-13;
flying=true;//從false改成true
}
第六個程式:week08_6_furit_class_tabs
設定一個Class Fruit,設定好x,y,vx,vy,boolean變數
//目標: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 update(){
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.update();
}
void keyPressed(){
fruit.reset();
}
第七個程式:week08_7_fruit_class_tabs
會有三個寫了字母的水果會跳起來,另外新增一個副程式名為fruit
呼叫PApplet sketch; 函式庫(PApplet sketch;是為了後面要可以用random函式)
\class呼叫自己 Fruit(PApplet _sketch)來產生新的水果
PApplet sketch; 函式庫裡面的random要寫成sketch.random()
設定reset和update的函式,主程式的地方改成用Fruit的Class
程式會分成兩個部分
第一個部分(主程式):
Fruit [] fruits;
void setup(){
size(400,300);
fruits = new Fruit[3];
for(int i=0;i<3;i++){
fruits[i] = new Fruit(this);//為了讓random可以用,修改一下
}
}
void draw(){
background(255,255,0);
for(int i=0;i<3;i++){
fill(255);ellipse(fruits[i].x,fruits[i].y,50,50);
textSize(30);
textAlign(CENTER,CENTER);
fill(0); text(fruits[i].c,fruits[i].x,fruits[i].y);
fruits[i].update();
}
}
void keyPressed(){
for(int i=0;i<3;i++){
if(keyCode == fruits[i].c){
fruits[i].reset();
}
}
}
副程式(Fruit):
//目標:class物件:每個水果都可以用物件生出來(值、函式)
String line = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";//陣列放入所有英文字母
class Fruit{
float x,y,vx,vy;
boolean flying;
char c;//水果對應的字母
PApplet sketch;//class呼叫PApplet sketch的class為了Random可以使用
Fruit(PApplet _sketch){//建構子:一開始要做的事情(就是呼叫自己來產生另外一顆水果)
sketch = _sketch;//設sketch
reset();
}
void reset(){//水果位置
x=sketch.random(100.0,300.0);
y=300;
vx=sketch.random(-2,+2);
vy=-13;
flying=true;
int i=int(random(26));
c=line.charAt(i);
}
void update(){//水果飛起來的路徑變化
x += vx;
y +=vy;
vy += 0.98/3;//重力加速度
}
}







沒有留言:
張貼留言