def page(web): try: location.append(Location(web.args['name'], web.args['lat'], web.args['lon'])) Location.flush() except Exception as e: dump_exception('WEB error:', e) yield web.index
def page(web): try: name = web.args['name'] for i in range(len(location)): if location[i].name == name: location.remove(location[i]) Location.flush() break except Exception as e: dump_exception('WEB error:', e) yield web.index
def positionToLocation(self, position: Position) -> Location: x, y = position r = -1 c = -1 for row in range(8): square = self.getSquare(Location(row, 0)) if square.pos.y <= y <= square.pos.y + SQUARE: r = row break for col in range(8): square = self.getSquare(Location(0, col)) if square.pos.x <= x <= square.pos.x + SQUARE: c = col break return Location(r, c)
def main(): createBoardPieces() board.getMoves(Location(0, 7)) print(board.positionToLocation(Position(100, 100))) clock = pygame.time.Clock() run = True while run: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False elif event.type == pygame.MOUSEBUTTONUP: print("here") board.handleClick() else: pass #update board.getMousePosition() sprites.update() #draw WIN.fill(BACKGROUND) board.drawBoard() sprites.draw(WIN) pygame.display.flip() pygame.quit()
def drawBoard(self): # print("drawing board") if self.grid != []: for row in range(8): for col in range(8): square = self.getSquare(Location(row, col)) square.update() else: widthOffset = (WIDTH - SQUARE * 8) / 2 heightOffset = (HEIGHT - SQUARE * 8) / 2 isWhite = False for row in range(8): temp = [] for col in range(8): pos = Position( widthOffset + SQUARE * col, heightOffset + SQUARE * row, ) color = "WHITE" if isWhite else "GREEN" temp.append(Square(color=color, position=pos)) isWhite = not isWhite self.grid.append(temp) isWhite = not isWhite del temp
def createBoardPieces(): c1, c2 = "BLACK", "WHITE" if BOARD_INVERTED: c1, c2 = c2, c1 board.addPiece(location=Location(0, 0), color=c1, name="ROOK") board.addPiece(location=Location(0, 1), color=c1, name="KNIGHT") board.addPiece(location=Location(0, 2), color=c1, name="BISHOP") board.addPiece(location=Location(0, 3), color=c1, name="KING") board.addPiece(location=Location(0, 4), color=c1, name="QUEEN") board.addPiece(location=Location(0, 5), color=c1, name="BISHOP") board.addPiece(location=Location(0, 6), color=c1, name="KNIGHT") board.addPiece(location=Location(0, 7), color=c1, name="ROOK") for i in range(8): board.addPiece(location=Location(1, i), color=c1, name="PAWN") board.addPiece(location=Location(7, 0), color=c2, name="ROOK") board.addPiece(location=Location(7, 1), color=c2, name="KNIGHT") board.addPiece(location=Location(7, 2), color=c2, name="BISHOP") board.addPiece(location=Location(7, 3), color=c2, name="KING") board.addPiece(location=Location(7, 4), color=c2, name="QUEEN") board.addPiece(location=Location(7, 5), color=c2, name="BISHOP") board.addPiece(location=Location(7, 6), color=c2, name="KNIGHT") board.addPiece(location=Location(7, 7), color=c2, name="ROOK") for i in range(8): board.addPiece(location=Location(6, i), color=c2, name="PAWN")
def clearIndicators(self): for move in self.moves: square = self.getSquare(move) square.indicator.kill() self.moves = set() self.indicatorOrigin = Location(-1, -1)
def getMoves(self, location: Location): self.indicatorOrigin = location piece = self.getSquare(location).piece def checkForPiece(loc: Location) -> bool: for i in loc: if not 0 <= i <= 7: return False square = self.getSquare(loc) if not square.piece: self.moves.add(loc) return False else: if square.piece.color != piece.color: self.moves.add(loc) return True self.moves = set() row, col = location if piece.name == "ROOK": for i in range(row - 1, 0): if checkForPiece(Location(i, col)): break for i in range(row + 1, 8): if checkForPiece(Location(i, col)): break for i in range(col - 1, 0): if checkForPiece(Location(row, i)): break for i in range(col + 1, 8): if checkForPiece(Location(row, i)): break elif piece.name == "KNIGHT": checkForPiece(Location(row + 2, col - 1)) checkForPiece(Location(row + 2, col + 1)) checkForPiece(Location(row - 2, col - 1)) checkForPiece(Location(row - 2, col + 1)) checkForPiece(Location(row - 1, col + 2)) checkForPiece(Location(row + 1, col + 2)) checkForPiece(Location(row - 1, col - 2)) checkForPiece(Location(row + 1, col - 2)) elif piece.name == "BISHOP": for i in range(8): checkForPiece(Location(row + i, col + i)) checkForPiece(Location(row - i, col + i)) checkForPiece(Location(row + i, col - i)) checkForPiece(Location(row - i, col - i)) elif piece.name == "QUEEN": for i in range(8): checkForPiece(Location(row + i, col + i)) checkForPiece(Location(row - i, col + i)) checkForPiece(Location(row + i, col - i)) checkForPiece(Location(row - i, col - i)) for i in range(row - 1, 0): if checkForPiece(Location(i, col)): break for i in range(row + 1, 8): if checkForPiece(Location(i, col)): break for i in range(col - 1, 0): if checkForPiece(Location(row, i)): break for i in range(col + 1, 8): if checkForPiece(Location(row, i)): break elif piece.name == "KING": checkForPiece(Location(row + 1, col + 1)) checkForPiece(Location(row + 1, col - 1)) checkForPiece(Location(row - 1, col + 1)) checkForPiece(Location(row - 1, col - 1)) checkForPiece(Location(row + 1, col)) checkForPiece(Location(row - 1, col)) checkForPiece(Location(row, col + 1)) checkForPiece(Location(row, col - 1)) elif piece.name == "PAWN": if piece.color == "BLACK": checkForPiece(Location(row + 1, col)) if self.getSquare(Location(row + 1, col + 1)).piece: self.moves.add(Location(row + 1, col + 1)) if self.getSquare(Location(row + 1, col - 1)).piece: self.moves.add(Location(row + 1, col - 1)) else: checkForPiece(Location(row - 1, col)) if self.getSquare(Location(row - 1, col + 1)).piece: # print("here") self.moves.add(Location(row - 1, col + 1)) if self.getSquare(Location(row - 1, col + 1)).piece: # print("here") self.moves.add(Location(row - 1, col - 1)) else: print("Clicked on an invalid chess tile") for move in self.moves: self.addIndicator(move)