/
catboard.py
273 lines (259 loc) · 14.6 KB
/
catboard.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from collections import Counter
import pygame, sys
from pygame.locals import *
from pygame import gfxdraw
import numpy as np
import random
class catboard(object):
def __init__(self, surf, default_catcolor, num_cats, bgcolor, textcolor, fpsclock, offset_frac,
font):
self.DISPLAYSURF = surf
self.WIDTH = self.DISPLAYSURF.get_width()
self.TOP_OFFSET = int(self.DISPLAYSURF.get_height()*offset_frac)
self.HEIGHT = self.DISPLAYSURF.get_height()
self.BGCOLOR = bgcolor
self.TEXTCOLOR = textcolor
self.FPSCLOCK = fpsclock
self.DEFAULT_CATCOLOR = default_catcolor
self.NCATS = num_cats
self.CAT_SIZE = int((self.HEIGHT - self.TOP_OFFSET) / ((11.*num_cats + 1)/10.))
self.MARGIN = int(float(self.CAT_SIZE)/10.)
self.CAT_STATES = self.initialize_cat_states()
self.COLOR_FIT = {self.DEFAULT_CATCOLOR:1.}
self.NUM_GEN = 0
self.FONT = font
def initialize_cat_states(self):
# Start off by putting a column of cats at the right side of the screen
result = [[]]
for cat_i in range(1, self.NCATS + 1):
cat_topx = int(self.WIDTH - self.MARGIN - self.CAT_SIZE)
cat_topy = int(cat_i*self.MARGIN + (cat_i - 1)*self.CAT_SIZE + self.TOP_OFFSET)
result[0].append([cat_topx, cat_topy, self.DEFAULT_CATCOLOR])
return result
def draw_current(self, cat_position):
for cat_col in self.CAT_STATES:
for cat in cat_col:
drawCat(cat[0], cat[1], self.CAT_SIZE, cat_position,
self.DISPLAYSURF, cat[2])
def wiggle_cats(self, fps, iterations=1):
for pos in ["out", "middle", "in", "middle"]*iterations:
self.DISPLAYSURF.fill(self.BGCOLOR)
self.draw_current(pos)
draw_num_gen(self.DISPLAYSURF, self.NUM_GEN, self.TOP_OFFSET,
self.FONT, self.TEXTCOLOR, self.BGCOLOR)
pygame.display.update()
self.FPSCLOCK.tick(fps)
def backshift(self):
for idx, cat_col in enumerate(self.CAT_STATES):
new_cat_col_x = cat_col[0][0] - self.MARGIN - self.CAT_SIZE
if new_cat_col_x < 0:
del self.CAT_STATES[idx]
else:
for cat in cat_col:
# Shift x position of cat backwards
cat[0] = new_cat_col_x
def mutate_cats(self, frequency, new_color):
num_mutate_cats = int(frequency*self.NCATS)
current_color = self.CAT_STATES[-1][0][2]
colors = sorted([current_color, new_color])
color_order = sorted([current_color, new_color]).index(new_color)
for idx, cat in enumerate(self.CAT_STATES[-1]):
if color_order == 1:
if idx > self.NCATS - num_mutate_cats - 1:
cat[2] = colors[1]
else:
cat[2] = colors[0]
else:
if idx < num_mutate_cats:
cat[2] = colors[0]
else:
cat[2] = colors[1]
def is_poly(self):
assert len(self.CAT_STATES) > 0, "ran out of cats!!!"
current_cats = self.CAT_STATES[-1]
if len(Counter([cat[2] for cat in current_cats]).keys()) > 1:
return True
else:
return False
def add_new_gen_fancy(self):
assert len(self.CAT_STATES) > 0, "ran out of cats!!!"
current_cats = self.CAT_STATES[-1]
cat_colors = Counter([cat[2] for cat in current_cats])
cat_color_prs = sorted([(cat_color,
float(cat_colors[cat_color]*
self.COLOR_FIT[cat_color]/(self.NCATS -
cat_colors[cat_color]
+ cat_colors[cat_color]*
self.COLOR_FIT[cat_color])))
for cat_color in cat_colors.keys()])
new_color_counts = np.random.multinomial(self.NCATS,
[cat_color_pr[1] for
cat_color_pr in cat_color_prs])
# Assign a number of offspring to each cat
color_indv_offspring = {}
for idx, cat_color in enumerate(cat_color_prs):
if new_color_counts[idx] > 0:
color_indv_offspring[cat_color[0]] = np.random.multinomial(new_color_counts[idx],
[1./cat_colors[cat_color[0]]]*
cat_colors[cat_color[0]])
else:
color_indv_offspring[cat_color[0]] = [0]*cat_colors[cat_color[0]]
new_gen = []
new_cat_i = 1
current_cat_i = {}
cat_topx = int(self.WIDTH - self.MARGIN - self.CAT_SIZE)
for cat_color in cat_colors.keys():
current_cat_i[cat_color] = 0
cat_lineage = []
for cat in current_cats:
cat_babies = []
current_cat_color = cat[2]
num_offspring = color_indv_offspring[current_cat_color][current_cat_i[current_cat_color]]
for baby_i in range(num_offspring):
cat_topy = int(new_cat_i*self.MARGIN + (new_cat_i - 1)*self.CAT_SIZE + self.TOP_OFFSET)
new_gen.append([cat_topx, cat_topy, current_cat_color])
cat_babies.append([cat_topx, cat_topy, current_cat_color])
new_cat_i += 1
current_cat_i[current_cat_color] += 1
cat_lineage.append(cat_babies)
self.animate_next_gen(cat_lineage, 10, 10)
self.CAT_STATES.append(new_gen)
self.NUM_GEN += 1
return cat_lineage
def animate_next_gen(self, cat_lineage, steps, fps):
for ii in range(1, steps + 1):
self.DISPLAYSURF.fill(self.BGCOLOR)
draw_num_gen(self.DISPLAYSURF, self.NUM_GEN, self.TOP_OFFSET,
self.FONT, self.TEXTCOLOR, self.BGCOLOR)
self.draw_current("middle")
current_cats = self.CAT_STATES[-1]
for idx, cat in enumerate(current_cats):
old_x = cat[0]
old_y = cat[1]
for baby_idx, baby_cat in enumerate(cat_lineage[idx]):
new_x = baby_cat[0]
new_y = baby_cat[1]
x = int(old_x + (float(ii)/steps)*(new_x - old_x))
y = int(old_y + (float(ii)/steps)*(new_y - old_y))
drawCat(x, y, self.CAT_SIZE, "middle", self.DISPLAYSURF, baby_cat[2])
pygame.display.update()
self.FPSCLOCK.tick(fps)
def setup_text(font, text, color, bgcolor, top, left):
textSurf = font.render(text, True, color)
textRect = textSurf.get_rect()
textRect.x = top
textRect.y = left
return textSurf, textRect
def draw_num_gen(surf, num_gen, bar_height, font, TEXTCOLOR, BGCOLOR):
textSurf, textRect = setup_text(font,
"# of generations: " + str(num_gen), TEXTCOLOR, BGCOLOR, 0, 0)
surf.blit(textSurf, textRect)
def drawCat(topx, topy, width, position, surf, cat_color):
BLACK = (0, 0, 0)
if position == 'middle':
## front
gfxdraw.aatrigon(surf, int(topx + 3*width/32), int(topy + width/2),
int(topx + 5*width/32), int(topy + width/2),
int(topx + 4*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.filled_trigon(surf, int(topx + 3*width/32), int(topy + width/2),
int(topx + 5*width/32), int(topy + width/2),
int(topx + 4*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.aatrigon(surf, int(topx + 5*width/32), int(topy + width/2),
int(topx + 7*width/32), int(topy + width/2),
int(topx + 6*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.filled_trigon(surf, int(topx + 5*width/32), int(topy + width/2),
int(topx + 7*width/32), int(topy + width/2),
int(topx + 6*width/32), int(topy + 7*width/8), cat_color)
## back
gfxdraw.aatrigon(surf, int(topx + 25*width/32), int(topy + width/2),
int(topx + 23*width/32), int(topy + width/2),
int(topx + 24*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.filled_trigon(surf, int(topx + 25*width/32), int(topy + width/2),
int(topx + 23*width/32), int(topy + width/2),
int(topx + 24*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.aatrigon(surf, int(topx + 23*width/32), int(topy + width/2),
int(topx + 21*width/32), int(topy + width/2),
int(topx + 22*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.filled_trigon(surf, int(topx + 23*width/32), int(topy + width/2),
int(topx + 21*width/32), int(topy + width/2),
int(topx + 22*width/32), int(topy + 7*width/8), cat_color)
elif position == 'out':
## front
gfxdraw.aatrigon(surf, int(topx + 3*width/32), int(topy + width/2),
int(topx + 5*width/32), int(topy + 7*width/16),
int(topx + 1*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.filled_trigon(surf, int(topx + 3*width/32), int(topy + width/2),
int(topx + 5*width/32), int(topy + 7*width/16),
int(topx + 1*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.aatrigon(surf, int(topx + 5*width/32), int(topy + width/2),
int(topx + 7*width/32), int(topy + 7*width/16),
int(topx + 3*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.filled_trigon(surf, int(topx + 5*width/32), int(topy + width/2),
int(topx + 7*width/32), int(topy + 7*width/16),
int(topx + 3*width/32), int(topy + 7*width/8), cat_color)
## back
gfxdraw.aatrigon(surf, int(topx + 25*width/32), int(topy + width/2),
int(topx + 23*width/32), int(topy + 7*width/16),
int(topx + 27*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.filled_trigon(surf, int(topx + 25*width/32), int(topy + width/2),
int(topx + 23*width/32), int(topy + 7*width/16),
int(topx + 27*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.aatrigon(surf, int(topx + 23*width/32), int(topy + width/2),
int(topx + 21*width/32), int(topy + 7*width/16),
int(topx + 25*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.filled_trigon(surf, int(topx + 23*width/32), int(topy + width/2),
int(topx + 21*width/32), int(topy + 7*width/16),
int(topx + 25*width/32), int(topy + 7*width/8), cat_color)
elif position == "in":
## front
gfxdraw.aatrigon(surf, int(topx + 3*width/32), int(topy + width/2),
int(topx + 5*width/32), int(topy + 7*width/16),
int(topx + 7*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.filled_trigon(surf, int(topx + 3*width/32), int(topy + width/2),
int(topx + 5*width/32), int(topy + 7*width/16),
int(topx + 7*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.aatrigon(surf, int(topx + 5*width/32), int(topy + width/2),
int(topx + 7*width/32), int(topy + 7*width/16),
int(topx + 9*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.filled_trigon(surf, int(topx + 5*width/32), int(topy + width/2),
int(topx + 7*width/32), int(topy + 7*width/16),
int(topx + 9*width/32), int(topy + 7*width/8), cat_color)
## back
gfxdraw.aatrigon(surf, int(topx + 25*width/32), int(topy + 7*width/16),
int(topx + 23*width/32), int(topy + width/2),
int(topx + 21*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.filled_trigon(surf, int(topx + 25*width/32), int(topy + 7*width/16),
int(topx + 23*width/32), int(topy + width/2),
int(topx + 21*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.aatrigon(surf, int(topx + 23*width/32), int(topy + 7*width/16),
int(topx + 21*width/32), int(topy + width/2),
int(topx + 19*width/32), int(topy + 7*width/8), cat_color)
gfxdraw.filled_trigon(surf, int(topx + 23*width/32), int(topy + 7*width/16),
int(topx + 21*width/32), int(topy + width/2),
int(topx + 19*width/32), int(topy + 7*width/8), cat_color)
# draw the body of the cat
pygame.draw.ellipse(surf, cat_color,
(int(topx + width/16), int(topy + width/4),
int(3*width/4), int(width/2)))
# pygame.draw.ellipse(surf, BLACK,
# (int(topx + width/16), int(topy + width/4),
# int(3*width/4), int(width/2)), 5)
# draw the ears of the cat
pygame.draw.aalines(surf, cat_color, False, ((int(topx + 7*width/12), int(topy + width/4)),
(int(topx + 8*width/12), int(topy)),
(int(topx + 9*width/12), int(topy + width/4))))
pygame.draw.aalines(surf, cat_color, False, ((int(topx + 9*width/12), int(topy + width/4)),
(int(topx + 10*width/12), int(topy)),
(int(topx + 11*width/12), int(topy + width/4))))
# draw the head of the cat
gfxdraw.filled_circle(surf, int(topx + 3*width/4), int(topy + width/4), int(width/6),
cat_color)
gfxdraw.aacircle(surf,
int(topx + 3*width/4), int(topy + width/4), int(width/6), cat_color)
# draw the eyes of the cat
pygame.draw.ellipse(surf, BLACK,
(int(topx + 5.2*width/8), int(topy + width/8),
int(width/10), int(width/12)))
pygame.draw.ellipse(surf, BLACK,
(int(topx + 6.2*width/8), int(topy + width/8),
int(width/10), int(width/12)))