⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 life.pde

📁 这是一个很经典的编程项目
💻 PDE
字号:
// Based on the work of Bjoern Froehli. Many thanks!

import promidi.*;

class Life {
  
  // The state of the life object
  final static int DEAD = -1;
  final static int BORN = 0;
  final static int ALIVE = 1;
  
  int state;

  // Midi
  MidiIO midiIO;
  MidiOut midiout;
  int midiInstrument; 
  int midiLength;
  int midiVelocity; 
  Note note;

  // x and y corner position, width and height
  int x, y, w, h;
  
  Life(int winWidth, int winHeight, int row, int col, int idx, int idy) {
    
    // Calculate width and height
    w = winWidth / row;
    h = winHeight / col;
    // Position in the grid
    x = w * idx;
    y = h * idy;
    // Not born yet
    state = Life.DEAD;
    
    // Midi instrument
    midiInstrument = 30; 
    midiLength = 20;
    midiVelocity = 80;
    // Midi object
    midiIO = MidiIO.getInstance();
    midiIO.closeOutput(1);
    midiout = midiIO.getMidiOut(1, 2);    
    midiout.sendProgramChange(new ProgramChange(midiInstrument));
    // Higher notes at the top of the grid
    setNote( (int) ( ((float) idy / col) * 16) );
  }
  
  void display() {
    ellipseMode(CORNER);
    fill(127);
    ellipse(x, y, w, h); 
  }
  
  void playSound() {
      midiout.sendNote(note);
  }  
  
  void clicked(int mx, int my) {
    if (mx > x && mx < x + w && my > y && my < y + h) {
      state = Life.BORN;
    }
  }  
  
  int getState() {
    return state; 
  }
  
  void setState(int state) {
    this.state = state;
  }
  
  void setNote(int n) {
    switch(n) {
      case 0:
        note = new Note(86, midiVelocity, midiLength);
      break;
      case 1:
        note = new Note(83, midiVelocity, midiLength); 
      break;
      case 2:
        note = new Note(79, midiVelocity, midiLength);
      break;
      case 3:
        note = new Note(74, midiVelocity, midiLength);
      break;
      case 4:
        note = new Note(71, midiVelocity, midiLength);  
      break;
      case 5:
        note = new Note(67, midiVelocity, midiLength);
      break;
      case 6:
        note = new Note(62, midiVelocity, midiLength); 
      break;
      case 7:
        note = new Note(59, midiVelocity, midiLength);
      break;
      case 8:
        note = new Note(55, midiVelocity, midiLength);
      break;
      case 9:
        note = new Note(50, midiVelocity, midiLength);  
      break;
      case 10:
        note = new Note(47, midiVelocity, midiLength);
      break;
      case 11:
        note = new Note(43, midiVelocity, midiLength);
      break;
      case 12:
        note = new Note(38, midiVelocity, midiLength);
      break;
      case 13:
        note = new Note(35, midiVelocity, midiLength);
      break;
      case 14:
        note = new Note(31, midiVelocity, midiLength);
      break;
      case 15:
        note = new Note(26, midiVelocity, midiLength);
      break;
      default:
        note = new Note(20, midiVelocity, midiLength);
      break;
      
    }  
  }
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -