#!/usr/bin/python """ __version__ = "$Revision: 1.26 $" __date__ = "$Date: 2005/04/19 18:12:43 $" """ from PythonCard import graphic,model import boardwindow import filteredboard, board class MyServer: def __init__(self): self.host = '127.0.0.1' def update(self, board): self.currentBoard = board def get(self): return self.currentBoard server = MyServer() CELLWIDTH = CELLHEIGHT = 60 # Bias is the offset of the difference between the cellwidth # and the size of the graphic, so that it is centered BIAS = 7 IMAGEDIR = '../pieces/plain/' def translateCoordinates(position): col,row = position col = col / CELLWIDTH row = row / CELLHEIGHT row = BIAS - row return col,row def upperLeftOfSquare((column,row)): col = column * CELLWIDTH r = CELLHEIGHT * (BIAS - row) return col, r class MyBackground(model.Background): def on_initialize(self, event): self.setup(board.GameState(), board.WHITE) self.secondaryboard = model.childWindow(self, boardwindow.MyBackground) self.boardViews = [self, self.secondaryboard] def setup(self, model, color): self.model = model self.state = filteredboard.PlayerView(self.model, color) self.state.setup(); ## self.state.dump() self.loadGraphics() self.viewpoint = color self.startCoordinates = None self.drawBoard() def drawBoards(self): for b in self.boardViews: b.drawBoard() def drawBoard(self): b = self.components.Board b.autoRefresh = False self.drawBoardBackground() self.drawBoardPieces() b.autoRefresh = True b.refresh() def drawBoardPieces(self): state = self.state print "Clearing cache for", self.viewpoint state.cachedMask = None for position in board.positions(): piece = state.pieceAt(position) if piece: if (piece.code() == '?'): self.drawFog(position) else: image = self.imageMap[piece.code()] self.drawPiece(image, position) def drawPiece(self, image, position): col,row = upperLeftOfSquare(position) col += BIAS row += BIAS self.components.Board.drawBitmap(image, (col,row)) def drawFog(self, position): column,row = position if ((column + row) & 1): image = self.imageMap['WhiteFog'] else: image = self.imageMap['BlackFog'] self.components.Board.drawBitmap(image, upperLeftOfSquare(position)) def loadGraphics(self): pieceCode = {'p':'Pawn', 'r':'Rook', 'q':'Queen', 'b':'Bishop', 'k':'King', 'n':'Knight'} self.imageMap ={} for k,v in pieceCode.items() : self.imageMap[k.lower()] = graphic.Bitmap(IMAGEDIR + 'Black' + v + '.png') self.imageMap[k.upper()] = graphic.Bitmap(IMAGEDIR + 'White' + v + '.png') self.imageMap['WhiteFog'] = graphic.Bitmap(IMAGEDIR + 'WhiteFog.png') self.imageMap['BlackFog'] = graphic.Bitmap(IMAGEDIR + 'BlackFog.png') def drawBoardBackground(self): b = self.components.Board b.clear() state = self.state b.size = CELLWIDTH * 8, CELLHEIGHT * 8 b.fillColor = "black" for position in board.positions(): column,row = position if ((column+row) & 1): b.drawRectangle((column*CELLWIDTH, row*CELLHEIGHT), (CELLWIDTH, CELLHEIGHT)) def on_Board_mouseDown(self, event): # print event.position, translateCoordinates(event.position) self.startCoordinates = translateCoordinates(event.position) def on_Board_mouseDrag(self, event): # workaround focus problem on Mac when switching between views if not self.startCoordinates: self.startCoordinates = self.startCoordinates = translateCoordinates(event.position) # print event.position, translateCoordinates(event.position) def on_Board_mouseUp(self, event): endCoordinates = translateCoordinates(event.position) print event.position, translateCoordinates(event.position), "start was:", self.startCoordinates state = self.state p = state.pieceAt(self.startCoordinates) if (p): if ((endCoordinates in p.moves()) & (p.color == self.state.nextMove)): state.movePiece(self.startCoordinates, endCoordinates) self.drawBoards() self.startCoordinates = None if __name__ == '__main__': app = model.Application(MyBackground) app.MainLoop()