2022年10月24日 星期一

END interaction week08

 1.sound.play

(sketch - library -manage lib... - "sound" install)

下載音檔放程式介面,不是資料夾


import processing.sound.*;

SoundFile sound1,s2,s3;


void setup()

{

  size(400,300);

  textSize(30);

  fill(0,255,0);

  sound1=new SoundFile(this,"Gong.mp3");

  s2=new SoundFile(this,"Freezing.mp3");

  sound1.play();

}


int stage=1;


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();

    s2.play();

  }

  else if(stage==2)

  {

    stage=1;

    s2.stop();

    sound1.play();

  }

}


2.moving fake fruit


void setup()

{

  size(400,300);

}


float fruitx=200,fruity=150;/// fruit coordinate

float fruitvx=1,fruitvy=-1;///fruit speed vx vy

boolean fly=true;///boolean - 成立不成立


void draw()

{

  background(255,0,255);

  ellipse(fruitx,fruity,50,50);

  if(fly)

  {

    fruitx+=fruitvx;

    fruity+=fruitvy;

  }

}


void mousePressed()

{

  fly=false;

}





3.up and down

void setup()
{
  size(400,300);
}

float fruitx=200,fruity=300;/// fruit coordinate
float fruitvx=2,fruitvy=-13;///fruit speed vx vy
boolean fly=true;///boolean - 成立不成立

void draw()
{
  background(255,0,255);
  ellipse(fruitx,fruity,50,50);
  if(fly)
  {
    fruitx+=fruitvx;
    fruity+=fruitvy;
    fruitvy+= 0.98/3;///gravity+speed
  }
}

void mousePressed()
{
  fly=false;
  reset();
}

void reset()
{
  fruitx=random(100,300);
  fruity=300;
  fruitvx=random(-2,2);
  fruitvy=-13;
  fly=true;
}


4.另類寫法?

class FRUIT///class物件:物件生成每個水果(值、函式)
{
  float x,y,vx,vy;
  boolean fly;
  PApplet sketch;///使random 可使用的修改
  FRUIT(PApplet _sketch)///建構子:the first thing to do
  {
    sketch= _sketch;///使random 可使用的修改
    reset();
  }
  
  void reset()
  {
    x=sketch.random(100.0,300.0);///使random 可使用的修改
    y=300;
    vx=sketch.random(-2,2);///使random 可使用的修改
    vy=-13;
    fly=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,0,255);
  ellipse(fruit.x,fruit.y,50,50);
  fruit.update();
}

void mousePressed()
{
  fruit.reset();
}

5.字母(新增分頁)

(1)week08-5
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,0,255);
  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 mousePressed()
{
  for(int i=0;i<3;i++)
  {
    if(keyCode == fruits[i].c)
    {
      fruits[i].reset();
    }
  }
}

(2)fruit
String line="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
class FRUIT
{
  float x,y,vx,vy;
  boolean fly;
  char c;
  PApplet sketch;
  FRUIT(PApplet _sketch)
  {
    sketch= _sketch;
    reset();
  }
  
  void reset()
  {
    x=sketch.random(100.0,300.0);///使random 可使用的修改
    y=300;
    vx=sketch.random(-2,2);///使random 可使用的修改
    vy=-13;
    fly=true;
    int i=int(random(26));
    c=line.charAt(i);
  }
  
  void update()
  {
    x+=vx;
    y+=vy;
    vy+=0.98/3;
  }
}




沒有留言:

張貼留言