예제 #1
0
 def test_get_tile_at_coords_2(self):
     board = Board("""
          2  1  3  4
          5  6  7  8
          9 10 11  .
         13 14 12 15
     """)
     view = BoardView(board, x=0, y=0)
     self.assertEqual(2, view.get_tile_at_coords((0, 0)))
예제 #2
0
 def test_get_tile_at_coords_4(self):
     board = Board("""
          2  1  3  4
          5  6  7  8
          9 10 11  .
         13 14 12 15
     """)
     view = BoardView(board, x=0, y=0)
     view.set_tile_height(10)
     self.assertEqual(5, view.get_tile_at_coords((0, 10)))
예제 #3
0
 def test_get_tile_at_coords_3(self):
     board = Board("""
          2  1  3  4
          5  6  7  8
          9 10 11  .
         13 14 12 15
     """)
     view = BoardView(board, x=0, y=0)
     view.set_tile_width(10)
     self.assertEqual(1, view.get_tile_at_coords((10, 0)))
예제 #4
0
 def test_mouse_motion__highlight_tile_when_mouse_is_over_that_tile(self):
     board = Board()
     TILE_POS = {'row': 0, 'col': 0}
     tile = board.get_tile(**TILE_POS)
     view = BoardView(board)
     controller = BoardController(board, view)
     mouse_position = view.get_screen_coords_of_tile(**TILE_POS)
     
     self.assertFalse(tile.is_highlighted())
     controller.mouse_motion(mouse_position)
     self.assertTrue(tile.is_highlighted())
예제 #5
0
 def test_mouse_motion__remove_highlight_when_mouse_leaves_tile_2(self):
     board = Board()
     TILE_POS_1 = {'row': 0, 'col': 1}
     view = BoardView(board)
     controller = BoardController(board, view)
     mouse_position = view.get_screen_coords_of_tile(**TILE_POS_1)
     
     controller.mouse_motion(mouse_position)
     self.assertTrue(board.get_tile(**TILE_POS_1).is_highlighted())
     
     TILE_POS_2 = {'row': 0, 'col': 2}
     mouse_position = view.get_screen_coords_of_tile(**TILE_POS_2)
     controller.mouse_motion(mouse_position)
     self.assertFalse(board.get_tile(**TILE_POS_1).is_highlighted())
     self.assertTrue(board.get_tile(**TILE_POS_2).is_highlighted())
예제 #6
0
 def test_mouse_click__should_move_tile(self):
     board = Board("""
          1  2  3  4
          5  6  7  8
          9 10 11  .
         13 14 12 15
     """)
     view = BoardView(board)
     controller = BoardController(board, view)
     mouse_position = view.get_screen_coords_of_tile(row=3, col=3)
     
     controller.mouse_click(mouse_position)
     
     expected = Board("""
          1  2  3  4
          5  6  7  8
          9 10 11 15
         13 14 12  .
     """)
     self.assertEqual(str(expected), str(board))
예제 #7
0
 def test_get_screen_coords_of_tile_5(self):
     board = Board()
     board_view = BoardView(board, x=10, y=20)
     board_view.set_tile_width(10)
     board_view.set_tile_height(20)
     coords = board_view.get_screen_coords_of_tile(row=3, col=2)
     self.assertEqual((30, 80), coords)
예제 #8
0
 def run(self):
     clock = pygame.time.Clock()
     pygame.init()
     screen = pygame.display.set_mode(WINSIZE)
     backbuffer = pygame.Surface(screen.get_size(), pygame.SRCALPHA, 32)
     pygame.display.set_caption('Fifteen')
     
     board = self._board
     board.shuffle()
     board_view = BoardView(board, x=80, y=80)
     board_controller = BoardController(board, board_view)
     game_over_msg = GameOverMessage()
     play_again_btn = Button(title='Play Again', x=80, y=340)
     play_again_btn.attach(self)
 
     while True:
         for event in pygame.event.get():
             if event.type == QUIT:
                 pygame.quit()
                 sys.exit()
             elif event.type == MOUSEMOTION:
                 if not board.is_solved():
                     board_controller.mouse_motion(event.pos)
                 else:
                     play_again_btn.mouse_motion(event.pos)
             elif event.type == MOUSEBUTTONDOWN:
                 if not board.is_solved():
                     board_controller.mouse_click(event.pos)
                 else:
                     play_again_btn.mouse_click(event.pos)
         
         backbuffer.fill((0, 0, 0))
         board_view.draw(backbuffer)
         if (board.is_solved()):
             game_over_msg.draw(backbuffer)
             play_again_btn.draw(backbuffer)
         
         screen.blit(backbuffer, (0, 0))
         pygame.display.flip()
         clock.tick(30)
예제 #9
0
 def test_get_tile_at_coords_6(self):
     board = Board("""
          2  1  3  4
          5  6  7  8
          9 10 11  .
         13 14 12 15
     """)
     view = BoardView(board, x=0, y=0)
     view.set_tile_width(10)
     view.set_tile_height(10)
     self.assertEqual(1, view.get_tile_at_coords((11, 0)))
     self.assertEqual(1, view.get_tile_at_coords((19, 0)))
     self.assertEqual(3, view.get_tile_at_coords((20, 0)))
     self.assertEqual(1, view.get_tile_at_coords((19, 9)))
     self.assertEqual(6, view.get_tile_at_coords((19, 10)))
예제 #10
0
 def test_get_tile_at_coords_5(self):
     board = Board("""
          2  1  3  4
          5  6  7  8
          9 10 11  .
         13 14 12 15
     """)
     view = BoardView(board, x=10, y=20)
     view.set_tile_width(10)
     view.set_tile_height(10)
     self.assertEqual(6, view.get_tile_at_coords((20, 30)))
예제 #11
0
 def test_get_tile_at_coords__if_no_tile_with_coords_exist__return_none_4(self):
     board = Board("""
          2  1  3  4
          5  6  7  8
          9 10 11  .
         13 14 12 15
     """)
     view = BoardView(board, x=0, y=10)
     view.set_tile_width(1)
     view.set_tile_height(1)
     self.assertEqual(None, view.get_tile_at_coords((0, 9)))
예제 #12
0
 def test_get_screen_coords_of_tile_1(self):
     board = Board()
     board_view = BoardView(board, x=0, y=0)
     coords = board_view.get_screen_coords_of_tile(row=0, col=0)
     self.assertEqual((0, 0), coords)
예제 #13
0
 def test_get_screen_coords_of_tile_3(self):
     board = Board()
     board_view = BoardView(board, x=0, y=0)
     board_view.set_tile_height(10)
     coords = board_view.get_screen_coords_of_tile(row=1, col=0)
     self.assertEqual((0, 10), coords)
예제 #14
0
 def test_get_tile_at_coords__if_no_tile_with_coords_exist__return_none_5(self):
     board = Board("""
          2  1  3  4
          5  6  7  8
          9 10 11  .
         13 14 12 15
     """)
     view = BoardView(board, x=0, y=0)
     view.set_tile_width(1)
     view.set_tile_height(1)
     self.assertEqual(4, view.get_tile_at_coords((3, 0)))
     self.assertEqual(None, view.get_tile_at_coords((4, 0)))
     self.assertEqual(None, view.get_tile_at_coords((999, 0)))
     self.assertEqual(13, view.get_tile_at_coords((0, 3)))
     self.assertEqual(None, view.get_tile_at_coords((0, 4)))
     self.assertEqual(None, view.get_tile_at_coords((0, 999)))
     self.assertEqual(None, view.get_tile_at_coords((999, 999)))
     self.assertEqual(None, view.get_tile_at_coords((-1, -1)))
예제 #15
0
 def test_get_screen_coords_of_tile_2(self):
     board = Board()
     board_view = BoardView(board, x=0, y=0)
     board_view.set_tile_width(10)
     coords = board_view.get_screen_coords_of_tile(row=0, col=1)
     self.assertEqual((10, 0), coords)