conway's game of life with music
in
Contributed Library Questions
•
1 year ago
For some reason, every time I try to run this program, I just get a gray window. Anybody know how to make this program work? I'm using a combination of Conway's Game of Life (
http://processing.org/learning/topics/conway.html) and the Ess library (
http://www.tree-axis.com/Ess/). Thanks!
- import krister.Ess.*;
- int sx, sy;
- float density = 0.5;
- int[][][] world; //triple array
- AudioChannel myChannel1;
- SineWave myWave1;
- int frameDuration;
- void setup()
- {
- size(640, 500, P2D);
- frameRate(12); //set a framerate variable--1000/framerate = new variable called frameDuration
- sx = width;
- sy = height;
- world = new int[sx][sy][2];
- frameDuration = 1000/12;
- // Set random cells to 'on'
- for (int i = 0; i < sx * sy * density; i++) {
- world[(int)random(sx)][(int)random(sy)][1] = 1;
- }
- // start up Ess
- Ess.start(this);
- // create a new AudioChannel
- myChannel1=new AudioChannel();
- // set the channel size to 5 seconds
- myChannel1.initChannel(myChannel1.frames(frameDuration));
- }
- void draw()
- {
- background(0);
- // Drawing and update cycle
- for (int x = 0; x < sx; x=x+1) {
- for (int y = 0; y < sy; y=y+1) {
- //if (world[x][y][1] == 1)
- // Change recommended by The.Lucky.Mutt
- if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1))
- {
- world[x][y][0] = 1;
- set(x, y, #FFFFFF);
- }
- if (world[x][y][1] == -1)
- {
- world[x][y][0] = 0;
- }
- world[x][y][1] = 0;
- }
- }
- // Birth and death cycle
- for (int x = 0; x < sx; x=x+1) {
- for (int y = 0; y < sy; y=y+1) {
- int count = neighbors(x, y);
- if (count == 3 && world[x][y][0] == 0) // if the cell is black and has three white neighbors
- {
- world[x][y][1] = 1;
- // audio here
- myWave1=new SineWave(480,.1);
- myWave1.generate(myChannel1,0,myChannel1.frames(frameDuration));
- }
- if ((count < 2 || count > 3) && world[x][y][0] == 1) // if the cell is white and has less than 2 or more than 3 white neighbors
- {
- world[x][y][1] = -1;
- //audio here
- myWave1=new SineWave(680,.1);
- myWave1.generate(myChannel1,0,myChannel1.frames(frameDuration));
- }
- }
- }
- }
- // differentiate between different coordinates on the grid
- // Count the number of adjacent cells 'on'
- int neighbors(int x, int y)
- {
- return world[(x + 1) % sx][y][0] +
- world[x][(y + 1) % sy][0] +
- world[(x + sx - 1) % sx][y][0] +
- world[x][(y + sy - 1) % sy][0] +
- world[(x + 1) % sx][(y + 1) % sy][0] +
- world[(x + sx - 1) % sx][(y + 1) % sy][0] +
- world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] +
- world[(x + 1) % sx][(y + sy - 1) % sy][0];
- }
- public void stop() {
- Ess.stop();
- super.stop();
- }
1