-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgridModule.py
More file actions
228 lines (183 loc) · 9.98 KB
/
gridModule.py
File metadata and controls
228 lines (183 loc) · 9.98 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
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
# GRID MODULE BY TIM
#This module contains 5 classes (grid, menu, pixelArt, pixel,colorPallet)
#the grid class is the abstract/parent class, the pixelArt and menu class inherit from it.(they are childs)
#The color pallet class inherits from pixel art class as they both use similar methods.
#The pixel art and colorPallet class creates pixel objects to
#populate the grid, therfore they are dependant of pixel.
#------------------------------------------------------
#Class Descriptions are given above each class.
import pygame
pygame.init()
#Main abstract class (parent)
#This class is capable of creating a grid containing different rows and different columns, bases upon those arguments it
#will automatically alter the pixel size. To display the grid simply call ____.drawGrid(). To find the item in the grid
#that was clicked on call ____.clicked().
class grid(object):
def __init__(self, win, width, height, cols, rows, showGrid=False, startx = 0, starty = 0, bg=(255,255,255)):
self.width = width
self.height = height
self.cols = cols
self.rows = rows
self.bg = bg
self.startx = startx
self.starty = starty
self.lineThick = 1
self.showGrid = showGrid #If we should show the black outline
self.isSelected = None
self.grid = None
self.screen = win
pygame.display.update()
def getGrid(self):
return self.grid #Return the grid list
def drawGrid(self, lineColor=(0,0,0)): #This will draw the lines to create the grid, this is done so by simply creating overlapping boxes
x = self.startx
y = self.starty
for i in range(self.cols):
y = self.starty + self.height
if i > 0:
x += (self.width / self.cols)
for j in range(self.rows):
y -= self.height / self.rows
pygame.draw.rect(self.screen, (0,0,0),(x, y, self.width / self.cols, self.height/ self.rows), 1)
def clicked(self, pos): #Return the position in the grid that user clicked on
try:
t = pos[0]
w = pos[1]
g1 = int((t - self.startx) / self.grid[0][0].w)
g2 = int((w - self.starty) / self.grid[0][0].h)
self.selected = self.grid[g1][g2]
return self.grid[g1][g2]
except IndexError: #If we run into an index error that means that the user did not click on a position in the grid
return False
def isSelected(self): #Return the currently selected object
return self.selected
#This is the concrete class used to draw pixels in a grid
#The draw grid function in this class uses polymorphism to create a grid
#full of pixel objects. It still contains the methods from the aboce class
#has its own specific clearGrid(). Using ____.clearGrid() will simply set the color
#to the original background color.
class pixelArt(grid):
def drawGrid(self):
self.grid = []
# Create pixels in the grid
for i in range(self.cols):
self.grid.append([])
for j in range(self.rows):
self.grid[i].append(pixel(i, j, self.width, self.height, self.cols, self.rows, self.startx, self.starty, self.showGrid))
self.grid[i][j].show(self.screen, (255,255,255), self.lineThick)
if self.showGrid:
self.grid[i][j].show(self.screen, (0,0,0), 1,False,True)
#This generates the neighbours of each pixel so that we can draw multiple thickness of lines
for c in range(self.cols):
for r in range(self.rows):
self.grid[c][r].getNeighbors(self.grid)
self.selected = self.grid[self.cols - 1][self.rows - 1]
def clearGrid(self): #This will set all of the pixels to the same color as the background color
for pixels in self.grid:
for p in pixels:
if self.showGrid: #If the grid is to be showing we must redraw the pixels so that we can see the grid after we change their color
p.show(self.screen, self.bg, 0)
p.show(self.screen, (0,0,0), 1)
else:
p.show(self.screen, self.bg, 0)
#This class is responsible for creating the color pallet in the bottom left hand side of the screen
#and is a concrete class. The setColor() method simply takes a list of colors and assigns them to pixels
#in the grid. This can only be called after the grid has been created.
class colorPallet(pixelArt):
def setColor(self, colorList): #The colorList argument passed to the function must be equal to the number of pixels in the grid
colourCount = 0
for pixels in self.getGrid():
for p in pixels:
p.show(self.screen, colorList[colourCount],0)
colourCount += 1
#This class creates basic grid menus that can contain text.
#It uses all of the methods from the parent grid class and is a concrete class
#The setText method takes a list of strings and displays them in the grid.
class menu(grid):
def setText(self, textList): #The textList argument passed must be equal to the number of spots in the grid
self.grid = []
# Create textObjects in the grid
for i in range(self.cols):
self.grid.append([])
for j in range(self.rows):
self.grid[i].append(textObject(i, j, self.width, self.height, self.cols, self.rows, self.startx, self.starty))
#Set the text for each of those objects
c = 0
for spots in self.getGrid():
for s in spots:
s.showText(self.screen, textList[c])
c += 1
#This class is responsible for displaying text and these objects are added into the grid.
#The showText() method will display the text while the show() method will draw a square showing thr grid.
class textObject():
def __init__(self, i, j, width, height, cols, rows, startx=0, starty=0):
self.col = i #The column of the current instance in the grid
self.row = j #The row of the current instance in the grid
self.rows = rows #Total amount of rows
self.cols = cols #Total amount of columns
self.w = width / cols
self.h = height / rows
self.x = self.col * self.w + startx
self.y = self.row * self.h + starty
self.text = ''
def showText(self, win, txt): #This will render and draw the text on the screen
self.text = txt
myFont = pygame.font.SysFont('comicsansms', 15)
text = myFont.render(self.text, 1, (0,0,0))
win.blit(text, (self.x + (self.w /2 - text.get_width() / 2), self.y + (self.h/2 - text.get_height() / 2))) #This will make sure the text is center in the screen.
def show(self, screen, color, st, outline=False): #Draws a square displaying the area in the grid
pygame.draw.rect(screen, color, (self.x, self.y, self.w, self.h), st)
#This pixel object is responsible for stroing a color and displaying it to the screen. These objects are added into the grid.
#The methods are named according to what they do.
class pixel():
def __init__(self, i,j, width, height, cols, rows, startx=0, starty=0, showGrid=False):
self.col = i #The column of the current instance
self.row = j #The row of the current instance
self.color = (255,255,255)
self.rows = rows #Amount of rows in whole grid
self.cols = cols #Amount of cols in whole grid
self.showGrid = showGrid
self.w = width / cols
self.h = height / rows
self.x = self.col * self.w + startx
self.y = self.row * self.h + starty
self.neighbors = []
def show(self, screen, color, st, outline=False, first=False): #Display the current pixel
if not(first):
self.color = color
pygame.draw.rect(screen, color, (self.x, self.y, self.w, self.h), st)
if self.showGrid and not(outline):
pygame.draw.rect(screen, (0,0,0), (self.x, self.y, self.w, self.h), 1)
def getPos(self):
return (self.col * self.w, self.row * self.h)#Return a tuple (x,y) of the top left co-ords of the pixel
def click(self, screen, color): #If the pixel has been clicked on call this and it will display the new color and set the color attribute for that pixel
self.show(screen, color, 0)
self.color = color
def getColor(self):
return self.color
def getNeighbors(self, grid):
# Get the neighbours of each pixel in the grid, this is used for drawing thicker lines
i = self.col #the var i is responsible for denoting the current col value in the grid
j = self.row #the var j is responsible for denoting the current row value in the grid
rows = self.rows
cols = self.cols
#Horizontal and vertical neighbors
if i < cols-1: #Right
self.neighbors.append(grid[i + 1][j])
if i > 0: #Left
self.neighbors.append(grid[i - 1][j])
if j < rows-1: #Up
self.neighbors.append(grid[i][j + 1])
if j > 0 : #Down
self.neighbors.append(grid[i][j - 1])
#Diagonal neighbors
if j > 0 and i > 0: #Top Left
self.neighbors.append(grid[i - 1][j - 1])
if j + 1 < rows and i > -1 and i - 1 > 0: #Bottom Left
self.neighbors.append(grid[i - 1][j + 1])
if j - 1 < rows and i < cols - 1 and j - 1 > 0: #Top Right
self.neighbors.append(grid[i + 1][j - 1])
if j < rows - 1 and i < cols - 1: #Bottom Right
self.neighbors.append(grid[i + 1][j + 1])
def neighborsReturn(self):
return self.neighbors #Return a list of the neighbours of the current pixel