-
Notifications
You must be signed in to change notification settings - Fork 0
/
conwaysa.py
165 lines (141 loc) · 4.58 KB
/
conwaysa.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
import os
import pygame
import random
from pygame.locals import *
import os.path, sys
import pygame.mixer, pygame.time
mixer = pygame.mixer
time = pygame.time
"""globals"""
playfield_size = [78,38]
playfield = [[False for x in range(playfield_size[0])] for y in range(playfield_size[1])]
playfield_samples = [["" for x in range(playfield_size[0])] for y in range(playfield_size[1])]
playfield_channels = [[None for x in range(playfield_size[0])] for y in range(playfield_size[1])]
def init_playfield(samples):
global playfield,playfield_samples,playfield_size
for i in range(390):
x = random.randint(0,playfield_size[0]-1)
y = random.randint(0,playfield_size[1]-1)
playfield[y][x] = True
# for y in range(playfield_size[1]):
# for x in range(playfield_size[0]):
#if(random.choice([True,False,False,False])):
# if(random.randint(0,20) == 0):
# sample = samples[random.randint(0,len(samples)-1)]
# sound = mixer.Sound(sample)
# playfield_samples[y][x] = sound
for r in range(1):
for i in range(len(samples)):
x = random.randint(0,playfield_size[0]-1)
y = random.randint(0,playfield_size[1]-1)
sample = samples[i]
sound = mixer.Sound(sample)
playfield_samples[y][x] = sound
def print_playfield():
global playfield,playfield_size
print("+-" + "--"*playfield_size[0] + "+")
for y in range(playfield_size[1]):
print("|", end=" ")
for x in range(playfield_size[0]):
if(playfield[y][x] == True):
print("o", end=" ")
else:
print(".", end=" ")
print("|")
print("+-" + "--"*playfield_size[0] + "+")
# print(playfield_samples)
def mutate_playfield():
global playfield,playfield_samples,playfield_size
for i in range(20):
x = random.randint(0,playfield_size[0]-1)
y = random.randint(0,playfield_size[1]-1)
playfield[y][x] = True
def get_neighbours_num(x,y):
global playfield,playfield_size
n = 0
if(y-1 >= 0 and playfield[y-1][x] == True):
n += 1
if(x-1 >= 0 and playfield[y][x-1] == True):
n += 1
if(x+1 < playfield_size[0] and playfield[y][x+1] == True):
n += 1
if(y+1 < playfield_size[1] and playfield[y+1][x] == True):
n += 1
if(y-1 >= 0 and x-1 > 0 and playfield[y-1][x-1] == True):
n += 1
if(y-1 >= 0 and x+1 < playfield_size[0] and playfield[y-1][x+1] == True):
n += 1
if(y+1 < playfield_size[1] and x+1 < playfield_size[0] and playfield[y+1][x+1] == True):
n += 1
if(y+1 < playfield_size[1] and x-1 >= 0 and playfield[y+1][x-1] == True):
n += 1
return n
def play_sounds():
global playfield,playfield_samples,playfield_size
for y in range(playfield_size[1]):
for x in range(playfield_size[0]):
if(playfield[y][x] == True and playfield_samples[y][x] != ""):
sample = playfield_samples[y][x];
#if(
if(playfield_channels[y][x] != None):
sample.stop()
playfield_channels[y][x] = sample.play()
#sample.set_volume(random.randint(0,255))
sample.set_volume(get_neighbours_num(x,y)*20)
"""compute next step"""
def tick():
global playfield,playfield_size
new_playfield = [[False for x in range(playfield_size[0])] for y in range(playfield_size[1])]
for y in range(playfield_size[1]):
for x in range(playfield_size[0]):
neighbours = get_neighbours_num(x,y)
if(playfield[y][x] == True):
if(neighbours < 2): #underpopulation
new_playfield[y][x] = False
if(neighbours == 2 or neighbours == 3): #great neighbourhood ,stay
new_playfield[y][x] = True
if(neighbours > 3): #overcrowded
new_playfield[y][x] = False
else:
if(neighbours == 3): #reproduce
new_playfield[y][x] = True
playfield = new_playfield
def get_samples(dir):
files = os.listdir(dir)
for index,f in enumerate(files):
f = dir + "/" + f
files[index] = f
return files
def setup():
random.seed()
mixer.init()
#screen = pygame.display.set_mode ((640, 480), 0, 32)
samples = get_samples("./samples")
init_playfield(samples)
tick()
print_playfield()
pygame.init ()
#screen.fill ((100, 100, 100))
#pygame.display.flip ()
#pygame.key.set_repeat (500, 30)
#mixer.init(11025)
#mixer.init(44100)
#sample = samples[random.randint(0,len(samples)-1)]
#print("playing sample:",sample)
#sound = mixer.Sound(sample)
#channel = sound.play()
while True:
play_sounds()
#for i in range(2):
mutate_playfield()
tick()
print_playfield()
#time.wait(int((1000*60)/80)) # 128bpm
time.wait(50)
#while channel.get_busy(): #still playing
# print(" ...still going...")
# time.wait(1000)
#print("...Finished")
pygame.quit()
setup()