110 lines
3.7 KiB
Python
110 lines
3.7 KiB
Python
import json
|
|
import math
|
|
import os
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
#from funcs import logThis, getName
|
|
|
|
def drawHex(channel):
|
|
#logThis("Drawing Hex board")
|
|
|
|
# Defining all the variables
|
|
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
|
|
|
|
fontsize = 15
|
|
textcolor = (0,0,0)
|
|
fnt = ImageFont.truetype('resources/futura-bold.ttf', fontsize)
|
|
|
|
# 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")
|
|
|
|
# Creates the hexagons
|
|
hexList = _scale_coordinates(generate_unit_hexagons, X_OFFSET, Y_OFFSET) # these two functions are defined below
|
|
for hexagon in hexList:
|
|
d.polygon(hexagon, fill = 'white',outline='black')
|
|
|
|
board = [ [ 0 for i in range(11) ] for j in range(11) ]
|
|
board[0][0] = 1
|
|
board[1][0] = 1
|
|
board[2][1] = 1
|
|
board[3][6] = 2
|
|
board[2][5] = 2
|
|
|
|
# Draws the pieces from the board
|
|
for row in range(11):
|
|
for column in range(11):
|
|
if board[row][column] != 0:
|
|
x = X_OFFSET + hexagonwidth*(column + row/2 + 1/2) - pieceDiameter/2
|
|
y = Y_OFFSET + hexagonheight*row + hexagonheight/2 - pieceDiameter/math.sqrt(2)
|
|
d.ellipse([(x,y),(x + pieceDiameter,y + pieceDiameter)], fill = pieceColor[board[row][column]])
|
|
|
|
# Writes "abc..", "123.." on the columns and rows
|
|
for i in range(11):
|
|
# Top letters
|
|
d.text( (X_OFFSET + hexagonwidth*i, Y_OFFSET-22) , "ABCDEFGHIJK"[i], font=fnt, fill=textcolor)
|
|
# Bottom letters
|
|
d.text( (X_OFFSET + hexagonwidth*(i+11.5/2), Y_OFFSET - 5 + 11*hexagonheight) , "ABCDEFGHIJK"[i], font=fnt, fill=textcolor)
|
|
# Left numbers
|
|
d.multiline_text( (X_OFFSET + hexagonwidth*i/2 - 24 -4*(i>8), Y_OFFSET + 6 + i*hexagonheight) , str(i+1), font=fnt, fill=textcolor, align="right")
|
|
# Right numbers
|
|
d.text( (X_OFFSET + hexagonwidth*(i/2+11) + 10 , Y_OFFSET + 6 + i*hexagonheight) , str(i+1), font=fnt, fill=textcolor)
|
|
|
|
im.save("board"+channel+".png")
|
|
#im.save(filename="resources/games/hexBoards/board"+channel+".png")
|
|
|
|
|
|
|
|
def generate_unit_hexagons():
|
|
h = math.sqrt(3)/2 # Half the width of the hexagon
|
|
for y in range(11):
|
|
for x in [i*2+y for i in range(11)]:
|
|
x_ = x*h
|
|
y_ = y*1.5
|
|
|
|
yield [
|
|
(x_, y_),
|
|
(x_+h, y_-0.5),
|
|
(x_+2*h, y_),
|
|
(x_+2*h, y_+1),
|
|
(x_+h, y_+1.5),
|
|
(x_, y_+1),
|
|
]
|
|
|
|
def _scale_coordinates(generator, x_offset, y_offset, side_length=25):
|
|
for coords in generator():
|
|
yield [(x * side_length + x_offset, y * side_length + y_offset) for (x, y) in coords]
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
drawHex("HexTest")
|
|
|
|
"""
|
|
|
|
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])
|
|
|
|
|
|
""" |