Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. import curses
  2. import random
  3.  
  4. class Cell:
  5.     alive = False
  6.  
  7.     def arise(self):
  8.         self.alive = True
  9.  
  10.     def kill(self):
  11.         self.alive = False
  12.  
  13. class Grid:
  14.     def __init__(self, height, width):
  15.             self.height = height
  16.             self.width = width
  17.             self.grid = [[Cell() for y in range(height)] for x in range(width)]
  18.  
  19.     def countAliveNeighbours(self, y, x):
  20.         c = 0
  21.  
  22. class gui:
  23.     def __init__(self, screen, grid):
  24.         self.screen = screen        
  25.         self.grid = grid
  26.  
  27.     def drawBorder(self):
  28.         pass
  29.  
  30. def main(screen):
  31.     num_rows, num_cols = screen.getmaxyx()
  32.     screen.addstr("cols: " + str(num_cols) + "\n")
  33.     screen.addstr("rows: " + str(num_rows))
  34.    
  35.     screen.refresh()
  36.     screen.getch()
  37.    
  38.     grid = Grid(num_rows, num_cols)
  39.     screen.clear()
  40.     gameRandomizer(screen, grid)
  41.     drawMap(screen, grid)
  42.     screen.refresh()
  43.     screen.getch()
  44.  
  45. def gameRandomizer(screen, grid: Grid, qty = 100):
  46.     initialCells = random.randrange(1,qty)
  47.  
  48.     for cell in range(initialCells):
  49.         y = random.randrange(1, grid.height)
  50.         x = random.randrange(1, grid.width)
  51.         while grid.grid[x][y].alive == True:
  52.             y = random.randrange(1, grid.height)
  53.             x = random.randrange(1, grid.width)
  54.         grid.grid[x][y].arise()
  55.  
  56. def drawMap(screen, grid: Grid):
  57.     for x in range(grid.width):
  58.         for y in range(grid.height):
  59.             if grid.grid[x][y].alive == True:
  60.                 drawCell(screen, y, x)
  61.  
  62. def drawCell(screen, y, x, sign="x"):
  63.     screen.addstr(y, x, "x")
  64.  
  65. if __name__ == "__main__":
  66.     curses.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement