+/HIP

hexagonal iso-path game

git clone https://git.julia.blue/+/HIP

Joseph RyanAdd replay saving, loading, and viewingb9df0d7

master
8.9 KiB212 linesraw
1import pygame
2from src.replay import *
3
4
5def removePiece(hex, pieces):
6    for i in range(len(pieces)):
7        for j in range(len(pieces[i])):
8            if pieces[i][j] == hex:
9                del pieces[i][j]
10                return
11
12
13def drawHex(surface, color, layout, hex, width=0):
14    x, y = tuple(hexToPixel(layout, hex))
15    if width == 0:
16        pygame.draw.polygon(surface, color, polygonCorners(layout, hex))
17    else:
18        pygame.draw.aalines(surface, color, width, polygonCorners(layout, hex))
19
20
21def drawBoard(surface, outlineColor, hexColor, layout, board, coords=False):
22    color = (hexColor, tuple([min(i + 40, 255) for i in hexColor]), tuple([max(i - 40, 0) for i in hexColor]))
23    for h in board:
24        drawHex(surface, color[sign(board[h])], layout, h)
25        temp = hexToPixel(layout, h)
26        if abs(board[h]) == 2:
27            c = (None, 0, 255)[sign(board[h])]
28            pygame.draw.circle(surface, (c, c, c), (int(temp.x), int(temp.y)), int(layout.size[0] / 2))
29        if coords:
30            words = pygame.font.SysFont(FONT, 16).render(str(h), 0, HINT_COLOR)
31            surface.blit(words, (temp.x - words.get_width() / 2, temp.y - words.get_height() / 2))
32    for h in board:
33        drawHex(surface, outlineColor, layout, h, 2)
34
35
36def reset(surface, text, layout, board, moveList):
37    createReplay(moveList)
38    words = pygame.font.SysFont(FONT, FONT_SIZE).render(text, 0, TEXT_COLOR)
39    while True:
40        windowSurface.fill(BACKGROUND_COLOR)
41        drawBoard(windowSurface, BACKGROUND_COLOR, BOARD_COLOR, layout, board)
42        surface.blit(words, (WINDOW_WIDTH / 2 - words.get_width() / 2, WINDOW_HEIGHT / 2 - words.get_height() / 2))
43        pygame.display.update()
44        for event in pygame.event.get():
45            if event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.KEYDOWN:
46                return True
47            elif event.type == pygame.QUIT:
48                return False
49
50
51def executeMove(board, move):
52    if type(move) == Hex:  # capture
53        board[move] -= sign(board[move])
54    elif abs(board[move.start]) == 2:  # hex move
55        board[move.start] -= sign(board[move.start])
56        board[move.end] += sign(board[move.start])
57    else:  # piece move
58        board[move.start] -= 1
59        board[move.end] += 1
60
61
62def revertMove(board, move):
63    if type(move) == Hex:  # capture
64        board[move] += sign(board[move])
65    elif abs(board[move.end]) == 2:  # hex move
66        board[move.start] += sign(board[move.start])
67        board[move.end] -= sign(board[move.start])
68    else:  # piece move
69        board[move.start] += 1
70        board[move.end] -= 1
71
72
73def viewReplay(fileName):
74    bs, moveList = readReplay(fileName)
75    b = createBoard(bs)
76    current = 0
77    l = Layout(Orientation(math.sqrt(3.0), math.sqrt(3.0) / 2.0, 0.0, 3.0 / 2.0, math.sqrt(3.0) / 3.0, -1.0 / 3.0, 0.0, 2.0 / 3.0, 0.5), Point(HEX_SIZE, HEX_SIZE), Point(WINDOW_WIDTH / 2., WINDOW_HEIGHT / 2.))
78    clock = pygame.time.Clock()
79    back = (pygame.K_LEFT, pygame.K_BACKSPACE, pygame.K_ESCAPE)
80    forward = (pygame.K_RIGHT, pygame.K_RETURN, pygame.K_SPACE)
81
82    while True:
83        clock.tick(FRAME_RATE)  # throttle cpu
84        windowSurface.fill(BACKGROUND_COLOR)
85        drawBoard(windowSurface, BACKGROUND_COLOR, BOARD_COLOR, l, b)
86        pygame.display.update()
87        for event in pygame.event.get():
88            if event.type == pygame.KEYDOWN:
89                if event.key in back:
90                    if current == 0:
91                        continue
92                    current -= 1
93                    revertMove(b, moveList[current])
94                elif event.key in forward:
95                    if current == len(moveList):
96                        continue
97                    executeMove(b, moveList[current])
98                    current += 1
99            if event.type == pygame.QUIT:
100                return
101
102
103def main():
104    b = createBoard(BOARD_SIZE)
105    turn = 1
106    l = Layout(Orientation(math.sqrt(3.0), math.sqrt(3.0) / 2.0, 0.0, 3.0 / 2.0, math.sqrt(3.0) / 3.0, -1.0 / 3.0, 0.0, 2.0 / 3.0, 0.5), Point(HEX_SIZE, HEX_SIZE), Point(WINDOW_WIDTH / 2., WINDOW_HEIGHT / 2.))
107    clock = pygame.time.Clock()
108    movedHex = False
109    movedPiece = False
110    captured = False
111    selected = None
112    underAttack = []
113    selectedColor = None
114    holdingPiece = False
115    currentMove = []
116    moveList = []
117    pieces = getPieces(b)
118
119    while True:
120        clock.tick(FRAME_RATE)  # throttle cpu
121        windowSurface.fill(BACKGROUND_COLOR)
122        pos = pygame.mouse.get_pos()
123        current = pixelToHex(l, Point(pos[0], pos[1]))
124        drawBoard(windowSurface, BACKGROUND_COLOR, BOARD_COLOR, l, b)
125
126        # Draw held piece
127        if selected != None:
128            if SHOW_HINTS:
129                for h in b:
130                    if holdingPiece and piecePlace(b, Move(selected, h), turn, False) or not holdingPiece and hexPlace(b, Move(selected, h), turn, movedPiece or captured, pieces, False):
131                        temp = hexToPixel(l, h)
132                        pygame.draw.circle(windowSurface, HINT_COLOR, (int(temp.x), int(temp.y)), int(l.size[0] / 8))
133            if holdingPiece:
134                pygame.draw.circle(windowSurface, selectedColor, pos, int(l.size[0] / 2))
135            else:
136                drawHex(windowSurface, selectedColor, l, pixelToHex(l, Point(pos[0], pos[1]), False))
137
138        if SHOW_HINTS and selected == None:
139            for h in b:
140                if not movedHex and hexPickup(b, h, turn, movedPiece or captured, pieces, False) or not movedPiece and piecePickup(b, h, turn, False):
141                    temp = hexToPixel(l, h)
142                    pygame.draw.circle(windowSurface, HINT_COLOR, (int(temp.x), int(temp.y)), int(l.size[0] / 8))
143
144        pygame.display.update()
145
146        for event in pygame.event.get():
147            if event.type == pygame.MOUSEBUTTONDOWN:
148                if current in b:
149                    if not movedHex and hexPickup(b, current, turn, movedPiece or captured, pieces):
150                        selected = current
151                        selectedColor = (BOARD_COLOR, tuple([min(i + 40, 255) for i in BOARD_COLOR]), tuple([max(i - 40, 0) for i in BOARD_COLOR]))[sign(b[current] + 1)]
152                    elif not movedPiece and piecePickup(b, current, turn):
153                        selected = current
154                        selectedColor = tuple([(None, 0, 255)[turn]] * 3)
155                        holdingPiece = True
156                    else:
157                        # failed pickup
158                        pass
159            elif event.type == pygame.MOUSEBUTTONUP:
160                if selected != None:  # dropping a piece
161                    if holdingPiece:
162                        movedPiece = piecePlace(b, Move(selected, current), turn)
163                        if movedPiece:
164                            removePiece(selected, pieces)
165                            pieces[(turn + 1) / 2].append(current)
166                        if hasWon(pieces) != 0:
167                            currentMove.append(Move(selected, current))
168                            if len(currentMove) == 0:
169                                currentMove.append(Move(Hex(0, 0, 0), Hex(0, 0, 0)))
170                            moveList.append(currentMove)
171                            return reset(windowSurface, "GAME OVER", l, b, moveList)
172                    else:
173                        movedHex = hexPlace(b, Move(selected, current), turn, movedPiece or captured, pieces)
174                elif not captured and current in underAttack:  # capturing a piece
175                    b[current] += turn
176                    captured = True
177                    removePiece(current, pieces)
178                    if hasWon(pieces) != 0:
179                        currentMove.append(current)
180                        moveList.append(currentMove)
181                        return reset(windowSurface, "GAME OVER", l, b, moveList)
182                temp = movedHex + movedPiece + captured
183                if temp == 1 and len(currentMove) == 0 or temp == 2:
184                    if selected == None:
185                        currentMove.append(current)
186                    else:
187                        currentMove.append(Move(selected, current))
188                if temp == 2:
189                    moveList.append(currentMove)
190                    movedHex = False
191                    movedPiece = False
192                    captured = False
193                    turn = -turn
194                    currentMove = []
195                    underAttack = getThreatened(turn, pieces)
196                selected = None
197                holdingPiece = False
198            elif event.type == pygame.QUIT:
199                return False
200
201if __name__ == '__main__':
202    pygame.init()
203    windowSurface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
204    if len(sys.argv) > 1:
205        pygame.display.set_caption("Viewing replay... ")
206        viewReplay(sys.argv[1])
207    else:
208        pygame.display.set_caption("Hexagonal Iso-Path")
209        while main():
210            pass
211    pygame.quit()
212    sys.exit()