python - Conways Game of Life in PyGame -


so read conways game of life , tried implement pygame.

i tried make object-oriented. way works have list of cell-instances, check how many neighbours have , either stay alive or die, based on neighbours. process repeats itself.

the problem when test known starting patterns (e.g. in code below (cell_map)) not work way should.

i read code on , on , dont i'm missing here. posted whole code below dont know mistake is, i'd highly appreciate if point me in right direction.

thanks in advance!

import pygame  class cell:     def __init__(self, live, xcor, ycor):         self.alive = live         self.x = xcor         self.y = ycor         self.neighbours = 0  def checkneighbours(self, celllist):     cell in celllist:         #left         if cell.x == self.x-1 , cell.y == self.y , cell.alive == true:             self.neighbours += 1                 #right         elif cell.x == self.x+1 , cell.y == self.y , cell.alive == true:             self.neighbours += 1          #upleft         elif cell.x == self.x-1 , cell.y == self.y-1 , cell.alive == true:             self.neighbours += 1              #up         elif cell.x == self.x , cell.y == self.y-1 , cell.alive == true:             self.neighbours += 1          #upright         elif cell.x == self.x+1 , cell.y == self.y-1 , cell.alive == true:             self.neighbours += 1              #downleft         elif cell.x == self.x-1 , cell.y == self.y+1 , cell.alive == true:             self.neighbours += 1              #down         elif cell.x == self.x , cell.y == self.y+1 , cell.alive == true:             self.neighbours += 1          #downright         elif cell.x == self.x+1 , cell.y == self.y+1 , cell.alive == true:             self.neighbours += 1   def breed(self):     if self.alive == false , self.neighbours == 3:         #dead cell ressurects if neighbours equals 3         self.alive = true     elif self.alive , self.neighbours < 2:         #die loneliness         self.alive = false     elif self.alive , self.neighbours == 2:         #stay alive         pass     elif self.alive , self.neighbours == 3:         #stay alive         pass     elif self.alive , self.neighbours > 3:         #die overpopulation         self.alive = false  def render(self, display):     if self.alive:         pygame.draw.rect(display, (0,0,0), [self.x*10, self.y*10, 10, 10])     elif self.alive == false:         pygame.draw.rect(display, (0,0,255), [self.x*10, self.y*10, 10, 10])     wid = 33 hei = 20             cell_map = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]  cell_list = []  xc = -1 yc = -1 ylist in cell_map:     yc += 1     x in ylist:         xc += 1         if x == 0:             #create dead cell             newcell = cell(false, xc, yc)             cell_list.append(newcell)         elif x == 1:             #create alive cell             newcell = cell(true, xc, yc)             cell_list.append(newcell)     xc = -1    #pygame init  pygame.init() (width, height) = (wid*10, hei*10)  pygame.display.set_caption('game of life')  screen = pygame.display.set_mode((width, height))  #game loop  def gameloop():     gameloop = true       while gameloop:         #check exit         event in pygame.event.get():             if event.type == pygame.quit:                 gameloop = false                 pygame.quit()                         #render cells         cell in cell_list:             cell.render(screen)              #check neighbours         cell in cell_list:             cell.checkneighbours(cell_list)          pygame.display.flip()          #breed         cell in cell_list:             cell.breed()          pygame.time.wait(5)      quit()  if __name__ == "__main__":     gameloop() 

i don't have pygame installed, can't run code. however, bug that's causing error don't reset cell's neighbour count 0 after you've determined whether it'll alive or dead in next generation. in each generation each cell's new neighbour count gets added previous accumulated neighbour count. should resetting in .breed method.

here's more compact version of method:

def breed(self):     self.alive = self.neighbours == 3 or self.alive , self.neighbours == 2     self.neighbours = 0 

i have few more comments code.

your checkneighbours method extremely inefficient: each cell, scans entire grid looking cell's neighbours! simple alternative store cells in 2d list can locate cell's neighbours.


here's more compact way build cell_list code does:

cell_list = [] y, row in enumerate(cell_map):     x, v in enumerate(row):         cell_list.append(cell(v == 1, x, y)) 

here's same thing list comprehension:

cell_list = [cell(bool(v), x, y)     y, row in enumerate(cell_map)         x, v in enumerate(row) ] 

but said earlier, it's idea make cell_list 2d list:

cell_list = [[cell(bool(v), x, y) x, v in enumerate(row)]     y, row in enumerate(cell_map)] 

your cell_map isn't convenient way put life patterns program, guess it's ok testing purposes. take @ this answer wrote earlier month alternative.

eventually, should give program ability read common rle format used many life programs.

you may check out moderately efficient version wrote uses numpy: numpy_life.py. other version linked displays output in linux terminal, both versions should easy adapt pygame or gui framework.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -