def test_group_a_group_and_a_circle(self):
    rectangle1 = Rectangle(-2, -1, 4, 1)
    rectangle2 = Rectangle(-1, 2, 4, 2)
    
    square1 = Square(7, 3, 2)
    square2 = Square(10, 3, 2)
    
    circle1 = Circle(2, 6, 3)

    self.diagram.add_shape(rectangle1)
    self.diagram.add_shape(rectangle2)
    
    self.diagram.add_shape(square1)
    self.diagram.add_shape(square2)
    
    self.diagram.add_shape(circle1)
    
    self.diagram.group_shapes(-1, 1, 8, 6)
    
    self.diagram.group_shapes(9, 3, 8, 4)
    
    self.diagram.group_shapes(3, 6, 20, 20)
    
    self.assertEqual(circle1, self.diagram.shapes[0].shapes[0])
    self.assertEqual([rectangle1, rectangle2], self.diagram.shapes[0].shapes[1].shapes)
    self.assertEqual([square1, square2], self.diagram.shapes[0].shapes[2].shapes)
Beispiel #2
0
    def __init__(self, size=4):
        self.size = size
        self.score = 0
        self.grid = [[Square(0)] * size for _ in range(size)]
        tab = [Square(0)] * size * size
        self.d = Display(size)
        self.add_number()

        while not lose(self.grid):
            moved = False
            while not moved:
                self.d.print_grid(self.grid)
                self.d.print_score(self.score)
                self.dir = self.d.newDirection()
                if self.dir == 0:
                    self.d.quit()
                    return
                moved = self.move_number()
            self.add_number()
            for i in range(size):
                for j in range(size):
                    tab[i + size * j] = self.grid[i][j]
            if win(tab):
                self.d.print_text("Tu as gagné !!!!!")
                print("win")
        print("Lose")
        self.d.print_text("Tu as perdu !!!!!")
        self.d.quit()
Beispiel #3
0
    def __init__(self, game, screen, x, y):

        self.game = game
        self.screen = screen
        self.x = x
        self.y = y

        self.border_rect = pygame.rect.Rect(
            self.x - Settings.field_border_width,
            self.y - Settings.field_border_width,
            Settings.field_width + 2 * Settings.field_border_width,
            Settings.field_height + 2 * Settings.field_border_width)

        left_edge = Square(self.x - Settings.field_height,
                           self.y,
                           size=Settings.field_height)
        right_edge = Square(self.x +
                            Settings.field_cells_x * Settings.field_cells_size,
                            self.y,
                            size=Settings.field_height)
        self.edges = Group(left_edge, right_edge)

        self.falling_figure = None
        self.next_figure = Figure(self)
        self.fallen = Fallen(self.screen, self.x, self.y)
        self.landed = 0

        self.barriers = [self.edges, self.fallen.group]

        self.timer = pygame.time.Clock()
        self.passed_time = 0

        self.pause = False
Beispiel #4
0
 def main(self) -> None:
     self.squares = []
     self.squares.append(
         Square(Vector(self.width / 2, self.height / 2),
                max(WIDTH, HEIGHT) / 2))
     # Main game loop
     while self.running:
         self.update_screen()
Beispiel #5
0
  def draw(self, square = None):
    sq_pad = 8
    font_size = 14

    fen_model = fromFEN(START_BOARD)

    if(len(self.squares) == 0):
      for r in range(0, len(fen_model)):
        rank = fen_model[r]
        row = []
        for s in range(0, len(rank)):
          sq = rank[s]
          _x = sq['_x']
          _y = sq['_y']
          tx = self.sq_size*_x
          ty = self.sq_size*_y
          toggle_color = is_even(_x+1) ^ is_even(_y+1)

          sq_piece = None
          if(sq['piece']):
            sq_piece = Piece({
              '_x' : _x,
              '_y' : _y,
              'x': tx, 
              'y': ty,
              'size': self.sq_size - (sq_pad*2),
              'role': sq['piece']['role'],
              'color': sq['piece']['color'],
              'path': []
            })
          square = Square({
            'size': self.sq_size,
            '_x' : _x,
            '_y' : _y,
            'x' : tx, 
            'y' : ty,
            'pad': sq_pad, 
            'font_size': font_size, 
            'piece': sq_piece,
            'color': DARK if toggle_color else LIGHT,
            'text_color': LIGHT if toggle_color else DARK,
            'label': str(chr(73-(_y+1))) + str(_x+1),
            'file': str(_x+1) if _y == 7 else None,
            'rank': str(chr(73-(_y+1))) if _x == 0 else None,
            'settings': { 
              'draw_coords': False,
              'draw_rankfile': True
            }
          })
          square.draw()
          row.append(square)
        self.squares.append(row)
        row_blits = list(map(lambda s: (s.surface, (s.x, s.y)), row))
        self.surface.blits(row_blits)

    return self.surface
  def test_diagram_has_a_group_and_two_ungrouped_shapes(self):
    rectangle1 = Rectangle(-1, 7, 8, 4)
    circle1 = Circle(3, 6, 3)
    square1 = Square(-5, 0, 8)

    circle2 = Circle(9, 6, 3)
    square2 = Square(8, 0, 8)

    self.diagram.add_shape(rectangle1)
    self.diagram.add_shape(circle1)
    self.diagram.add_shape(square1)

    self.diagram.add_shape(circle2)
    self.diagram.add_shape(square2)
    
    self.diagram.group_shapes(0, 0, 21, 20)
    
    self.assertEqual([rectangle1, circle1, square1], self.diagram.shapes[2].shapes)
    self.assertEqual([circle2, square2], self.diagram.shapes[0:2])
 def test_add_circle_square_rectangle_to_diagram(self):
   circle1 = Circle(0, 0, 3)
   rectangle1 = Rectangle(0, 0, 4, 2)
   square1 = Square(0, 0, 4)
   
   self.diagram.add_shape(circle1)
   self.diagram.add_shape(rectangle1)
   self.diagram.add_shape(square1)
   
   self.assertEqual([circle1, rectangle1, square1], self.diagram.shapes)
Beispiel #8
0
    def reload_sprites(self):

        self.group.empty()
        for i in range(self.size):
            for k in range(self.size):
                cell_type = self.template[self.rotate_status][i][k]
                if cell_type:
                    self.group.add(
                        Square(self.x + k * Settings.field_cells_size,
                               self.y + i * Settings.field_cells_size,
                               self.color,
                               star=(cell_type == 2)))
  def test_draw_a_rectangle_area_any_shape_within_it_will_be_grouped(self):
    rectangle1 = Rectangle(-1, 7, 8, 4)
    circle1 = Circle(0, 0, 3)
    square1 = Square(-5, 0, 8)

    self.diagram.add_shape(rectangle1)
    self.diagram.add_shape(circle1)
    self.diagram.add_shape(square1)
    
    self.diagram.group_shapes(0, 0, 25, 25)
  
    self.assertEqual([rectangle1, circle1, square1], self.diagram.shapes[0].shapes)
Beispiel #10
0
 def generateBoard(self):
     logging.info("Generating board")
     self.squares = []
     for id_y, y in enumerate(range(0, self.height, self.sizeY)):
         line = []
         for id_x, x in enumerate(range(0, self.width, self.sizeX)):
             square = Square(self.screen, id_x, id_y, x, y, self.sizeX, self.sizeY, self.colour__default)
             line.append(square)
             square.update()
         self.squares.append(line)
     pygame.display.update()
     logging.info("Board generated")
     return self.squares
 def test_diagram_has_only_a_group_no_ungrouped_shapes(self):
   rectangle1 = Rectangle(-1, 7, 8, 4)
   circle1 = Circle(3, 6, 3)
   square1 = Square(-5, 0, 8)
   
   self.diagram.add_shape(rectangle1)
   self.diagram.add_shape(circle1)
   self.diagram.add_shape(square1)
   
   self.diagram.group_shapes(0, 0, 21, 20)
   
   self.assertEqual([rectangle1, circle1, square1], self.diagram.shapes[0].shapes)
   self.assertEqual(1, len(self.diagram.shapes))
  def test_delete_group_and_its_shapes(self):
    rectangle1 = Rectangle(-1, 7, 8, 4)
    circle1 = Circle(20, 6, 3)
    square1 = Square(-5, 0, 8)

    self.diagram.add_shape(rectangle1)
    self.diagram.add_shape(circle1)
    self.diagram.add_shape(square1)

    self.diagram.group_shapes(0, 0, 21, 20)
    
    self.diagram.delete_shape_at(3, 6)
    
    self.assertEqual([circle1], self.diagram.shapes)
  def test_ungroup_a_group(self):
    rectangle1 = Rectangle(-1, 7, 8, 4)
    circle1 = Circle(20, 2, 3)
    square1 = Square(-5, 0, 8)

    self.diagram.add_shape(rectangle1)
    self.diagram.add_shape(circle1)
    self.diagram.add_shape(square1)

    self.diagram.group_shapes(0, 0, 21, 20)
    
    self.diagram.ungroup(self.diagram.shapes[1])

    self.assertEqual([circle1, rectangle1, square1], self.diagram.shapes)
Beispiel #14
0
    def init_matrix(self):

        for i in range(Settings.field_cells_y):
            self.matrix.append(list())
            for k in range(Settings.field_cells_x):
                self.matrix[-1].append(None)

        row = []
        for i in range(Settings.field_cells_x):
            row.append(
                Square(self.x + Settings.field_cells_size * i,
                       self.y + Settings.field_height,
                       empty=True))
        self.matrix.append(row)
Beispiel #15
0
    def __init__(self, size=4):
        self.size = size
        self.grid = [[Square(0)] * size for _ in range(size)]
        tab = [Square(0)] * size * size
        self.d = Display(size)
        self.add_number()

        while not lose(self.grid):
            self.d.print_grid(self.grid)
            moved = False
            while not moved:
                self.dir = new_direction()
                if self.dir == 0:
                    quit()
                    return
                moved = self.move_number()
                self.add_number()
            for i in range(self.size):
                for j in range(self.size):
                    tab[i + self.size * j] = self.grid[i][j]
            if win(tab):
                print("win")
        print("Lose")
        quit()
Beispiel #16
0
 def add_number(self):
     count = 0
     for i in range(self.size):
         for j in range(self.size):
             if self.grid[i][j] == 0:
                 count += 1
     if not count:
         print("Lose")
         quit()
     number = randrange(count) + 1
     for i in range(self.size):
         for j in range(self.size):
             if self.grid[i][j] == 0:
                 number -= 1
                 if number == 0:
                     self.grid[i][j] = Square(new_number())
Beispiel #17
0
 def setUp(self):
     self.square = Square(0, 0, 4)
	def create_shape(self):
		self.diagram.add_shape(Square(125, 125, 50))
		self.canvas.create_rectangle(100, 100, 150, 150, outline='black', fill='white', tags="shape")
Beispiel #19
0
 def move_number(self):
     has_moved = False
     if self.dir % 2 == 0:
         for i in range(1 if self.dir > 0 else self.size - 2,
                        self.size if self.dir > 0 else -1, self.dir // 2):
             for j in range(self.size):
                 if self.grid[i][j] != 0:
                     k = i - self.dir // 2
                     move = True
                     while (k >= 0
                            if self.dir > 0 else k < self.size) and move:
                         if self.grid[k][j] != 0:
                             if self.grid[k][j].fusion or self.grid[i][
                                     j].fusion or not is_fusion(
                                         self.grid[k][j].value,
                                         self.grid[i][j].value):
                                 if k + self.dir // 2 != i:
                                     has_moved = True
                                     n = self.grid[i][j]
                                     self.grid[i][j] = Square(0)
                                     self.grid[k + self.dir // 2][j] = n
                             else:
                                 has_moved = True
                                 self.grid[k][j] = Square(
                                     fusion(self.grid[i][j].value))
                                 self.grid[k][j].fusion = True
                                 self.grid[i][j] = Square(0)
                             move = False
                         elif k == 0 or k == self.size - 1:
                             has_moved = True
                             self.grid[k][j] = self.grid[i][j]
                             self.grid[i][j] = Square(0)
                             move = False
                         k -= self.dir // 2
     else:
         for j in range(1 if self.dir > 0 else self.size - 2,
                        self.size if self.dir > 0 else -1, self.dir):
             for i in range(self.size):
                 if self.grid[i][j] != 0:
                     k = j - self.dir
                     move = True
                     while (k >= 0
                            if self.dir > 0 else k < self.size) and move:
                         if self.grid[i][k] != 0:
                             if self.grid[i][k].fusion or self.grid[i][
                                     j].fusion or not is_fusion(
                                         self.grid[i][k].value,
                                         self.grid[i][j].value):
                                 if k + self.dir != j:
                                     has_moved = True
                                     n = self.grid[i][j]
                                     self.grid[i][j] = Square(0)
                                     self.grid[i][k + self.dir] = n
                             else:
                                 has_moved = True
                                 self.grid[i][k] = Square(
                                     fusion(self.grid[i][j].value))
                                 self.grid[i][k].fusion = True
                                 self.grid[i][j] = Square(0)
                             move = False
                         elif k == 0 or k == self.size - 1:
                             has_moved = True
                             self.grid[i][k] = self.grid[i][j]
                             self.grid[i][j] = Square(0)
                             move = False
                         k -= self.dir
     for i in range(self.size):
         for j in range(self.size):
             self.grid[i][j].fusion = False
     return has_moved