Пример #1
0
 def test_draw(self, draw_rect, draw_line):
     game_screen = GameScreen(GameConfig(), GameModel())
     window = pygame.display.set_mode((1, 1))
     quadratic_grid = QuadraticGrid(4, 4)
     quadratic_grid.draw(window, game_screen)
     self.assertTrue(draw_rect.called)
     self.assertFalse(draw_line.called)
Пример #2
0
 def test_draw_borders(self, draw_rect, draw_line):
     game_screen = GameScreen(GameConfig(), GameModel())
     window = pygame.display.set_mode((1, 1))
     qg = QuadraticGrid(4, 4, border_properties=BorderProperties())
     qg.draw(window, game_screen)
     self.assertTrue(draw_rect.called)
     self.assertTrue(draw_line.called)
Пример #3
0
    def test_draw_borders_woffset(self, draw_rect, draw_line):
        config = GameConfig(window_size=(500, 600))
        game_screen = GameScreen(config, GameModel())
        window = pygame.display.set_mode(config.get_config_val("window_size"))
        border_prop = BorderProperties()
        draw_offset = (100, 0)
        qg = QuadraticGrid(10, 10, draw_offset=draw_offset, border_properties=border_prop)
        qg.draw(window, game_screen)
        self.assertTrue(draw_rect.called)

        block_dim = 50
        # Python sorcery
        self.assertEqual(block_dim, qg._QuadraticGrid__compute_block_dimension(config, "width"))
        self.assertEqual(block_dim, qg._QuadraticGrid__compute_block_dimension(config, "height"))
        window_dimensions = config.get_config_val("window_size")
        # The horizontal borders
        for i in range(10):
            y = i * block_dim + draw_offset[0]
            draw_line.assert_any_call(
                window, border_prop.color, (0, y), (window_dimensions[0], y),
                border_prop.thickness
            )

        # The vertical borders
        for i in range(10):
            x = i * block_dim
            draw_line.assert_any_call(
                window, border_prop.color, (x, draw_offset[0]), (x, window_dimensions[1]),
                border_prop.thickness
            )
class ColorBlocksScreen(GameScreen):
    
    COLOR_MAPPING = (Colors.LUCID_DARK, Colors.HUMAN_RED, Colors.HUMAN_GREEN,
      Colors.HUMAN_BLUE, Colors.LIGHT_GRAY)
    GRID_OFFSET = (100, 0)
    
    def __init__(self, config, grid_size):
        """
        Instantiates a ColorBlocksScreen instance.
        
        @param screen_size
          The dimensions of the screen. An iterable whose first element
          is taken for the width while the second one is taken for the
          height.
        @param grid_size
          The size of the grid, in squares per dimension. First element
          is taken for the width while the second one is taken for the
          height.
        """
        super(ColorBlocksScreen, self).__init__(config, ColorBlocksModel(grid_size[0], grid_size[1], 2))
        screen_size = config.get_config_val("window_size")
        self.game_model = self.model
        # Instantiate an underlying grid model
        self.block_width = int(math.floor((screen_size[0] - ColorBlocksScreen.GRID_OFFSET[1]) / grid_size[0]))
        self.block_height = int(math.floor((screen_size[1] - ColorBlocksScreen.GRID_OFFSET[0]) / grid_size[1]))
        self.grid_model = QuadraticGrid(
            grid_size[0], grid_size[1],
            draw_offset=ColorBlocksScreen.GRID_OFFSET,
            border_properties=BorderProperties()
        )
        self.rect_list = []
        self.color_list = []
    
    def setup(self):
        self.represent_tiles()
    
    def represent_tiles(self):
        """
        Scans the game model and then lists their assigned colors.
        """
        # rect_list and color_list are associative arrays.
        # for the rect described in rect_list, its color is
        # in color_list.
        self.rect_list, self.color_list = QuadraticGrid.cons_rect_list(
          self.grid_model, self.model, self.block_width, self.block_height,
          offset=ColorBlocksScreen.GRID_OFFSET
        )
    
    def draw_screen(self, window):
        self.grid_model.draw(window, self)
        score_font = pygame.font.Font(None, 25)
        score = score_font.render("Score: " + str(self.game_model.score), True, Colors.HUMAN_RED)
        window.blit(score, [10, 10])
Пример #5
0
    def test_draw_borders_wparams(self, draw_rect, draw_line):
        config = GameConfig(window_size=(400, 400))
        game_screen = GameScreen(config, GameModel())
        window = pygame.display.set_mode(config.get_config_val("window_size"))
        border_prop = BorderProperties()
        qg = QuadraticGrid(2, 2, border_properties=border_prop)
        qg.draw(window, game_screen)
        self.assertTrue(draw_rect.called)

        # The vertical borders
        draw_line.assert_any_call(
            window, border_prop.color, (0, 0), (0, 400),
            border_prop.thickness
        )
        draw_line.assert_any_call(
            window, border_prop.color, (200, 0), (200, 400),
            border_prop.thickness
        )
        draw_line.assert_any_call(
            window, border_prop.color, (400, 0), (400, 400),
            border_prop.thickness
        )

        # The horizontal borders
        draw_line.assert_any_call(
            window, border_prop.color, (0, 0), (400, 0),
            border_prop.thickness
        )
        draw_line.assert_any_call(
            window, border_prop.color, (0, 200), (400, 200),
            border_prop.thickness
        )
        draw_line.assert_any_call(
            window, border_prop.color, (0, 400), (400, 400),
            border_prop.thickness
        )