()
| 19 | IMAGES[piece] = p.transform.scale(p.image.load("images/" + piece + ".png"), (SQ_SIZE, SQ_SIZE)) |
| 20 | |
| 21 | def main(): |
| 22 | p.init() |
| 23 | screen = p.display.set_mode((WIDTH, HEIGHT)) |
| 24 | clock = p.time.Clock() |
| 25 | screen.fill(p.Color("white")) |
| 26 | gs = ChessEngine.GameState() |
| 27 | validMoves = gs.getValidMoves() |
| 28 | moveMade = False |
| 29 | animate = False |
| 30 | loadImages() |
| 31 | running = True |
| 32 | sqSelected = () |
| 33 | playerClicks = [] |
| 34 | gameOver = False |
| 35 | while running: |
| 36 | for e in p.event.get(): |
| 37 | if e.type == p.QUIT: |
| 38 | running = False |
| 39 | elif e.type == p.MOUSEBUTTONDOWN: |
| 40 | if not gameOver: |
| 41 | location = p.mouse.get_pos() |
| 42 | col = location[0]//SQ_SIZE |
| 43 | row = location[1]//SQ_SIZE |
| 44 | if sqSelected == (row, col): |
| 45 | sqSelected = () |
| 46 | playerClicks = [] |
| 47 | else: |
| 48 | sqSelected = (row, col) |
| 49 | playerClicks.append(sqSelected) |
| 50 | if len(playerClicks) == 1 and (gs.board[row][col] == "--"): |
| 51 | sqSelected = () |
| 52 | playerClicks = [] |
| 53 | if len(playerClicks) == 2: |
| 54 | move = ChessEngine.Move(playerClicks[0], playerClicks[1], gs.board) |
| 55 | for i in range(len(validMoves)): |
| 56 | if move == validMoves[i]: |
| 57 | gs.makeMove(move) |
| 58 | moveMade = True |
| 59 | animate = True |
| 60 | sqSelected = () |
| 61 | playerClicks = [] |
| 62 | if not moveMade: |
| 63 | playerClicks = [sqSelected] |
| 64 | elif e.type == p.KEYDOWN: |
| 65 | if e.key == p.K_z: |
| 66 | gs.undoMove() |
| 67 | moveMade = True |
| 68 | animate = False |
| 69 | if e.key == p.K_r: |
| 70 | gs = ChessEngine.GameState() |
| 71 | validMoves = gs.getValidMoves() |
| 72 | sqSelected = () |
| 73 | playerClicks = [] |
| 74 | moveMade = False |
| 75 | animate = False |
| 76 | if moveMade: |
| 77 | if animate: |
| 78 | animatedMoves(gs.moveLog[-1], screen, gs.board,clock) |
no test coverage detected