forked from AndreasErodotou/Conways-Game-Of-Life
/
Grid.py
244 lines (215 loc) · 9.88 KB
/
Grid.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
import pygame as pg
from random import randint, getrandbits
from Tile import Tile
from Logic import Logic
class Grid():
SCREEN_WIDTH = 500 # width (in px)
SCREEN_HEIGHT = 500 # height (in px)
TileWidth = 1 # initializing the width of tile (in px)
TileHeight = 1 # initializing the height of tile (in px)
tilesMatrix = [] # initializing the matrix of Tile instances
WHITE=(255,255,255)
BLACK=(0,0,0)
RANDOM_COLOR = (randint(0,255),randint(0,100),randint(0,255))
def __init__(self, ni, nj):
self.speed = 10 # frames per second when the game is running
self.ni = ni
self.nj = nj
pg.init()
self.WIN = pg.display.set_mode((Grid.SCREEN_WIDTH, Grid.SCREEN_HEIGHT), pg.RESIZABLE)
pg.display.set_caption('Conway\'s Game of Life v02')
for i in range(self.ni):
tempLst = []
for j in range(self.nj):
tempLst.append(Tile(xpos=i * Grid.TileWidth, ypos=j * Grid.TileHeight, state=0, i=i, j=j)) # getrandbits(1)
Grid.tilesMatrix.append(tempLst)
for i in range(100):
Grid.tilesMatrix[randint(0,ni-1)][randint(0,nj-1)].setState(1)
self.logic = Logic(Grid.tilesMatrix)
self.SCREEN_CENTER = (Grid.SCREEN_WIDTH/2, Grid.SCREEN_HEIGHT/2)
self.gameLoop()
def initialize(self):
Grid.TileHeight = Grid.SCREEN_HEIGHT / self.nj
Grid.TileWidth = Grid.SCREEN_WIDTH / self.ni
for i in range(self.ni):
tempLst = []
for j in range(self.nj):
Grid.tilesMatrix[i][j].setX(i * Grid.TileWidth)
Grid.tilesMatrix[i][j].setY(j * Grid.TileHeight)
def gameLoop(self):
clock = pg.time.Clock()
running = True
started = False
one = 1
while (running):
if (started):
self.logic.checker()
clock.tick(self.speed)
ev = pg.event.get() # get all events
for event in ev:
if event.type == pg.MOUSEBUTTONUP:
pos = pg.mouse.get_pos() # x and y
self.pressed(pos)
if event.type == pg.QUIT:
running = False
if event.type == pg.VIDEORESIZE:
self.WIN = pg.display.set_mode((event.w, event.h), pg.RESIZABLE)
Grid.SCREEN_WIDTH = event.w
Grid.SCREEN_HEIGHT = event.h
self.initialize()
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
if one > 0:
started = True
one = -1
else:
started = False
one = +1
if event.key == pg.K_p:
self.mainMenu()
if pg.mouse.get_pressed()[0]:
self.pressed(pg.mouse.get_pos())# x and y
self.drawGrid()
pg.display.update() # updates the screen
def mainMenu(self):
inMenu = True
while inMenu:
boxX = Grid.SCREEN_WIDTH/6
boxY = Grid.SCREEN_HEIGHT/6
boxWidth = Grid.SCREEN_WIDTH - boxX * 2
boxHight = Grid.SCREEN_HEIGHT - boxY * 2
boxCenterX = boxWidth/2 + boxX
boxCenterY = boxHight/2 + boxY
Returnwidth=200
Returnheight=60
Settingswidth=200
Settingsheight=60
Exitwidth=200
Exitheight=60
spaceY = 40
ReturnxPos=boxCenterX-Returnwidth/2
ReturnyPos=boxCenterY-spaceY-Returnheight-Settingsheight/2
SettingsxPos=boxCenterX-Settingswidth/2
SettingsyPos=boxCenterY-Settingsheight/2
ExitxPos=boxCenterX-Exitwidth/2
ExityPos=boxCenterY+spaceY+Exitheight/2
pg.draw.rect(self.WIN,(10,10,10),(boxX, boxY , boxWidth, boxHight))
self.drawText(text='Start', color=self.BLACK, xPos=ReturnxPos, yPos=ReturnyPos, width=Returnwidth, height=Returnheight, size=80)
self.drawText(text='Settings', color=self.BLACK, xPos=SettingsxPos, yPos=SettingsyPos, width=Settingswidth, height=Settingsheight, size=60)
self.drawText(text='Exit', color=self.BLACK, xPos=ExitxPos, yPos=ExityPos, width=Exitwidth, height=Exitheight, size=60)
pg.display.update() # displays the drawScreen() stuff on the actual screen
inMenu = self.checkForEventsINMAInMenus(MENU=True)
def checkForEventsINMAInMenus(self, MENU=False, SETTINGS=False):
inMenu = True
ev = pg.event.get() # get all events
for event in ev:
if event.type == pg.MOUSEBUTTONUP:
pos = pg.mouse.get_pos() # x and y
#inMenu = False
if event.type == pg.QUIT:
quit()
if event.type == pg.VIDEORESIZE:
self.WIN = pg.display.set_mode((event.w, event.h), pg.RESIZABLE)
Grid.SCREEN_WIDTH = event.w
Grid.SCREEN_HEIGHT = event.h
self.initialize()
keys = pg.mouse.get_pressed() #checking pressed keys
if keys[0]:
pos = pg.mouse.get_pos() # x and y
inMenu = self.pressedMenu(pos, menu=MENU, settings=SETTINGS)
return inMenu
def settingsMenu(self):
insettingsMenu = True
while insettingsMenu:
boxX = Grid.SCREEN_WIDTH/6
boxY = Grid.SCREEN_HEIGHT/6
boxWidth = Grid.SCREEN_WIDTH - boxX * 2
boxHight = Grid.SCREEN_HEIGHT - boxY * 2
boxCenterX = boxWidth/2 + boxX
boxCenterY = boxHight/2 + boxY
Returnwidth=200
Returnheight=60
Settingswidth=200
Settingsheight=60
Backwidth=200
Backheight=60
spaceY = 40
ReturnxPos=boxCenterX-Returnwidth/2
ReturnyPos=boxCenterY-spaceY-Returnheight-Settingsheight/2
SettingsxPos=boxCenterX-Settingswidth/2
SettingsyPos=boxCenterY-Settingsheight/2
BackxPos=boxCenterX-Backwidth/2
BackyPos=boxCenterY+spaceY+Backheight/2
pg.draw.rect(self.WIN,(10,10,10),(boxX, boxY , boxWidth, boxHight))
self.drawText(text='Setting 1', color=self.BLACK, xPos=ReturnxPos, yPos=ReturnyPos, width=Returnwidth, height=Returnheight, size=60)
self.drawText(text='Setting 2', color=self.BLACK, xPos=SettingsxPos, yPos=SettingsyPos, width=Settingswidth, height=Settingsheight, size=60)
self.drawText(text='Back', color=self.BLACK, xPos=BackxPos, yPos=BackyPos, width=Backwidth, height=Backheight, size=60)
pg.display.update() # displays the drawScreen() stuff on the actual screen
insettingsMenu = self.checkForEventsINMAInMenus(SETTINGS=True)
def drawText(self, text, color, xPos, yPos, width, height, size):
pg.draw.rect(self.WIN, Grid.RANDOM_COLOR, (xPos, yPos, width, height))
font = pg.font.Font(None, size)
text_ = font.render(text, True, color)
text_rect = text_.get_rect(center=(xPos+width/2, yPos+height/2))
if text_ is not None: self.WIN.blit(text_, text_rect)
def pressedMenu(self, pos, menu=False, settings=False):
boxX = Grid.SCREEN_WIDTH/6
boxY = Grid.SCREEN_HEIGHT/6
boxWidth = Grid.SCREEN_WIDTH - boxX * 2
boxHight = Grid.SCREEN_HEIGHT - boxY * 2
boxCenterX = boxWidth/2 + boxX
boxCenterY = boxHight/2 + boxY
Returnwidth=200
Returnheight=60
Settingswidth=200
Settingsheight=60
Exitwidth=200
Exitheight=60
spaceY = 40
ReturnxPos=boxCenterX-Returnwidth/2
ReturnyPos=boxCenterY-spaceY-Returnheight-Settingsheight/2
SettingsxPos=boxCenterX-Settingswidth/2
SettingsyPos=boxCenterY-Settingsheight/2
ExitxPos=boxCenterX-Exitwidth/2
ExityPos=boxCenterY+spaceY+Exitheight/2
pressedSomething = False
# Return
if (pos[0] > ReturnxPos and pos[0] < (ReturnxPos + Returnwidth)):
if (pos[1] > ReturnyPos and pos[1] < (ReturnyPos + Returnheight)):
if menu:
return False
if settings:
return False
pressedSomething = True
# Settings
if (pos[0] > SettingsxPos and pos[0] < SettingsxPos + Settingswidth):
if (pos[1] > SettingsyPos and pos[1] < SettingsyPos + Settingsheight):
self.settingsMenu()
pressedSomething = True
return True
# Exit
if (pos[0] > ExitxPos and pos[0] < ExitxPos + Exitwidth):
if (pos[1] > ExityPos and pos[1] < ExityPos + Exitheight):
if menu:
quit()
if settings:
pressedSomething = True
return False
if not pressedSomething:
return True
def pressed(self, pos):
for x in range(self.ni):
for y in range(self.nj):
if (pos[0] > Grid.tilesMatrix[x][y].getX() and pos[0] < Grid.tilesMatrix[x][y].getX() + Grid.TileWidth):
if (pos[1] > Grid.tilesMatrix[x][y].getY() and pos[1] < Grid.tilesMatrix[x][y].getY() + Grid.TileHeight):
Grid.tilesMatrix[x][y].setState(1)
def drawGrid(self):
self.WIN.fill(Grid.BLACK)
for i in range(self.ni):
for j in range(self.nj):
if (Grid.tilesMatrix[i][j].getState() == 0):
color = Grid.BLACK
elif (Grid.tilesMatrix[i][j].getState() == 1):
color = Grid.RANDOM_COLOR
pg.draw.rect(self.WIN,color,(Grid.tilesMatrix[i][j].getX()+1,Grid.tilesMatrix[i][j].getY()+1,Grid.TileWidth-1,Grid.TileHeight-1))
Grid(50,50) # ni, nj