-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygrid.py
377 lines (288 loc) · 11.9 KB
/
pygrid.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""
====================================================================
PYGRID ENGINE 1.1
Copyright (C) <2014> <Ericson Willians.>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
====================================================================
Engine written by Ericson Willians, a brazilian composer and programmer.
CONTACT: ericsonwrp@gmail.com
AS A COMPOSER: https://soundcloud.com/r-p-ericson-willians
YOUTUBE CHANNEL: http://www.youtube.com/user/poisonewein
====================================================================
"""
__author__ = 'EricsonWillians'
import pygame
class Grid():
"""
A Pygrid Grid is a tessellation of n-dimensional Euclidean space by congruent bricks.
"""
def __init__(self, wh, rwh):
"""
The Grid constructor defines its size in width and height by elements.
Every element is a grid rectangle, with a x and y position and fixed size.
Both (wh) and (rwh) arguments are list objects of two indexes.
self refers to each individual grid instance.
The wh argument expects a list with two indexes (One for the width, one for the height).
the rwh argument expects a list with two indexes (One for the fixed width of each rectangle, one for the fixed height of each rectangle)
The keys list has two indexes.
keys[0] is the width of the grid in elements.
keys[1] is the height of the grid in elements.
The x dictionary has keys[0] number of keys.
Each key[0] key in x dictionary has an equivalent int value in pixels.
The same applies to the y dictionary.
Colors dictionary has basic colors as values of capitalized color-name string keys..
"""
self.gridRectWidth = rwh[0]
self.gridRectHeight = rwh[1]
self.gridWidth = wh[0]
self.gridHeight = wh[1]
self.gridWidthInPixels = rwh[0]*wh[0]
self.gridHeightInPixels = rwh[1]*wh[1]
self.keys = [[x for x in range(self.gridWidth)], [x for x in range(self.gridHeight)]]
self.x = dict(zip([x for x in self.keys[0]], [x for x in range(0, self.gridWidthInPixels, self.gridRectWidth)]))
self.y = dict(zip([x for x in self.keys[1]], [x for x in range(0, self.gridHeightInPixels, self.gridRectHeight)]))
self.colors = {"BLACK": (0,0,0), "WHITE": (255,255,255), "RED": (255,0,0), "GREEN": (0,255,0), "BLUE": (0,0,255)}
def getX(self, key):
"""
The getX() method expects a x-key as an argument. It returns its equivalent value in pixels.
"""
return self.x.get(key)
def getY(self, key):
"""
The getY() method expects a y-key as an argument. It returns its equivalent value in pixels.
"""
return self.y.get(key)
class Grect():
"""
A Pygrid Grect is a grid rectangle.
"""
def __init__(self, grid, x, y, color, isBackground):
"""
The Grect constructor defines its position on the specified grid and its color.
The grect x position in pixels is the specified x-key.
The grect y position in pixels is the specified y-key.
The grect width and height are grid-fixed (But they can be altered).
The grect color is the specified color.
The boolean defines if it shall be used as a background for the whole grid.
"""
self.grid = grid
self.x = grid.getX(x)
self.y = grid.getY(y)
self.w = grid.gridRectWidth
self.h = grid.gridRectHeight
self.color = color
self.isBackground = isBackground
def changeColor(self, color):
"""
The changeColor() method expects a new color as an argument (A RGB-tuple with 3 indexes).
"""
self.color = color
def draw(self, surface):
"""
The draw() method draws the grect in its current x and y positions.
If the grect position is offset within the grid limits, it raises a TypeError.
If it is a background, then it draws the grect of the size of the whole grid.
"""
if self.isBackground == False:
try:
pygame.draw.rect(surface, self.color, (self.x, self.y, self.w, self.h))
except:
pass
elif self.isBackground == True:
try:
pygame.draw.rect(surface, self.color, (0, 0, self.grid.gridWidthInPixels, self.grid.gridHeightInPixels))
except:
pass
def getGrid(self):
"""
Returns the associated grid instance.
"""
return self.grid
def getColor(self):
"""
Returns the grect RGB color (Tuple).
"""
return self.color
def isBackground(self):
"""
Returns the isBackground boolean.
"""
return self.isBackground
class GrectArray():
"""
A Pygrid GrectArray is a sequence of grid rectangles.
"""
def __init__(self, grid, isBackground):
"""
The constructor defines to the GrectArray instance its own list and a boolean indicating if it is used as a background.
"""
self.grid = grid
self.array = []
self.isBackground = isBackground
def add(self, grect):
"""
The add() method adds to the GrectArray instance a specified grect.
"""
self.array.append(grect)
def remove(self, grect):
"""
The remove() method removes from the GrectArray the specified grect instance (If it exists).
"""
if len(self.array) > 0:
for i in self.array:
if i == grect:
self.array.remove(grect)
def addBackground(self, color):
"""
The addBackground() method adds to the GrectArray a full-filling width and height sequence of grects in the specified color.
Not recommended with large grids (Slow performance).
"""
for i in range(self.grid.gridHeight):
for j in range(self.grid.gridWidth):
self.array.append(list())
self.array[i].append(Grect(self.grid, j, i, color, False))
def draw(self, surface):
"""
The draw() method draws the sequence of grects.
It loops through the sequence and draws each grect in their own x and y positions.
If the position of a grect is offset within the grid limits, it raises a TypeError.
If the background boolean is false, it draws it as just a simple sequence.
If the background boolean is true, it draws it as a full-filling background.
"""
if self.isBackground == False:
try:
if len(self.array) > 0:
for i in self.array:
pygame.draw.rect(surface, i.color, (i.x, i.y, self.grid.gridRectWidth, self.grid.gridRectHeight))
except:
pass
elif self.isBackground == True:
try:
if len(self.array) > 0:
for i in self.array:
for j in i:
pygame.draw.rect(surface, j.color, (j.x, j.y, self.grid.gridRectWidth, self.grid.gridRectHeight))
except:
pass
def getGrid(self):
"""
Returns the associated grid instance.
"""
return self.grid
def isBackground(self):
"""
Returns the isBackground boolean.
"""
return self.isBackground
class Controller():
"""
A Pygrid controller alters the positions of grects.
"""
def __init__(self, grid, isArray, isWarper):
"""
The constructor defines four basic directions, and a boolean indicating if the controlled target is a sequence or not.
The isWarper boolean indicates if the Controller shall allow screen-warping.
"""
self.grid = grid
self.UP = 0
self.DOWN = 1
self.LEFT = 2
self.RIGHT = 3
self.isArray = isArray
self.isWarper = isWarper
def control(self, target, direction, step):
"""
The control() method moves the specified grect or grect sequence target in a specified direction, by specified step and in the controller's grid.
"""
if self.isArray == False:
if direction == 0:
try:
target.y -= self.grid.getY(step)
except:
pass
if self.isWarper == True:
if target.y < self.grid.getY(0):
target.y = self.grid.getY(self.grid.gridHeight-1)
elif direction == 1:
try:
target.y += self.grid.getY(step)
except:
pass
if self.isWarper == True:
if target.y > self.grid.getY(self.grid.gridHeight-1):
target.y = self.grid.getY(0)
elif direction == 2:
try:
target.x -= self.grid.getX(step)
except:
pass
if self.isWarper == True:
if target.x < self.grid.getX(0):
target.x = self.grid.getX(self.grid.gridWidth-1)
elif direction == 3:
try:
target.x += self.grid.getX(step)
except:
pass
if self.isWarper == True:
if target.x > self.grid.getX(self.grid.gridWidth-1):
target.x = self.grid.getX(0)
elif self.isArray == True:
if len(target.array) > 0:
for i in target.array:
if direction == 0:
try:
i.y -= self.grid.getY(step)
except:
pass
if self.isWarper == True:
if i.y < self.grid.getY(0):
i.y = self.grid.getY(self.grid.gridHeight-1)
elif direction == 1:
try:
i.y += self.grid.getY(step)
except:
pass
if self.isWarper == True:
if i.y > self.grid.getY(self.grid.gridHeight-1):
i.y = self.grid.getY(0)
elif direction == 2:
try:
i.x -= self.grid.getX(step)
except:
pass
if self.isWarper == True:
if i.x < self.grid.getX(0):
i.x = self.grid.getX(self.grid.gridWidth-1)
elif direction == 3:
try:
i.x += self.grid.getX(step)
except:
pass
if self.isWarper == True:
if i.x > self.grid.getX(self.grid.gridWidth-1):
i.x = self.grid.getX(0)
def getGrid(self):
"""
Returns the associated grid instance.
"""
return self.grid
def isArray(self):
"""
Returns the isArray boolean.
"""
return self.isArray
def isWarper(self):
"""
Returns the isWarper boolean.
"""
return self.isWarper