137 lines
5.3 KiB
Python
137 lines
5.3 KiB
Python
import json
|
|
import math
|
|
import os
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
#from funcs import logThis, getName
|
|
|
|
def drawBoard(channel):
|
|
#logThis("Drawing Hex board")
|
|
|
|
# Defining all the variables
|
|
CANVAS_WIDTH = 2400
|
|
CANVAS_HEIGHT = 1800
|
|
SIDELENGTH = 75
|
|
X_OFFSET = CANVAS_WIDTH/2 - 8*math.sqrt(3)*SIDELENGTH # The offsets centers the board in the picture
|
|
Y_OFFSET = CANVAS_HEIGHT/2 - 8*SIDELENGTH # The offsets are the coordinates of the upperleft point in the upperleftmost hexagon
|
|
hexagonwidth = math.sqrt(3) * SIDELENGTH # the whole width of one hexagon
|
|
hexagonheight = 1.5 * SIDELENGTH # not really the entire height, but the height difference between two layers
|
|
fontsize = 45
|
|
textcolor = (0,0,0)
|
|
fnt = ImageFont.truetype('resources/futura-bold.ttf', fontsize)
|
|
hexThickness = 6
|
|
xThickness = hexThickness * math.cos(math.pi/6)
|
|
yThickness = hexThickness * math.sin(math.pi/6)
|
|
|
|
# Creates the empty image
|
|
im = Image.new('RGB', size=(CANVAS_WIDTH, CANVAS_HEIGHT),color='lightgrey')
|
|
# 'd' is a shortcut to drawing on the image
|
|
d = ImageDraw.Draw(im,"RGBA")
|
|
|
|
# These are the coordinates for the upperleft corner of every hex
|
|
boardCoordinates = [ [(X_OFFSET + hexagonwidth*(column + row/2),Y_OFFSET + hexagonheight*row) for column in range(11)] for row in range(11)]
|
|
# The board is indexed with [row][column]
|
|
for column in boardCoordinates:
|
|
for startingPoint in column:
|
|
x = startingPoint[0]
|
|
y = startingPoint[1]
|
|
h = SIDELENGTH * math.sqrt(3)/2 # which is also sin(pi/3)
|
|
d.polygon([
|
|
(x, y),
|
|
(x+h, y-0.5*SIDELENGTH),
|
|
(x+2*h, y),
|
|
(x+2*h, y+SIDELENGTH),
|
|
(x+h, y+1.5*SIDELENGTH),
|
|
(x, y+SIDELENGTH),
|
|
],fill="black")
|
|
d.polygon([
|
|
(x+xThickness, y + yThickness),
|
|
(x+h, y-0.5*SIDELENGTH + hexThickness),
|
|
(x+2*h-xThickness, y + yThickness),
|
|
(x+2*h-xThickness, y+SIDELENGTH - yThickness),
|
|
(x+h, y+1.5*SIDELENGTH - hexThickness),
|
|
(x+xThickness, y+SIDELENGTH - yThickness),
|
|
],fill="white")
|
|
|
|
# Draw color on the outside of the board
|
|
for point in boardCoordinates[0]:
|
|
"hi nikolaj"
|
|
|
|
# Writes "abc..", "123.." on the columns and rows
|
|
for i in range(11):
|
|
# Top letters
|
|
d.text( (X_OFFSET + hexagonwidth*i, Y_OFFSET-66) , "ABCDEFGHIJK"[i], font=fnt, fill=textcolor)
|
|
# Bottom letters
|
|
d.text( (X_OFFSET + hexagonwidth*(i+11.5/2), Y_OFFSET - 15 + 11*hexagonheight) , "ABCDEFGHIJK"[i], font=fnt, fill=textcolor)
|
|
# Left numbers
|
|
d.multiline_text( (X_OFFSET + hexagonwidth*i/2 - 72 -4*(i>8), Y_OFFSET + 18 + i*hexagonheight) , str(i+1), font=fnt, fill=textcolor, align="right")
|
|
# Right numbers
|
|
d.text( (X_OFFSET + hexagonwidth*(i/2+11) + 30 , Y_OFFSET + 6 + i*hexagonheight) , str(i+1), font=fnt, fill=textcolor)
|
|
|
|
im.save("board"+channel+".png")
|
|
#im.save("resources/games/hexBoards/board"+channel+".png")
|
|
|
|
|
|
|
|
|
|
def drawHexPlacement(channel,player,position):
|
|
# Same variables as above
|
|
CANVAS_WIDTH = 800
|
|
CANVAS_HEIGHT = 600
|
|
SIDELENGTH = 25
|
|
X_OFFSET = CANVAS_WIDTH/2 - 8*math.sqrt(3)*SIDELENGTH # The offsets centers the board in the picture
|
|
Y_OFFSET = CANVAS_HEIGHT/2 - 8*SIDELENGTH # The offsets are the coordinates of the upperleft point in the upperleftmost hexagon
|
|
hexagonwidth = math.sqrt(3) * SIDELENGTH # the whole width
|
|
hexagonheight = 1.5 * SIDELENGTH # not really the entire height, but the height difference between two layers
|
|
pieceColor = {1:(200,0,0),2:(0,0,200)} # player1 is red, player2 is blue
|
|
pieceDiameter = 30
|
|
|
|
# Translates 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
|
|
|
|
# Find the coordinates for the piece-drawing
|
|
x = X_OFFSET + hexagonwidth*(column + row/2 + 1/2) - pieceDiameter/2
|
|
y = Y_OFFSET + hexagonheight*row + hexagonheight/2 - pieceDiameter/math.sqrt(2)
|
|
|
|
# Opens the image
|
|
#logThis("Finding and opening the board-image at hex"+channel+".png")
|
|
try:
|
|
with Image.open("board"+channel+".png") as im:
|
|
d = ImageDraw.Draw(im,"RGBA")
|
|
# Draws the piece
|
|
|
|
d.ellipse([(x,y),(x + pieceDiameter,y + pieceDiameter)], fill = pieceColor[player])
|
|
# Draw some fancy connection thing
|
|
#something something
|
|
|
|
# Save
|
|
im.save("board"+channel+".png")
|
|
except:
|
|
#logThis("Error loading board-image (error code 1541")
|
|
print("Oh no, error error")
|
|
|
|
if __name__ == '__main__':
|
|
drawBoard("HexTest2")
|
|
#drawHexPlacement("HexTest",1,"f7")
|
|
"""
|
|
|
|
with open("resources/games/hexGames.json", "r") as f:
|
|
data = json.load(f)
|
|
|
|
board = data[channel]["board"]
|
|
# Getting player names
|
|
players = data[channel]["players"]
|
|
if players[0] == "Gwendolyn":
|
|
player1 = "Gwendolyn"
|
|
else:
|
|
player1 = getName(players[0])
|
|
if players[1] == "Gwendolyn":
|
|
player2 = "Gwendolyn"
|
|
else:
|
|
player2 = getName(players[1])
|
|
|
|
|
|
""" |