本文整理匯總了Python中game_of_life.GameOfLife.next_generation方法的典型用法代碼示例。如果您正苦於以下問題:Python GameOfLife.next_generation方法的具體用法?Python GameOfLife.next_generation怎麽用?Python GameOfLife.next_generation使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類game_of_life.GameOfLife
的用法示例。
在下文中一共展示了GameOfLife.next_generation方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: GameOfLifeTest
# 需要導入模塊: from game_of_life import GameOfLife [as 別名]
# 或者: from game_of_life.GameOfLife import next_generation [as 別名]
class GameOfLifeTest(unittest.TestCase):
def setUp(self):
self.game = GameOfLife({Vec2D(0, 0): Cell(True), Vec2D(0, 1): Cell(True),
Vec2D(1, 0): Cell(True), Vec2D(3, 3): Cell(True)})
def test_alive_cell_when_game_is_set_up(self):
self.assertEqual(self.game.is_alive(Vec2D(1, 0)), True)
self.assertEqual(self.game.is_alive(Vec2D(1, 1)), False)
def test_number_of_alive_neighbours(self):
self.assertEqual(self.game.number_of_alive_neighbours(Vec2D(0, 0)), 2)
self.assertEqual(self.game.number_of_alive_neighbours(Vec2D(1, 1)), 3)
self.assertEqual(self.game.number_of_alive_neighbours(Vec2D(4, 0)), 0)
def test_neighbours_of(self):
self.assertEqual(sorted(self.game.neighbours_of(Vec2D(0, 0))), sorted([Vec2D(-1, -1), Vec2D(-1, 0), Vec2D(-1, 1),
Vec2D(0, -1), Vec2D(0, 1),
Vec2D(1, -1), Vec2D(1, 0), Vec2D(1, 1)]))
def test_will_be_born(self):
self.assertEqual(self.game.will_be_born(Vec2D(1, 1)), True)
self.assertEqual(self.game.will_be_born(Vec2D(0, 1)), False)
def test_will_servive(self):
self.assertEqual(self.game.will_servive(Vec2D(0, 1)), True)
self.assertEqual(self.game.will_servive(Vec2D(3, 3)), False)
def test_stable_cells(self):
stable_cells = self.game.stable_cells()
self.assertEqual(sorted(stable_cells.keys()), sorted([Vec2D(0, 0), Vec2D(0, 1), Vec2D(1, 0)]))
self.assertEqual(stable_cells[Vec2D(0, 0)].new_born, False)
def test_new_born_cells(self):
new_born_cells = self.game.new_born()
self.assertEqual(sorted(new_born_cells.keys()), [Vec2D(1, 1)])
self.assertEqual(new_born_cells[Vec2D(1, 1)].new_born, True)
def test_next_generation(self):
next_generation = self.game.next_generation()
self.assertEqual(sorted(next_generation.keys()), sorted([Vec2D(0, 0), Vec2D(0, 1), Vec2D(1, 0), Vec2D(1, 1)]))
示例2: __init__
# 需要導入模塊: from game_of_life import GameOfLife [as 別名]
# 或者: from game_of_life.GameOfLife import next_generation [as 別名]
class GUIGameOfLife:
def __init__(self, settings=Settings()):
pygame.init()
self.generation = {}
self.game = GameOfLife(self.generation, settings)
self.clock = pygame.time.Clock()
self.screen = pygame.display.set_mode((configs.SCREENWIDTH, configs.SCREENHEIGHT))
self.background = pygame.Surface(self.screen.get_size())
self.background.fill(configs.BLACK)
pygame.display.set_caption('Game of Life')
while self.play_game():
pass
def play_game(self):
while True:
pygame.time.wait(1000)
self.clock.tick(10)
self.draw_cells()
pygame.display.update()
for event in pygame.event.get():
print(event)
escape = event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == escape:
pass
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
self.restart()
elif event.type == pygame.MOUSEBUTTONDOWN:
mousebuttondown = True
while mousebuttondown:
pos = pygame.mouse.get_pos()
self.new_cell(Vec2D(pos[0]/configs.PIXEL_SIZE, pos[1]/configs.PIXEL_SIZE))
self.game = GameOfLife(self.generation)
self.draw_cells()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
mousebuttondown = False
self.generation = self.game.next_generation()
self.game = GameOfLife(self.generation)
def draw_cells(self):
self.screen.fill(configs.BLACK)
for coords, cell in self.game.alive_cells.items():
if cell.new_born:
color = configs.GREEN
else:
color = configs.WHITE
x, y = coords.x*configs.PIXEL_SIZE, coords.y*configs.PIXEL_SIZE
pygame.draw.rect(self.screen, color, (x, y, configs.PIXEL_SIZE, configs.PIXEL_SIZE))
def new_cell(self, coords):
cell = {coords: Cell(True)}
self.generation.update(cell)
def restart(self):
self.generation = {}
self.game = GameOfLife(self.generation)
self.screen.fill(configs.BLACK)
pygame.display.update()