2022年10月24日 星期一

互動程式week08

播放音檔

import processing.sound.*; #使用sound函式庫所有內容
SoundFile file1;#宣告音檔
file1 =new SoundFile(this,"In Game Music.mp3");
file1.play();#撥放音檔


轉換舞台

int stage=1;
void draw()
{
  background(125);
  if(stage == 1)
  {
    text("stage1",100,100);
  }
  else if(stage == 2)
  {
    text("stage2",100,100);
  }
}
void mousePressed()
{
  if(stage == 1)
  {
    stage = 2;
    file1.stop();
    file2.play();
  }
  else if(stage == 2)
  {
    stage = 1;
    file2.stop();
    file1.play();
  }
}
水果忍者(用類別class)
week08_3
Fruit[] fruits;
void setup()
{
  size(400,300);
  fruits = new Fruit[3];
  for(int  i=0;i<3;i++)
  {
    fruits[i] = new Fruit(this);
  }
}
void draw()
{
  background(125);
  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;
  }
}


沒有留言:

張貼留言