Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. # coding=utf-8
  2. import pygame
  3. from pygame.locals import QUIT, MOUSEMOTION, MOUSEBUTTONDOWN, KEYDOWN, K_RETURN
  4.  
  5.  
  6.  
  7.  
  8. BOARD_SIZE = (150, 100)
  9. CELL_SIZE = 8
  10. DISPLAY_SIZE = (BOARD_SIZE[0] * CELL_SIZE, BOARD_SIZE[1] * CELL_SIZE)
  11.  
  12.  
  13. class Board(object):
  14.  
  15. def __init__(self, width=DISPLAY_SIZE[0], height=DISPLAY_SIZE[1]):
  16. self.surface = pygame.display.set_mode((width, height), 0, 32)
  17.  
  18. def draw(self, *args):
  19. for drawable in args:
  20. drawable.draw_on(self.surface)
  21. pygame.display.update()
  22.  
  23.  
  24. class Population(object):
  25.  
  26. def __init__(self, cols=BOARD_SIZE[0], rows=BOARD_SIZE[1]):
  27. self.generation = [[False for row in range(rows)] for col in range(cols)]
  28. self.cols = cols
  29. self.rows = rows
  30.  
  31.  
  32. def draw_on(self, surface):
  33. for col in range(len(self.generation)):
  34. for row in range(len(self.generation[col])):
  35. if self.generation[col][row]:
  36. pygame.draw.rect(surface, (255,255,255), (col*CELL_SIZE, row*CELL_SIZE, CELL_SIZE, CELL_SIZE), 0)
  37.  
  38. def check_cells(self, x, y): #zlicza żywe komórki naokoło
  39. count = 0
  40. for nx in (-1, 0, 1):
  41. if (x + nx) >= self.cols or (x + nx) < 0: continue
  42. for ny in (-1, 0, 1):
  43. if (y + ny) >= self.rows or (y + ny) < 0 or (nx == ny == 0): continue
  44. if self.generation[x+nx][y+ny] == True: count +=1
  45. return count
  46.  
  47. def handle_mouse(self):
  48. buttons = pygame.mouse.get_pressed()
  49. if any(buttons):
  50. pos = pygame.mouse.get_pos()
  51. x, y = int(pos[0]/CELL_SIZE), int(pos[1]/CELL_SIZE)
  52. #zamiana wartości komorki
  53. self.generation[x][y] = not self.generation[x][y]
  54.  
  55. def next_gen(self):
  56. self.temp = [[False for y in range(self.rows)] for x in range(self.cols)]
  57. for x in range(self.cols):
  58. for y in range(self.rows):
  59. if self.check_cells(x, y) == 2:
  60. self.temp[x][y] = self.generation[x][y]
  61. elif self.check_cells(x, y) == 3:
  62. self.temp[x][y] = True
  63. else:
  64. self.temp[x][y] = False
  65.  
  66. self.generation = self.temp
  67.  
  68.  
  69.  
  70.  
  71.  
  72. class Game(object):
  73.  
  74. def __init__(self):
  75. pygame.init()
  76. self.board = Board()
  77. self.population = Population()
  78. self.clock = pygame.time.Clock()
  79. self.started = False
  80.  
  81.  
  82. def run(self):
  83. while not self.handle_events():
  84. self.board.draw(self.population)
  85. if self.started:
  86. self.population.next_gen()
  87. self.clock.tick(15)
  88.  
  89.  
  90. def handle_events(self):
  91. for event in pygame.event.get():
  92. if event.type == QUIT:
  93. pygame.quit()
  94. return True
  95.  
  96. if event.type == MOUSEMOTION or event.type == MOUSEBUTTONDOWN:
  97. self.population.handle_mouse()
  98.  
  99. if event.type == KEYDOWN and event.key == K_RETURN:
  100. self.started = not self.started
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. if __name__ == "__main__":
  109. game = Game()
  110. game.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement