Advertisement
Guest User

chess

a guest
Mar 16th, 2015
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.80 KB | None | 0 0
  1. from __future__ import print_function
  2. import pygame, os.path
  3.  
  4. LETTERS = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
  5.  
  6. class DrawError(BaseException): pass
  7.  
  8. def coordify(coord):
  9.     return LETTERS[ coord[0] ] + str(coord[1] + 1)
  10.  
  11. def screen_coord(coord):
  12.     return (bdims[0] * coord[0], bdims[0] * abs(8 - coord[1]))
  13.    
  14.  
  15. class Board(pygame.Surface):
  16.     def __init__(self, move = True, from_pos = False, custom_pieces = None):
  17.         pygame.Surface.__init__(self, bdims)
  18.         self.fill((0,0,0))
  19.         self.make_pieces() if not from_pos else self.make_pieces_custom(custom_pieces)
  20.         self.make_spaces()
  21.         self.whites, self.blacks = pygame.sprite.Group(*[s.piece for s in self.spaces[0:16]]), \
  22.                                    pygame.sprite.Group(*[s.piece for s in self.spaces[48:64]])
  23.        
  24.         self.move = move    # Addition of starting from a position later
  25.        
  26.     def make_pieces(self):
  27.         self.pieces = [Rook(True, (0,0), 3), Knight(True, (1,0), 1),
  28.                        Bishop(True, (2,0), 2), Queen(True, (3,0), 4),
  29.                        King(True, (4,0), 5), Bishop(True, (5,0), 2),
  30.                        Knight(True, (6,0), 1), Rook(True, (7,0), 3)] + \
  31.                       [Pawn(True, (i,1), 0) for i in range(8)] + \
  32.                       [None for _ in range(32)] + \
  33.                       [Pawn(False, (i, 6), 0) for i in range(8)] + \
  34.                       [Rook(False, (0, 7), 3), Knight(False, (1, 7), 1),
  35.                        Bishop(False, (2, 7), 2), Queen(False, (3, 7), 4),
  36.                        King(False, (4, 7), 1), Bishop(False, (5, 7), 2),
  37.                        Knight(False, (6, 7), 1), Rook(False, (7, 7), 3)]
  38.  
  39.     def occupied_spaces(self, sel = ''):
  40.         if isinstance(sel, str): sel = self.move
  41.         return [ s for s in self.spaces if s.piece and (s.piece.color == sel) ]
  42.  
  43.     def make_spaces(self):
  44.         self.spaces = []
  45.         for y in xrange(8):
  46.             for x in xrange(8):
  47.                 self.spaces.append( Space( (x, y),
  48.                                            bool((x + y) % 2),
  49.                                            self.pieces[8 * y + x] )
  50.                                   )
  51.                 self.spaces[-1].rect = self.get_rect()
  52.                 self.blit( self.spaces[-1], (x * 75, y * 75) )
  53.         del self.pieces
  54.  
  55.     def make_pieces_custom(self, c_p):
  56.         pass
  57.  
  58.     def move_piece(self, other_piece):
  59.        
  60.         i_from = [s.piece for s in self.spaces].index(other_piece)
  61.         i_to = [s.coord for s in self.spaces].index(other_piece.coord)
  62.        
  63.         self.spaces[i_from].piece, self.spaces[i_to].piece = None, self.spaces[i_from].piece
  64.        
  65. class Space(pygame.Surface):
  66.     def __init__(self, coord, color, piece = None):
  67.         pygame.Surface.__init__(self, (75, 75))
  68.         s_c = color
  69.         color = 127 if color else 255
  70.         self.fill( (color, color, color) )
  71.         self.coord = coord
  72.         self.color = s_c
  73.         self.piece = piece
  74.         self.printable_coord = coordify(coord)
  75.  
  76.     def draw_piece(self):
  77.         self.blit(self.piece.image, self.piece.rect)
  78.  
  79. class Piece(pygame.sprite.Sprite):
  80.  
  81.     IMG_NAMES = (
  82.         ('wpawn.png', 'bpawn.png'),     ('wknight.png', 'bknight.png'),
  83.         ('wbishop.png', 'bbishop.png'), ('wrook.png', 'brook.png'),
  84.         ('wqueen.png', 'bqueen.png'),   ('wking.png', 'bking.png')
  85.         )
  86.    
  87.     def __init__(self, color, coord, i):
  88.         pygame.sprite.Sprite.__init__(self)
  89.         self.image = pygame.image.load( os.path.join('piece_pics', self.IMG_NAMES[i][0] if color else \
  90.                                                                    self.IMG_NAMES[i][1])
  91.                                       ).convert()
  92.         self.rect = self.image.get_rect()
  93.         self.color = color
  94.         self.coord = coord
  95.         self.alive = True
  96.         self.printable_coord = coordify(coord)
  97.  
  98.     def move(self, coord):
  99.         taken = self.take(coord)
  100.         taken = taken if taken else False
  101.         self.coord = coord
  102.         self.printable_coord = coordify(coord)
  103. #        print('p_c:', self.printable_coord)
  104.         B.move_piece(self)
  105.         return taken
  106.  
  107.     def take(self, coord):
  108.         taken = False
  109.         if coord in [ c for c in [ s.coord for s in B.occupied_spaces(not B.move) ] ]:
  110. #            print([space.piece for space in B.occupied_spaces(not B.move) if space.coord == coord][0], 'taken')
  111.             for space in B.occupied_spaces(not B.move):
  112.                 if space.coord == coord:
  113.                     taken = space.piece
  114.                     break
  115.             if taken:
  116.                 return repr(self) + taken.destroy()
  117.    
  118.     def destroy(self):
  119.         self.coord = None
  120.         self.alive = False
  121.         self.kill()
  122.  
  123. class Pawn(Piece):
  124.     def __repr__(self):
  125.         return self.printable_coord[0]
  126.  
  127. class Knight(Piece):
  128.     def __repr__(self):
  129.         return 'N'
  130.  
  131. class Bishop(Piece):
  132.     def __repr__(self):
  133.         return 'B'
  134.  
  135. class Rook(Piece):    
  136.     def __repr__(self):
  137.         return 'R'
  138.  
  139. class Queen(Piece):
  140.     def __repr__(self):
  141.         return 'Q'
  142.  
  143. class King(Piece):
  144.     def __repr__(self):
  145.         return 'K'
  146.  
  147. def main():
  148.     global B, screen, bdims
  149.     pygame.init()
  150.     bdims = 600, 600
  151.     dims = 800, 800
  152.     screen_bg = 0, 0, 0
  153.     screen = pygame.display.set_mode(dims)
  154.     pygame.display.set_caption('Chess')
  155.     screen.fill(screen_bg)
  156.     B = Board()
  157.     B.convert()
  158.     screen.blit(B, (100, 100))
  159.    
  160.     while True:
  161.  
  162.         for event in pygame.event.get():
  163.             if event.type == pygame.QUIT:
  164.                 pygame.quit()
  165.                 break
  166.             elif event.type == pygame.MOUSEBUTTONDOWN:
  167.                 print(event)
  168.         pygame.display.flip()
  169.    
  170.    
  171. if __name__ == '__main__': main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement