-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
executable file
·73 lines (57 loc) · 1.95 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env
import sys, os, pygame, random
from pygame.locals import *
from cell import Cell
from consts import *
from sound import Sound
class Game:
def __init__(self, scale, vol, colors):
self.colors = colors
self.__init_randomized_cells()
self.surface = pygame.Surface(screen_size)
self.sound = Sound(scale, vol)
def __init_randomized_cells(self):
self.cells = []
for y in range(game_height):
row = []
self.cells.append(row)
for x in range(game_width):
row.append(Cell(x, y, self.colors, random.choice([True, False, False, False])))
self.cellsT = [list(i) for i in zip(*self.cells)]
def playSound(self, ticks):
if len != 0:
self.sound.play(len(filter(lambda c: c.alive, self.cellsT[ticks])))
def change_column_color(self, ticks):
for cell in self.cellsT[ticks]:
cell.switch_color()
def draw(self):
self.surface.fill(black)
for x in range(game_width):
for y in range(game_height):
cell = self.cells[y][x]
if cell.alive:
pygame.draw.rect(self.surface, cell.color, cell.rect)
def iterate(self):
for x in range(game_width):
for y in range(game_height):
cell = self.cells[y][x]
cell.calculate_next(len(self.__alive_neighbours(cell)))
def new_generation(self):
for x in range(game_width):
for y in range(game_height):
cell = self.cells[y][x]
cell.new_generation()
def __neighbours(self, cell):
iter_start_x = cell.x - 1
iter_start_y = cell.y - 1
iter_end_x = cell.x + 1
iter_end_y = cell.y + 1
neighbours = []
for xi in range(iter_start_x, iter_end_x+1):
for yi in range(iter_start_y, iter_end_y+1):
if cell.x == xi and cell.y == yi:
continue
neighbours.append(self.cells[yi % game_height][xi % game_width])
return neighbours
def __alive_neighbours(self, cell):
return filter(lambda c: c.is_alive(), self.__neighbours(cell))