-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
118 lines (102 loc) · 3.09 KB
/
main.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
import copy
import pygame
from pygame import *
import random
import time
import sys
# Set window dimensions and block size
WINDOW_HEIGHT = 800
WINDOW_WIDTH = 800
blockSize = 30 # Size of each grid block
# Calculate the number of rows and columns based on the window size and block size
rows = WINDOW_HEIGHT // blockSize
cols = WINDOW_WIDTH // blockSize
# Colors
BLACK = (0, 0, 0)
WHITE = (200, 200, 200)
CAL = (111,222,178)
SHADOW = (192, 192, 192)
WHITE = (255, 255, 255)
LIGHTGREEN = (0, 255, 0 )
GREEN = (0, 200, 0 )
BLUE = (0, 0, 128)
LIGHTBLUE = (0, 0, 255)
RED = (200, 0, 0 )
LIGHTRED = (255, 100, 100)
PURPLE = (102, 0, 102)
# Initialize grid arrays
arr = [[0 for i in range(cols)] for j in range(rows)]
parr = [[0 for i in range(cols)] for j in range(rows)]
# Randomly initialize some grid cells
for _ in range(190):
x = random.randint(2, rows - 1)
y = random.randint(2, cols - 1)
arr[x][y] = 1
def main():
z = 0
global SCREEN, CLOCK
pygame.init()
SCREEN = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
font = pygame.font.Font(pygame.font.get_default_font(), 36)
CLOCK = pygame.time.Clock()
SCREEN.fill(BLACK)
while True:
z += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
r = event.pos
arr[int(r[0] / blockSize)][int(r[1] / blockSize)] = 1
text_surface = font.render(str(int(r[0] / blockSize)), True, RED)
SCREEN.blit(text_surface, (102, 200))
drawGrid()
text_surface = font.render(str(z), True, RED)
SCREEN.blit(text_surface, (2, 2))
pygame.display.update()
time.sleep(0.1)
def drawGrid():
for x in range(rows):
for y in range(cols):
l = arr[x][y]
if l == 0:
col = BLACK
elif l == 1:
col = CAL
elif l == 2:
col = RED
else:
col = GREEN
rect = pygame.Rect(x * blockSize, y * blockSize, blockSize, blockSize)
pygame.draw.rect(SCREEN, col, rect, 0)
new(x, y)
for p in range(rows):
for q in range(cols):
if arr[p][q] == parr[p][q] and arr[p][q] > 0:
arr[p][q] += 1
else:
arr[p][q] = parr[p][q]
def new(x, y):
if x == 0 or y == 0 or x == rows - 1 or y == cols - 1:
return
i = 0
# Check surrounding cells
if arr[x-1][y-1] != 0: i += 1
if arr[x][y-1] != 0: i += 1
if arr[x+1][y-1] != 0: i += 1
if arr[x-1][y] != 0: i += 1
if arr[x+1][y] != 0: i += 1
if arr[x-1][y+1] != 0: i += 1
if arr[x][y+1] != 0: i += 1
if arr[x+1][y+1] != 0: i += 1
# Update the cell based on neighbor count
if i < 2 and arr[x][y] != 0:
parr[x][y] = 0
elif i > 3 and arr[x][y] != 0:
parr[x][y] = 0
elif i == 3 and arr[x][y] == 0:
parr[x][y] = 1
else:
parr[x][y] = arr[x][y]
main()