Exemplo n.º 1
0
 def __init__(self, x, y, width, height):
     self.x = x
     self.y = y
     self.width = width
     self.height = height
     self.screen = None
     self.core = ChessBoard()
     white_field = pygame.Surface((width / self.core.width, height / self.core.height))
     white_field.fill((255, 255, 255))
     self.possibilities_image = white_field.copy()
     self.possibilities_image.fill((0, 255, 0))
     self.possibilities_image.set_alpha(80)
     self.pieces = {}
     self.selected_piece = None
     self.image = pygame.Surface((width, height))
     for x in range(self.core.width):
         for y in range(self.core.height):
             field = self.core.get_field(x, y)
             if field.color == "white":
                 self.image.blit(white_field, (x * width / self.core.width, y * height / self.core.height))
             if field.piece:
                 name = "_".join([field.piece.__class__.__name__.lower(), field.piece.color])
                 self.pieces[name] = Piece(field.piece)
     self.normal = self.image.copy()
     self.update_pieces()
Exemplo n.º 2
0
class Board(Widget):

    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.screen = None
        self.core = ChessBoard()
        white_field = pygame.Surface((width / self.core.width, height / self.core.height))
        white_field.fill((255, 255, 255))
        self.possibilities_image = white_field.copy()
        self.possibilities_image.fill((0, 255, 0))
        self.possibilities_image.set_alpha(80)
        self.pieces = {}
        self.selected_piece = None
        self.image = pygame.Surface((width, height))
        for x in range(self.core.width):
            for y in range(self.core.height):
                field = self.core.get_field(x, y)
                if field.color == "white":
                    self.image.blit(white_field, (x * width / self.core.width, y * height / self.core.height))
                if field.piece:
                    name = "_".join([field.piece.__class__.__name__.lower(), field.piece.color])
                    self.pieces[name] = Piece(field.piece)
        self.normal = self.image.copy()
        self.update_pieces()

    def start(self, screen):
        self.screen = screen
        self.handle_event = self._handle_event

    def _handle_event(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                field_width = self.width / self.core.width
                field_height = self.height / self.core.height
                x, y = event.pos
                x = (x - self.x + self.width / 2) / field_width
                y = (y - self.y + self.height / 2) / field_height
                if self.selected_piece:
                    self.put_piece(x, y)
                else:
                    self.get_piece(x, y, field_width, field_height)

    def get_piece(self, x, y, width, height):
        core_piece = self.core.get_piece(x, y)
        if core_piece:
            name = "_".join([core_piece.__class__.__name__.lower(), core_piece.color])
            piece = self.pieces[name]
            piece.core.x = x
            piece.core.y = y
            self.selected_piece = piece
            self.screen.window.cursor_image = piece.get_image(width, height)
            self.update_pieces()

    def put_piece(self, x, y):
        if self.core.put_piece(self.selected_piece.core, x, y):
            self.selected_piece = None
            self.screen.window.cursor_image = None
            self.update_pieces()

    def update_pieces(self):
        field_width = self.width / self.core.width
        field_height = self.height / self.core.height
        self.normal = self.image.copy()
        self.put_possibles()
        for field_x in range(self.core.width):
            for field_y in range(self.core.height):
                field = self.core.get_field(field_x, field_y)
                x = field_x * field_width
                y = field_y * field_height
                if field.piece:
                    name = "_".join([field.piece.__class__.__name__.lower(), field.piece.color])
                    piece = self.pieces[name]
                    self.normal.blit(piece.get_image(field_width, field_height), (x, y))
        self.mouse_over = self.normal.copy()

    def put_possibles(self):
        if self.selected_piece:
            field_width = self.width / self.core.width
            field_height = self.height / self.core.height
            for x, y in self.selected_piece.core.define_possibilities(self.core.board):
                self.normal.blit(self.possibilities_image, (x * self.width / self.core.width, y * self.height / self.core.height))