1-1
上禮拜的最後步驟,加入音樂所以按下撥放後會有In Game Music.mp3為背景音樂。
import processing.sound.*;
SoundFile sound1,sound2,sound3;
void setup(){
size(400,300);
sound1=new SoundFile(this,"In Game Music.mp3");
sound1.play();
}
void draw(){
}
1-2
放入兩個音樂,同時畫面會出現stage1或是stage2按下滑鼠後能將stage1音樂和畫面停止變換成stage2的畫面和音樂。
import processing.sound.*;
SoundFile sound1,sound2,sound3;
void setup(){
size(400,300);
sound1=new SoundFile(this,"In Game Music.mp3");
sound2=new SoundFile(this,"Intro Song_Final.mp3");
sound1.play();
}
int stage=1;
void draw(){
background(255);
textSize(50);
fill(255,0,0);
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();
}
}
1-3
簡化上面stage1、2更換的程式
import processing.sound.*;
SoundFile sound1,sound2,sound3;
void setup(){
size(400,300);
sound1=new SoundFile(this,"In Game Music.mp3");
sound2=new SoundFile(this,"Intro Song_Final.mp3");
sound1.play();
}
int stage=1;
void draw(){
background(255);
textSize(50);
fill(255,0,0);
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;
}//簡化
void setup()
{
size(400,300);
}
float fruitX=200,fruitY=150;
float fruitVX=1,fruitVY=-1;
boolean flying=true;
void draw(){
background(255,255,0);
ellipse(fruitX,fruitY,50,50);
if(flying){
fruitX+=fruitVX;
fruitY+=fruitVY;
}
}
void keyPressed()
{
flying=false;
}
2-2
修改上面球移動的程式碼,更改速度方向。
void setup()
{
size(400,300);
}
float fruitX=200,fruitY=300;
float fruitVX=2,fruitVY=-13;
boolean flying=true;
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;
fruitRest();
}
void fruitRest(){
fruitX=random(100,300);
fruitY=300;
fruitVX=random(-2,+2);
fruitVY=-13;
flying=true;
}
3-1
球會從上面往下掉,沒有字母而且還無法消除。
class Fruit{
float x,y,vx,vy;
boolean flying;
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;
}
void update(){
x+=vx;
y+=vy;
vy+=0.98/3;
}
}
Fruit fruit;
void setup(){
size(400,400);
fruit=new Fruit(this);
}
void draw(){
background(255,255,0);
ellipse(fruit.x,fruit.y,50,50);
fruit.update();
}
void keyPressed()
{
fruit.reset();
}
3-2
開一個新的分頁名字是Fruit,當有英文字母的球掉下來,你輸入同樣的字母球就會被消除。
Fruit [] fruits;
void setup(){
size(400,400);
fruits=new Fruit[3];
for(int i=0;i<3;i++){
fruits[i]=new Fruit(this);
}
}
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程式碼
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;
}
}







沒有留言:
張貼留言