Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- import pygame, os.path
- LETTERS = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
- class DrawError(BaseException): pass
- def coordify(coord):
- return LETTERS[ coord[0] ] + str(coord[1] + 1)
- def screen_coord(coord):
- return (bdims[0] * coord[0], bdims[0] * abs(8 - coord[1]))
- class Board(pygame.Surface):
- def __init__(self, move = True, from_pos = False, custom_pieces = None):
- pygame.Surface.__init__(self, bdims)
- self.fill((0,0,0))
- self.make_pieces() if not from_pos else self.make_pieces_custom(custom_pieces)
- self.make_spaces()
- self.whites, self.blacks = pygame.sprite.Group(*[s.piece for s in self.spaces[0:16]]), \
- pygame.sprite.Group(*[s.piece for s in self.spaces[48:64]])
- self.move = move # Addition of starting from a position later
- def make_pieces(self):
- self.pieces = [Rook(True, (0,0), 3), Knight(True, (1,0), 1),
- Bishop(True, (2,0), 2), Queen(True, (3,0), 4),
- King(True, (4,0), 5), Bishop(True, (5,0), 2),
- Knight(True, (6,0), 1), Rook(True, (7,0), 3)] + \
- [Pawn(True, (i,1), 0) for i in range(8)] + \
- [None for _ in range(32)] + \
- [Pawn(False, (i, 6), 0) for i in range(8)] + \
- [Rook(False, (0, 7), 3), Knight(False, (1, 7), 1),
- Bishop(False, (2, 7), 2), Queen(False, (3, 7), 4),
- King(False, (4, 7), 1), Bishop(False, (5, 7), 2),
- Knight(False, (6, 7), 1), Rook(False, (7, 7), 3)]
- def occupied_spaces(self, sel = ''):
- if isinstance(sel, str): sel = self.move
- return [ s for s in self.spaces if s.piece and (s.piece.color == sel) ]
- def make_spaces(self):
- self.spaces = []
- for y in xrange(8):
- for x in xrange(8):
- self.spaces.append( Space( (x, y),
- bool((x + y) % 2),
- self.pieces[8 * y + x] )
- )
- self.spaces[-1].rect = self.get_rect()
- self.blit( self.spaces[-1], (x * 75, y * 75) )
- del self.pieces
- def make_pieces_custom(self, c_p):
- pass
- def move_piece(self, other_piece):
- i_from = [s.piece for s in self.spaces].index(other_piece)
- i_to = [s.coord for s in self.spaces].index(other_piece.coord)
- self.spaces[i_from].piece, self.spaces[i_to].piece = None, self.spaces[i_from].piece
- class Space(pygame.Surface):
- def __init__(self, coord, color, piece = None):
- pygame.Surface.__init__(self, (75, 75))
- s_c = color
- color = 127 if color else 255
- self.fill( (color, color, color) )
- self.coord = coord
- self.color = s_c
- self.piece = piece
- self.printable_coord = coordify(coord)
- def draw_piece(self):
- self.blit(self.piece.image, self.piece.rect)
- class Piece(pygame.sprite.Sprite):
- IMG_NAMES = (
- ('wpawn.png', 'bpawn.png'), ('wknight.png', 'bknight.png'),
- ('wbishop.png', 'bbishop.png'), ('wrook.png', 'brook.png'),
- ('wqueen.png', 'bqueen.png'), ('wking.png', 'bking.png')
- )
- def __init__(self, color, coord, i):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load( os.path.join('piece_pics', self.IMG_NAMES[i][0] if color else \
- self.IMG_NAMES[i][1])
- ).convert()
- self.rect = self.image.get_rect()
- self.color = color
- self.coord = coord
- self.alive = True
- self.printable_coord = coordify(coord)
- def move(self, coord):
- taken = self.take(coord)
- taken = taken if taken else False
- self.coord = coord
- self.printable_coord = coordify(coord)
- # print('p_c:', self.printable_coord)
- B.move_piece(self)
- return taken
- def take(self, coord):
- taken = False
- if coord in [ c for c in [ s.coord for s in B.occupied_spaces(not B.move) ] ]:
- # print([space.piece for space in B.occupied_spaces(not B.move) if space.coord == coord][0], 'taken')
- for space in B.occupied_spaces(not B.move):
- if space.coord == coord:
- taken = space.piece
- break
- if taken:
- return repr(self) + taken.destroy()
- def destroy(self):
- self.coord = None
- self.alive = False
- self.kill()
- class Pawn(Piece):
- def __repr__(self):
- return self.printable_coord[0]
- class Knight(Piece):
- def __repr__(self):
- return 'N'
- class Bishop(Piece):
- def __repr__(self):
- return 'B'
- class Rook(Piece):
- def __repr__(self):
- return 'R'
- class Queen(Piece):
- def __repr__(self):
- return 'Q'
- class King(Piece):
- def __repr__(self):
- return 'K'
- def main():
- global B, screen, bdims
- pygame.init()
- bdims = 600, 600
- dims = 800, 800
- screen_bg = 0, 0, 0
- screen = pygame.display.set_mode(dims)
- pygame.display.set_caption('Chess')
- screen.fill(screen_bg)
- B = Board()
- B.convert()
- screen.blit(B, (100, 100))
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- break
- elif event.type == pygame.MOUSEBUTTONDOWN:
- print(event)
- pygame.display.flip()
- if __name__ == '__main__': main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement