-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuffergame.py
153 lines (113 loc) · 3.52 KB
/
buffergame.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
"""
buffergame.py - the base class for all the games
author : Benjamin Blundell
email : [email protected]
"""
def loadFromFile(boardX, boardY, filename):
''' function that loads buffers from a file using our format.
file format is:
time <seconds>
<bufferdata>
...
return data is {
"buffers" : [
"time" : <seconds> OR "key" : <key>
"buffer": <data>
]
"duration" : <seconds>
}
'''
buffers = []
timeLength = 0
def _newBuffer():
for i in range(0,boardY):
row = []
for j in range(0,boardX):
row.append( 0 )
new_frame["buffer"].append(row)
buffers.append( new_frame )
with open(filename) as f:
ridx = boardY - 1
try:
for line in f.readlines():
if "time" in line:
# Create a new frame
new_frame = { "buffer" :[], "time" : 0 }
new_frame["time"] = float(line.split(" ")[1])
timeLength += new_frame["time"]
_newBuffer()
ridx = boardY - 1
elif "key" in line:
# create a new frame with a special key and a default time
new_frame = { "buffer" :[], "key" : "a" }
new_frame["key"] = line.split(" ")[1].strip()
_newBuffer()
ridx = boardY - 1
else:
if ridx >= 0:
cidx = 0
for char in line:
if char != '\n':
buffers[len(buffers)-1]["buffer"][ridx][cidx] = char
cidx+=1
ridx-=1
except:
print("Failed to read frame format.")
raise
return { "buffers" : buffers, "duration" : timeLength }
class BufferGame(object):
''' Base class that creates and sets buffers '''
def __init__(self, boardX = 14, boardY = 13):
self.boardX = boardX
self.boardY = boardY
self.buffer = []
for i in range(0,self.boardY):
row = []
for j in range(0,self.boardX):
row.append( 0 )
self.buffer.append(row)
def frame(self,dt):
pass
def clearBuffer(self):
for i in range(0,self.boardY):
for j in range(0,self.boardX):
self.buffer[i][j] = 0
# A lot of the functions below are similar - maybe refactor?
def copyBuffer(self, bbuffer):
''' Copy a frame into the actual buffer '''
for i in range(0,self.boardY):
for j in range(0,self.boardX):
self.buffer[i][j] = bbuffer[i][j]
def copyLinearBuffer(self, bbuffer):
''' Copy a linear buffer into the actual buffer '''
idx = 0
for i in range(0,self.boardY):
for j in range(0,self.boardX):
self.buffer[i][j] = bbuffer[idx]
idx += 1
def copyLinearBufferReversed(self, bbuffer):
''' Copy a linear buffer into the actual buffer reversed columns '''
idx = 0
for i in reversed(range(0,self.boardY)):
for j in range(0,self.boardX):
self.buffer[i][j] = bbuffer[idx]
idx += 1
def getLinearBuffer(self):
data = []
for i in range(0,self.boardY):
for j in range(0,self.boardX):
data.append(self.buffer[i][j])
return data
def getLinearBufferReversed(self):
data = []
for i in reversed(range(0,self.boardY)):
for j in range(0,self.boardX):
data.append(self.buffer[i][j])
return data
def prettyPrint(self):
''' rprint the buffer out nicely '''
for i in reversed(range(0,self.boardY)):
for j in range(0,self.boardX):
print(str(self.buffer[i][j])),
print("")
print("")