def __init__(self, tiles=None): self.tiles = tiles if self.tiles is None: self.tiles = [ Tile(color, value) for color in range(1, 5) for value in range(1, 14) for _ in range(2) ] + [Tile(0, 0), Tile(0, 0)]
class TestLast(unittest.TestCase): tiles = [Tile(5, 4), Tile(4, 5)] def test_last(self): """ Returns the last tile in a list of tiles """ result = find.last(self.tiles) self.assertEqual(result, self.tiles[-1])
def generate_level(level, all_sprites, tile_group, player_group, tile_size): player1, player2, x, y = None, None, None, None for y in range(len(level)): for x in range(len(level[y])): if level[y][x] in " .": pass elif level[y][x] == 'p': player1 = Player( "Player 1", x, y, all_sprites, player_group, pygame.transform.scale( load_image('Player1.png', -1), tuple(map(int, (tile_size * 0.8, tile_size * 0.8)))), tile_size) elif level[y][x] == 'q': player2 = Player( "Player 2", x, y, all_sprites, player_group, pygame.transform.scale( load_image('Player2.png', -1), tuple(map(int, (tile_size * 0.8, tile_size * 0.8)))), tile_size) else: Tile( tile_render[level[y][x]], x, y, all_sprites, tile_group, pygame.transform.scale( load_image(tile_render[level[y][x]] + '.png'), (tile_size, tile_size)), tile_size) return player1, player2
class TestMatchingTile(unittest.TestCase): data = [Tile(5, 4), Tile(4, 5)] def test_matching_tile(self): """ Returns a successful match """ result = find.matching_tile(self.data[0], self.data) self.assertEqual(result, True) def test_non_matching_tile(self): """ Returns an unsuccessful match """ result = find.matching_tile(Tile(1, 1), self.data) self.assertEqual(result, False)
class TestExists(unittest.TestCase): tiles = [Tile(5, 4), Tile(4, 5)] def test_tile_exists(self): """ Returns a successful match """ result = find.exists(self.tiles[0], self.tiles) self.assertEqual(result, True) def test_does_not_exist(self): """ Returns an unsuccessful match """ result = find.exists(Tile(1, 1), self.tiles) self.assertEqual(result, False)
def tiles(): dominoes = [] for side1 in range(7): for side2 in range(7): tile = Tile(side1, side2) if not find.exists(tile, dominoes): dominoes.append(tile) return dominoes
class TestIsDouble(unittest.TestCase): tile1 = Tile(5, 4) tile2 = Tile(2, 2) def test_is_double_success(self): """ returns a positive match """ result = compare.is_double(self.tile2) self.assertEqual(result, True) def test_is_double_failure(self): """ returns a negative match """ result = compare.is_double(self.tile1) self.assertEqual(result, False)
def __init__(self, w, h): self.width = w self.height = h self.size = [self.width, self.height] self.board = [] for i in range(self.height): self.board.append([]) for j in range(self.width): self.board[i].append(Tile())
class TestMatchesLeft(unittest.TestCase): tile1 = Tile(4, 5) tile2 = Tile(2, 2) tile3 = Tile(5, 5) def test_matches_left_success(self): """ returns a positive match """ result = compare.matches_left(self.tile1, self.tile3) self.assertEqual(result, True) def test_matches_left_failure(self): """ returns a negative match """ result = compare.matches_left(self.tile1, self.tile2) self.assertEqual(result, False)
class TestIsBlocker(unittest.TestCase): tile = Tile(5, 4) played_tiles_mismatch = [Tile(2, 2), Tile(4, 4)] played_tiles_match = [Tile(4, 2), Tile(2, 5)] def test_is_blocker_success(self): """ returns a positive match """ result = compare.is_blocker(self.tile, self.played_tiles_match) self.assertEqual(result, True) def test_is_blocker_failure(self): """ returns a negative match """ result = compare.is_blocker(self.tile, self.played_tiles_mismatch) self.assertEqual(result, False)
def initialize_tiles(self): tiles = [[Tile(True) for y in range(self.height)] for x in range(self.width)] x, y = 0, 28 for x in range(10, 60): tiles[x][y].blocked = True tiles[x][y].block_sight = True return tiles
def drawSlice(self, layer, surface, bounds, mouse=(-1, -1)): bounds = pygame.Rect(bounds) board = self.board[layer].board tilesX = len(board[0]) tilesY = len(board) border = 3 tborder = 2 bx = (bounds.w - border * 2) / tilesX by = (bounds.h - border * 2) / tilesY tw = bx - tborder th = by - tborder surface.fill(Tile().colors["BOARD_BACK"], bounds) hitTile = [[-1, -1, -1], Tile(), False] # [i, j, tile] for i in range(0, tilesY): for j in range(0, tilesX): x = bounds.x + bx * j + border + tborder y = bounds.y + by * i + border + tborder ti = board[i][j] ti.drawTile(surface, x, y, min(bx, by) - tborder) if x <= mouse[0] <= x + tw and y <= mouse[1] <= y + th: hitTile = [[layer, i, j], ti, True] return hitTile
def __init__(self, map_file): self.music = Music() self.map_file = pytmx.load_pygame(map_file) # IDEA: is needed. Player here or in main class? self.player = Player() self.background_tiles = pygame.sprite.Group() self.collision_tiles = pygame.sprite.Group() self.items = pygame.sprite.Group() self.items.add(Item(3, 32, 32)) self.items.add(Item(1, 300, 320)) self.items.add(Item(0, 64, 64)) self.items.add(Item(0, 400, 400)) # Load tiles for x, y, image in self.map_file.layers[0].tiles(): self.background_tiles.add(Tile(x*self.map_file.tilewidth, y*self.map_file.tileheight, image)) for x, y, image in self.map_file.layers[1].tiles(): self.collision_tiles.add(Tile(x*self.map_file.tilewidth, y*self.map_file.tileheight, image))
class TestFlipped(unittest.TestCase): data = Tile(5, 4) def test_flipped(self): """ Test that the values of the returned tile are flipped """ result = flipped(self.data) self.assertEqual(result.side1, self.data.side2) self.assertEqual(result.side2, self.data.side1) def test_flipped_type(self): """ Test that it returns a tile """ result = flipped(self.data) self.assertIsInstance(result, Tile)
def __init__(self, level_number): self.wall_tiles = pygame.sprite.Group() self.bg_tiles = pygame.sprite.Group() if level_number == 1: for j in range(0, 25): for i in range(0, 25): if level_one[j][i] == 0: self.bg_tiles.add(Tile(i * 32, j * 32, 'bg-2.png')) if level_one[j][i] == 1: self.wall_tiles.add(Tile(i * 32, j * 32, 'bg-3.png')) if level_one[j][i] == 2: self.bg_tiles.add(Tile(i * 32, j * 32, 'grass.png')) if level_number == 2: for j in range(0, 25): for i in range(0, 25): if level_two[j][i] == 0: self.bg_tiles.add(Tile(i * 32, j * 32, 'bg-2.png')) if level_two[j][i] == 1: self.wall_tiles.add(Tile(i * 32, j * 32, 'bg-3.png')) if level_two[j][i] == 2: self.bg_tiles.add(Tile(i * 32, j * 32, 'grass.png'))
def init_tilemap(): consts = Constants.consts tilemap = [[Tile(True) for y in range(consts['MAP_HEIGHT'])] for x in range(consts['MAP_WIDTH'])] return tilemap
def flipped(tile): return Tile(tile.side2, tile.side1)
def test_non_matching_tile(self): """ Returns an unsuccessful match """ result = find.matching_tile(Tile(1, 1), self.data) self.assertEqual(result, False)
def test_does_not_exist(self): """ Returns an unsuccessful match """ result = find.exists(Tile(1, 1), self.tiles) self.assertEqual(result, False)
class TestTiles(unittest.TestCase): data = [Tile(0, 0), Tile(0, 1), Tile(0, 2), Tile(0, 3), Tile(0, 4), Tile(0, 5), Tile(0, 6), Tile(1, 1), Tile(1, 2), Tile(1, 3), Tile(1, 4), Tile(1, 5), Tile(1, 6), Tile(2, 2), Tile(2, 3), Tile(2, 4), Tile(2, 5), Tile(2, 6), Tile(3, 3), Tile(3, 4), Tile(3, 5), Tile(3, 6), Tile(4, 4), Tile(4, 5), Tile(4, 6), Tile(5, 5), Tile(5, 6), Tile(6, 6)] def test_tile_count(self): """ Returns a list of 28 tiles """ result = generate.tiles() self.assertEqual(len(result), 28) def test_tile_combinations(self): """ Returns the correct combinations of tiles """ result = generate.tiles() for i in range(28): self.assertTrue(result[i].eq(self.data[i]))