![]() Draw - A method that I use to draw the nodes onto the window, note- these won’t have the black borders around them so we need to add these separately.If you want an explanation of the grid, I would recommend going and looking at my code for chess. import pygame import sys WIDTH = 800 ROWS = 20 WIN = _mode((WIDTH, WIDTH)) _caption("Game of Life") WHITE = (255, 255, 255) BLACK = (0, 0, 0) class Node: def _init_(self, row, col, width): self.row = row l = col self.x = int(row * width) self.y = int(col * width) lour = WHITE self.occupied = None def draw(self, WIN): (WIN, lour, (self.x, self.y, WIDTH / 8, WIDTH / 8)) def make_grid(rows, width): grid = gap = WIDTH // rows print(gap) for i in range(rows): grid.append() for j in range(rows): node = Node(j, i, gap) grid.append(node) return grid def draw_grid(win, rows, width): gap = width // ROWS for i in range(rows): (win, BLACK, (0, i * gap), (width, i * gap)) for j in range(rows): (win, BLACK, (j * gap, 0), (j * gap, width)) """ The nodes are all white so this we need to draw the grey lines that separate all the chess tiles from each other and that is what this function does""" def update_display(win, grid, rows, width): for row in grid: for spot in row: spot.draw(win) draw_grid(win, rows, width) () def Find_Node(pos, WIDTH): interval = WIDTH / ROWS y, x = pos rows = y // interval columns = x // interval return int(rows), int(columns) def neighbour(tile): col, row = tile.row, l # print(row, col) neighbours =, ,, ,, ,, , ] actual = for i in neighbours: row, col = i if 0 <= row <= (ROWS - 1) and 0 <= col <= (ROWS - 1): actual.append(i) # print(row, col, actual) return actual def update_grid(grid): newgrid = for row in grid: for tile in row: neighbours = neighbour(tile) count = 0 for i in neighbours: row, col = i if lour = BLACK: count += 1 if lour = BLACK: if count = 2 or count = 3: newgrid.append(BLACK) else: newgrid.append(WHITE) else: if count = 3: newgrid.append(BLACK) else: newgrid.append(WHITE) return newgrid def main(WIN, WIDTH): run = None grid = make_grid(ROWS, WIDTH) while True: (50) #stops cpu dying for event in (): if event.type = pygame.QUIT: pygame.quit() sys.exit() if event.type = pygame.KEYDOWN: if event.key = pygame.K_SPACE: run = True if event.type = pygame.MOUSEBUTTONDOWN: pos = _pos() row, col = Find_Node(pos, WIDTH) if lour = WHITE: lour = BLACK elif lour = BLACK: lour = WHITE while run: for event in (): if event.type = pygame.MOUSEBUTTONDOWN: run = False #(50) newcolours = update_grid(grid) count=0 for i in range(0,len(grid)): for j in range(0, len(grid)): lour=newcolours count+=1 update_display(WIN, grid, ROWS, WIDTH) #run= False update_display(WIN, grid, ROWS, WIDTH) main(WIN, WIDTH) Code Explained: import pygame import sys WIDTH = 800 ROWS = 20 WIN = _mode((WIDTH, WIDTH)) _caption("Game of Life") WHITE = (255, 255, 255) BLACK = (0, 0, 0) class Node: def _init_(self, row, col, width): self.row = row l = col self.x = int(row * width) self.y = int(col * width) lour = WHITE self.occupied = None def draw(self, WIN): (WIN, lour, (self.x, self.y, WIDTH / 8, WIDTH / 8)) def make_grid(rows, width): grid = gap = WIDTH // rows print(gap) for i in range(rows): grid.append() for j in range(rows): node = Node(j, i, gap) grid.append(node) return grid def draw_grid(win, rows, width): gap = width // ROWS for i in range(rows): (win, BLACK, (0, i * gap), (width, i * gap)) for j in range(rows): (win, BLACK, (j * gap, 0), (j * gap, width)) """ The nodes are all white so this we need to draw the grey lines that separate all the chess tiles from each other and that is what this function does""" def update_display(win, grid, rows, width): for row in grid: for spot in row: spot.draw(win) draw_grid(win, rows, width) () def Find_Node(pos, WIDTH): interval = WIDTH / ROWS y, x = pos rows = y // interval columns = x // interval return int(rows), int(columns)Īll of this code is a copy from the chess program I made, and I basically just creates the tiles of the grid using a class which I have called a Node. Full Codeįirtst I’ll share the full code and then explain it below. So I copied most of the grid set-up (I might have accidentally left a new comments from the chess code too). ![]() If you look at the code, you might realise that it using very similar code to the chess grid, and this is because a lot of this program is a copy of the chess with new logic. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.Any live cell with more than three live neighbours dies, as if by overpopulation. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |