-
Notifications
You must be signed in to change notification settings - Fork 0
/
LifeGame.pde
149 lines (137 loc) · 3.67 KB
/
LifeGame.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
int sx, sy;
float density = 0.5;
int[][][] world;
int[][] generation;
float dotsize = 15;
boolean pose = false;
color [] backcolors = new color[2];
int dx=5;
int dy=10;
int baseline = 0;
int blur,turn;
float turn_MAX = 15;
void draw()
{
background(0,180,220,300);
update();
}
void setup()
{
size(750, 540);
frameRate(30);
sx = (int)(width/dotsize)+2;
sy = (int)(height/dotsize)+1;
world = new int[sx][sy][2];
generation = new int[sx][sy];
smooth();
noStroke();
fill(51);
kill();
// Set random cells to 'on'
for (int i = 0; i < sx * sy * density; i++) {
world[(int)random(sx)][(int)random(sy)][1] = 1;
}
}
// Drawing and update cycle
void update(){
for (int x = 0; x < sx; x=x+1) {
for (int y = 0; y < sy; y=y+1) {
// update
if ((world[x][y][1] == 1) && (world[x][y][0] == 0)){
fill(250,250,0,generation[x][y]);
ellipse(dotsize*x+dx, dotsize*y+dy, (dotsize-1)*(turn/turn_MAX), (dotsize-1)*(turn/turn_MAX) );
}else if((world[x][y][1] == -1) && ( world[x][y][0] == 1)){
blur = (int)random(-2,2);
fill(255,255,0,generation[x][y]);
ellipse(dotsize*x+dx, dotsize*y+dy, dotsize-1 + blur, dotsize-1 + blur);
fill(0,200,240,generation[x][y]);
ellipse(dotsize*x+dx, dotsize*y+dy, (dotsize-1)*(turn/turn_MAX), (dotsize-1)*(turn/turn_MAX) );
}else if ((world[x][y][1] == 1 || world[x][y][0] == 1)) {
blur = (int)random(-3,4);
fill(250,250,0,generation[x][y]);
ellipse(dotsize*x+dx, dotsize*y+dy, dotsize-1 + blur, dotsize-1 + blur);
}
}
}
if(!pose){
for (int x = 0; x < sx; x=x+1) {
for (int y = 0; y < sy; y=y+1) {
// update
// Change recommended by The.Lucky.Mutt
if ((world[x][y][1] == 1) && (world[x][y][0] == 0)){
if(turn >= turn_MAX){ world[x][y][0] = 1; }
}else if((world[x][y][1] == -1) && ( world[x][y][0] == 1)){
if(turn >= turn_MAX){world[x][y][0] = 0;}
}else if ((world[x][y][1] == 1 || world[x][y][0] == 1)) {
if(turn >= turn_MAX){ world[x][y][0] = 1; }
}
if(turn >= turn_MAX){world[x][y][1] = 0; }
}
}
if(turn >= turn_MAX ){
BD();
turn = 0;
}else{
turn++;
}
}
}
// Count the number of adjacent live cells
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];
}
// Kill all lives
void kill(){
for(int i=0; i<sx; i++){
for(int j=0; j<sy; j++){
generation[i][j]=50;
world[i][j][0]=0;
world[i][j][1]=0;
}
}
}
// Birth and death cycle
void BD(){
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){
world[x][y][1] = 1;
generation[x][y]+=10;
}
if ((count < 2 || count > 3) && world[x][y][0] == 1){
world[x][y][1] = -1;
}
}
}
}
// User's action
void godHand(){
int x = (int)(mouseX/dotsize);
int y = (int)(mouseY/dotsize);
if(0<=x && x<sx && 0<=y && y<sy){
world[x][y][1]=1;
}
}
void mouseClicked(){
godHand();
}
void mouseDragged(){
godHand();
}
void keyPressed(){
switch(key) {
case 'p': pose = !pose;
break;
case 'k': kill();
break;
}
}