Spaces:
Sleeping
Sleeping
| import numpy as np | |
| from life_game.arcade import HELPER_ITEMS_KEY, NEGATIVE_ITEMS_KEY, NEGATIVE_STRUCTURES_KEY | |
| from life_game.game import GameModel | |
| from life_game.render import ( | |
| BOSS_COLOR, | |
| BULLET_COLOR, | |
| ENEMY_COLOR, | |
| HELPER_ITEM_COLOR, | |
| NEGATIVE_ITEM_COLOR, | |
| NEGATIVE_STRUCTURE_COLOR, | |
| PLAYER_COLOR, | |
| TOWER_COLOR, | |
| cell_size_for, | |
| render_board, | |
| ) | |
| from life_game.rules import RULES | |
| def test_render_board_draws_semantic_overlay_glyphs(): | |
| grid = np.zeros((8, 8), dtype=np.uint8) | |
| enemies = np.zeros_like(grid, dtype=bool) | |
| boss = np.zeros_like(grid, dtype=bool) | |
| enemies[1, 1] = True | |
| boss[1, 5] = True | |
| game = GameModel( | |
| enemies=enemies, | |
| boss=boss, | |
| player=(2, 2), | |
| bullets=((3, 3),), | |
| towers=((4, 4),), | |
| data={ | |
| HELPER_ITEMS_KEY: ((5, 5),), | |
| NEGATIVE_ITEMS_KEY: ((5, 1),), | |
| NEGATIVE_STRUCTURES_KEY: ((6, 6),), | |
| }, | |
| ) | |
| image = render_board(grid, RULES["conway"], game) | |
| cell_size = cell_size_for(grid) | |
| assert image.dtype == np.uint8 | |
| assert image.shape == (grid.shape[0] * cell_size, grid.shape[1] * cell_size, 3) | |
| assert _cell_has_color(image, 1, 1, ENEMY_COLOR, cell_size) | |
| assert _cell_has_color(image, 1, 5, BOSS_COLOR, cell_size) | |
| assert _cell_has_color(image, 2, 2, PLAYER_COLOR, cell_size) | |
| assert _cell_has_color(image, 3, 3, BULLET_COLOR, cell_size) | |
| assert _cell_has_color(image, 4, 4, TOWER_COLOR, cell_size) | |
| assert _cell_has_color(image, 5, 5, HELPER_ITEM_COLOR, cell_size) | |
| assert _cell_has_color(image, 5, 1, NEGATIVE_ITEM_COLOR, cell_size) | |
| assert _cell_has_color(image, 6, 6, NEGATIVE_STRUCTURE_COLOR, cell_size) | |
| def _cell_has_color(image: np.ndarray, row: int, col: int, color: np.ndarray, cell_size: int) -> bool: | |
| region = image[row * cell_size : (row + 1) * cell_size, col * cell_size : (col + 1) * cell_size] | |
| return bool(np.any(np.all(region == color, axis=2))) | |