edit: i made a huge mistake in the qeustion don't bother trying to help.
I'm trying but when ever run the code, I get a window that I can draw on but when i run (which i set the button to key pad enter) the simulation, it get rid of all the cell. code:
import pygame,sys,math
from pygame.constants import USEREVENT
pygame.init()
srceen_size = (900,900)
srceen = pygame.display.set_mode(srceen_size)
pygame.display.set_caption("conway game of life")
clock = pygame.time.Clock()
def D_list(list):
list2 = []
for i in list:
if i not in list2:
list2.append(i)
return list2
fps = 120
chuck = (320,320)
cell = []
cell_color = (120, 120, 139)
cell_size = 18
cell_delete = False
movement = USEREVENT
cell_create = False
cell_moving = False
cell_re = False
list_dumbie = []
index1 = 0
index4 = 0
gen = []
index2 = 0
index3 = 0
new_gen = []
celless = True
pygame.time.set_timer(movement, 3000)
while True:
clock.tick(fps)
if cell_create == True:
pos_mouse = pygame.mouse.get_pos()
x,y = pos_mouse
X = x % 20
Y = y % 20
index2 = index2 + 1
x, y = x - X, y - Y
cell.append((x,y))
cell = D_list(cell)
cell.sort()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
if event.key == pygame.K_KP_ENTER:
if cell_moving == True:
cell_moving = False
else:
cell_moving = True
if event.key == pygame.K_LSHIFT:
if cell_re == False:
cell_re = True
if cell_re == True:
cell_re = False
if event.type == pygame.MOUSEBUTTONDOWN:
if cell_re == False:
cell_create = True
cell_delete = False
if cell_re == True:
cell_create = False
cell_delete = True
if event.type == pygame.MOUSEBUTTONUP:
if cell_create == True:
cell_create = False
if cell_delete == True:
cell_delete = False
if event.type == movement: #this is where i had problem
if cell_moving == True:
for a in cell:
list_dumbie = cell
list_dumbie.pop(index1)
for b in list_dumbie:
if b[0] == a[0] - cell_size or b[0] == a[0] + cell_size:
if b[1] == a[1] - cell_size or b[1] == a[1] + cell_size:
index4 = index4 + 1
if index4 == 3 or index4 == 4:
new_gen.append(a)
index4 = 0
index1 = index1 + 1
cell = new_gen
new_gen = []
cell.sort()
index1 = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_LSHIFT:
cell_re = False
srceen.fill((22, 22, 22))
for x,y in cell:
pygame.draw.rect(srceen,cell_color,(x+1,y+1,cell_size,cell_size))
pygame.display.update()
also conway game of life is a cellular automata. there only two rules
- a cell stay alive if there 3 or 4 cell next to it and if more or less cell are next to the cell, the cell die
- if a dead cell surrounded by 3 living cell, the dead cell come alive.