Skip to content

Commit 98cf040

Browse files
committed
Numpy for animation
1 parent 000d6f7 commit 98cf040

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

2019/day13.py

+27-24
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ def __init__(self, day, part):
1515

1616

1717
def paint(self):
18-
self.canvas = {}
18+
self.canvas = np.zeros((43,23,12000))
19+
self.scores = np.zeros((12000))
1920
self.ball_x = 0
2021
self.paddle_x = 0
21-
22+
inc = 0
2223
while True:
2324
x = self.execute_opcode(reset_pointer=False)
2425
y = self.execute_opcode(reset_pointer=False)
@@ -27,26 +28,38 @@ def paint(self):
2728
if z == 4:
2829
self.ball_x = x
2930
self.input(-1 if self.ball_x < self.paddle_x else 1 if self.ball_x > self.paddle_x else 0)
31+
self.canvas[x, y, inc] = z
32+
inc += 1
3033
if z == 3:
3134
self.paddle_x = x
35+
inc += 1
36+
3237

3338
if isinstance(z, Day):
39+
print(inc)
3440
break
3541
if x == -1 and y == 0:
3642
#print("Score: ", z)
3743
self.score = z
38-
self.visualize()
44+
self.scores[inc:] = z
45+
#self.visualize()
3946
else:
40-
self.canvas[(x, y)] = z
41-
if len(self.canvas.keys()) > 910:
42-
self.visualize()
47+
if z in [1,2]:
48+
self.canvas[x, y, :] = z
49+
elif z == 0:
50+
self.canvas[x, y, inc:] = z
51+
elif z == 3:
52+
self.canvas[x, y, inc] = z
53+
54+
#self.visualize()
55+
4356
return self
4457
def visualize(self):
4558

4659
print("Score: ", self.score)
4760
#x, y = zip(*self.canvas.keys())
48-
#painting = {0: " ", 1: "░", 2: "▒", 3: "▉", 4: "◓"}
49-
#for j in range(min(y)-2, max(y)+1):
61+
painting = {0: " ", 1: "░", 2: "▒", 3: "▉", 4: "◓"}
62+
# for j in range(min(y)-2, max(y)+1):
5063
# for i in range(min(x), max(x)+1):
5164
# for j in range(0, 25):
5265
# for i in range(0, 45):
@@ -87,27 +100,17 @@ def visualize(self):
87100
# 3 is a horizontal paddle tile. The paddle is indestructible.
88101
# 4 is a ball tile. The ball moves diagonally and bounces off objects.
89102

90-
fig = plt.figure()
103+
fig = plt.figure(figsize=(9,5))
91104

92105

93-
def f(x, y):
94-
return np.sin(x) + np.cos(y)
95-
96-
x = np.linspace(0, 2 * np.pi, 120)
97-
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
98-
# ims is a list of lists, each row is a list of artists to draw in the
99-
# current frame; here we are just animating one artist, the image, in
100-
# each frame
101106
ims = []
102-
for i in range(60):
103-
x += np.pi / 15.
104-
y += np.pi / 20.
105-
im = plt.imshow(f(x, y), animated=True)
107+
for i in range(0,7000,10):
108+
im = plt.imshow(np.rot90(part2.canvas[:,:,i], k=-1), animated=True, cmap=plt.cm.get_cmap('Greens', 6))
106109
ims.append([im])
107-
108-
ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
110+
plt.axis('off')
111+
ani = animation.ArtistAnimation(fig, ims, interval=50, blit=False,
109112
repeat_delay=1000)
110113

111-
# ani.save('dynamic_images.mp4')
114+
ani.save('dynamic_images.mp4')
112115

113116
plt.show()

2019/dynamic_images.mp4

210 KB
Binary file not shown.

0 commit comments

Comments
 (0)