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