week08 水果忍者
- 加入音檔
- 程式素描本-使用函式庫-install sound程式
```java
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. 切換音檔
```java
import processing.sound.*;
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){
text("stage 1", 100, 100);
}else if (stage==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();
}
}```
3. stage切換
```java
void setup(){
size(400, 300);
}
int stage =1;//1, 2, 3
void draw(){
background(255, 255, 0);
fill(255, 0, 0);
textSize(80);
if(stage==1){
text("stage 1", 100, 100);
}else if (stage==2){
text("stage 2", 100, 100);
}
}
void mousePressed(){
if(stage==1) stage=2;
else if (stage==2) stage=1;
}```
4. 加入會飛的水果,按下鍵盤暫停
```java
//目標: 有個水果可以飛起來
//按按鍵, 可以把它消掉
void setup(){
size(400, 300);
}
float fruitX=200, fruitY=150;//水果的位置 X Y有小數點, 精確
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;
}```
```java
//目標: 有個水果可以飛起來
//按按鍵, 可以把它消掉
void setup(){
size(400, 300);
}
float fruitX=200, fruitY=300;//水果的位置 X Y有小數點, 精確
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;
}
```
6. 修改程式讓random可以用
```java
//目標: 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();
}```
7.在水果加上英文字母,按下對應英文字重新發送水果
```java
String line="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
class Fruit{
float x, y, vx, vy;
boolean flying;
char c;//水果對應的英文
PApplet sketch;
Fruit(PApplet _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;//重力加速度
}
}
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();
}
}
}```







沒有留言:
張貼留言