-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateAnimation.py
More file actions
executable file
·81 lines (64 loc) · 2.25 KB
/
createAnimation.py
File metadata and controls
executable file
·81 lines (64 loc) · 2.25 KB
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
import numpy as np
import copy
from exportGIF import to_gif
maze = None
sequence = []
current_pos = None
np.random.seed(0)
# maze visualization constants
b = 'b'
w = 'w'
t = 't'
p = 'p'
e = 'e'
def add_frame():
'''Adds image of current maze state to sequence of frames, to be exported to GIF later'''
global sequence
temp = copy.copy(maze)
temp[current_pos] = p
sequence.append(temp)
def move(direction):
'''Moves current player position in given direction, adds new frame to sequence.
Valid directions are "u" (up), "d" (down), "l" (left), and "r" (right)'''
global current_pos
if np.array_equal(maze[current_pos], t) or np.array_equal(maze[current_pos], e):
maze[current_pos] = b
if direction.lower() == 'u':
if current_pos[0] == 0 or np.array_equal(maze[current_pos[0] - 1][current_pos[1]], w):
print('Invalid move UP from position ({},{})'.format(current_pos[0], current_pos[1]))
else:
current_pos = (current_pos[0] - 1, current_pos[1])
add_frame()
elif direction.lower() == 'd':
if current_pos[0] == len(maze) - 1 or np.array_equal(maze[current_pos[0] + 1][current_pos[1]], w):
print('Invalid move DOWN from position ({},{})'.format(current_pos[0], current_pos[1]))
else:
current_pos = (current_pos[0] + 1, current_pos[1])
add_frame()
elif direction.lower() == 'l':
if current_pos[1] == 0 or np.array_equal(maze[current_pos[0]][current_pos[1] - 1], w):
print('Invalid move LEFT from position ({},{})'.format(current_pos[0], current_pos[1]))
else:
current_pos = (current_pos[0], current_pos[1] - 1)
add_frame()
elif direction.lower() == 'r':
if current_pos[1] == len(maze[0]) - 1 or np.array_equal(maze[current_pos[0]][current_pos[1] + 1], w):
print('Invalid move RIGHT from position ({},{})'.format(current_pos[0], current_pos[1]))
else:
current_pos = (current_pos[0], current_pos[1] + 1)
add_frame()
else:
print('Unknown move command "{}"'.format(direction))
def createAnimation(input_maze, start_pos, player_moves):
global sequence
global maze
global current_pos
maze = input_maze
current_pos = start_pos
add_frame()
for m in player_moves:
move(m)
add_frame()
add_frame()
add_frame()
to_gif(sequence, 'animation.gif')