Hex so beautiful. And works!!!

This commit is contained in:
jona605a
2020-08-07 23:31:10 +02:00
parent b45aac1d1c
commit e6ce610366
3 changed files with 143 additions and 82 deletions

View File

@ -23,12 +23,14 @@ X_THICKNESS = HEXTHICKNESS * math.cos(math.pi/6)
Y_THICKNESS = HEXTHICKNESS * math.sin(math.pi/6)
BACKGROUND_COLOR = (230,230,230)
BETWEEN_COLOR = BACKGROUND_COLOR
BLANK_COLOR = "lightgrey"
BLANK_COLOR = "lightgrey" # maybe lighter?
BOARDCOORDINATES = [ [(X_OFFSET + HEXAGONWIDTH*(column + row/2),Y_OFFSET + HEXAGONHEIGHT*row) for column in range(11)] for row in range(11)] # These are the coordinates for the upperleft corner of every hex
COLHEXTHICKNESS = 4
COLX_THICKNESS = COLHEXTHICKNESS * math.cos(math.pi/6)
COLY_THICKNESS = COLHEXTHICKNESS * math.sin(math.pi/6)
X_NAME = {1:175, 2:CANVAS_WIDTH-100}
Y_NAME = {1:CANVAS_HEIGHT-150, 2:150}
NAMEHEXPADDING = 75
def drawBoard(channel):
logThis("Drawing empty Hex board")
@ -106,6 +108,26 @@ def drawBoard(channel):
else:
player2 = getName(players[1])
"""
with open("resources/games/hexGames.json", "r") as f:
data = json.load(f)
for p in [1,2]:
playername = getName(data[channel]["players"][p-1])
# Draw name
x = X_NAME[p]
x -= fnt.getsize(playername)[0] if p==2 else 0 # player2's name is right-aligned
y = Y_NAME[p]
d.text((x,y),playername, font=fnt, fill = TEXTCOLOR)
# Draw a half-size Hexagon to indicate the player's color
x -= NAMEHEXPADDING # To the left of both names
d.polygon([
(x, y),
(x+HEXAGONWIDTH/4, y-SIDELENGTH/4),
(x+HEXAGONWIDTH/2, y),
(x+HEXAGONWIDTH/2, y+SIDELENGTH/2),
(x+HEXAGONWIDTH/4, y+SIDELENGTH*3/4),
(x, y+SIDELENGTH/2),
],fill = PIECECOLOR[p])
im.save("resources/games/hexBoards/board"+channel+".png")
@ -119,7 +141,7 @@ def drawHexPlacement(channel,player,position):
# We don't need to error-check, because the position is already checked in placeOnHexBoard()
position = position.lower()
column = ord(position[0])-97 # ord() translates from letter to number
row = int(position[1])-1
row = int(position[1:])-1
# Find the coordinates for the filled hex drawing
hexCoords = [
@ -136,7 +158,7 @@ def drawHexPlacement(channel,player,position):
with Image.open(FILEPATH) as im:
d = ImageDraw.Draw(im,"RGBA")
# Draws the hex piece
d.polygon(hexCoords,fill = PIECECOLOR[player])
d.polygon(hexCoords,fill = PIECECOLOR[player], outline = BETWEEN_COLOR)
# Save
im.save(FILEPATH)
@ -144,12 +166,12 @@ def drawHexPlacement(channel,player,position):
logThis("Error drawing new hex on board (error code 1541")
if __name__ == '__main__':
drawBoard("HexTest2")
drawHexPlacement("HexTest2",1,"f7")
drawHexPlacement("HexTest2",2,"f8")
drawHexPlacement("HexTest2",1,"h6")
drawHexPlacement("HexTest2",2,"e8")
drawHexPlacement("HexTest2",1,"e7")
drawHexPlacement("HexTest2",2,"c9")
drawHexPlacement("HexTest2",1,"g8")
drawHexPlacement("HexTest2",2,"h4")
drawBoard("resources/games/hexBoards/boardTest.png")
drawHexPlacement("resources/games/hexBoards/boardTest.png",1,"f7")
drawHexPlacement("resources/games/hexBoards/boardTest.png",2,"f8")
drawHexPlacement("resources/games/hexBoards/boardTest.png",1,"h6")
drawHexPlacement("resources/games/hexBoards/boardTest.png",2,"e8")
drawHexPlacement("resources/games/hexBoards/boardTest.png",1,"e7")
drawHexPlacement("resources/games/hexBoards/boardTest.png",2,"c9")
drawHexPlacement("resources/games/hexBoards/boardTest.png",1,"g8")
drawHexPlacement("resources/games/hexBoards/boardTest.png",2,"h4")