Hexdraw almost done

This commit is contained in:
jona605a
2020-08-06 20:45:07 +02:00
parent 89df136c8d
commit af276e268b
3 changed files with 71 additions and 80 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

View File

@ -93,16 +93,12 @@ def hexStart(channel, user, opponent):
json.dump(data,f,indent=4) json.dump(data,f,indent=4)
# draw the board # draw the board
#fourInARowDraw.drawImage(channel) hexDraw.drawBoard(channel) # something something
# hexDraw() # something something showImage = True
gwendoTurn = False gwendoTurn = True if players[0] == "Gwendolyn" else False
if players[0] == "Gwendolyn": return "Started Hex game against "+getName(opponent)+ diffText+". It's "+getName(players[0])+"'s turn", showImage, False, False, gwendoTurn
# in case she has the first move
gwendoTurn = True
return "Started game against "+getName(opponent)+ diffText+". It's "+getName(players[0])+"'s turn", True, False, False, gwendoTurn
else: else:
return "There's already a hex game going on in this channel", False, False, False, False return "There's already a hex game going on in this channel", False, False, False, False

View File

@ -5,73 +5,77 @@ import os
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
#from funcs import logThis, getName #from funcs import logThis, getName
def drawHex(channel): def drawBoard(channel):
#logThis("Drawing Hex board") #logThis("Drawing Hex board")
# Defining all the variables # Defining all the variables
CANVAS_WIDTH = 800 CANVAS_WIDTH = 2400
CANVAS_HEIGHT = 600 CANVAS_HEIGHT = 1800
SIDELENGTH = 25 SIDELENGTH = 75
X_OFFSET = CANVAS_WIDTH/2 - 8*math.sqrt(3)*SIDELENGTH # The offsets centers the board in the picture 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 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 hexagonwidth = math.sqrt(3) * SIDELENGTH # the whole width
hexagonheight = 1.5 * SIDELENGTH # not really the entire height, but the height difference between two layers 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 fontsize = 45
pieceDiameter = 30
fontsize = 15
textcolor = (0,0,0) textcolor = (0,0,0)
fnt = ImageFont.truetype('resources/futura-bold.ttf', fontsize) 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 # Creates the empty image
im = Image.new('RGB', size=(CANVAS_WIDTH, CANVAS_HEIGHT),color='lightgrey') im = Image.new('RGB', size=(CANVAS_WIDTH, CANVAS_HEIGHT),color='lightgrey')
# 'd' is a shortcut to drawing on the image # 'd' is a shortcut to drawing on the image
d = ImageDraw.Draw(im,"RGBA") d = ImageDraw.Draw(im,"RGBA")
# Creates the hexagons # These are the coordinates for the upperleft corner of every hex
hexList = _scale_coordinates(generate_unit_hexagons, X_OFFSET, Y_OFFSET) # these two functions are defined below boardCoordinates = [ [(X_OFFSET + hexagonwidth*(column + row/2),Y_OFFSET + hexagonheight*row) for column in range(11)] for row in range(11)]
for hexagon in hexList: # The board is indexed with [row][column]
d.polygon(hexagon, fill = 'white',outline='black') 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")
# TESTING BOARD # Draw color on the outside of the board
board = [ [ 0 for i in range(11) ] for j in range(11) ] for point in boardCoordinates[0]
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 # Writes "abc..", "123.." on the columns and rows
for i in range(11): for i in range(11):
# Top letters # Top letters
d.text( (X_OFFSET + hexagonwidth*i, Y_OFFSET-22) , "ABCDEFGHIJK"[i], font=fnt, fill=textcolor) d.text( (X_OFFSET + hexagonwidth*i, Y_OFFSET-66) , "ABCDEFGHIJK"[i], font=fnt, fill=textcolor)
# Bottom letters # Bottom letters
d.text( (X_OFFSET + hexagonwidth*(i+11.5/2), Y_OFFSET - 5 + 11*hexagonheight) , "ABCDEFGHIJK"[i], font=fnt, fill=textcolor) d.text( (X_OFFSET + hexagonwidth*(i+11.5/2), Y_OFFSET - 15 + 11*hexagonheight) , "ABCDEFGHIJK"[i], font=fnt, fill=textcolor)
# Left numbers # 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") 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 # Right numbers
d.text( (X_OFFSET + hexagonwidth*(i/2+11) + 10 , Y_OFFSET + 6 + i*hexagonheight) , str(i+1), font=fnt, fill=textcolor) 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("board"+channel+".png")
#im.save(filename="resources/games/hexBoards/board"+channel+".png") #im.save("resources/games/hexBoards/board"+channel+".png")
def drawHexPlacement(channel,player,position): def drawHexPlacement(channel,player,position):
# Opens the image # Same variables as above
try:
im = loadsomethingsomething("board"+channel+".png")
except:
logThis("Error loading board-image (error code 1541")
# 'd' is a shortcut to drawing on the image
d = ImageDraw.Draw(im,"RGBA")
CANVAS_WIDTH = 800 CANVAS_WIDTH = 800
CANVAS_HEIGHT = 600 CANVAS_HEIGHT = 600
SIDELENGTH = 25 SIDELENGTH = 25
@ -83,44 +87,35 @@ def drawHexPlacement(channel,player,position):
pieceDiameter = 30 pieceDiameter = 30
# Translates position # Translates position
# We don't need to check, because the position is already checked in placeOnHexBoard() # We don't need to error-check, because the position is already checked in placeOnHexBoard()
position = position.lower() position = position.lower()
column = ord(position[0])-97 # ord() translates from letter to number column = ord(position[0])-97 # ord() translates from letter to number
row = int(position[1]) row = int(position[1])-1
# Draws the piece # Find the coordinates for the piece-drawing
x = X_OFFSET + hexagonwidth*(column + row/2 + 1/2) - pieceDiameter/2 x = X_OFFSET + hexagonwidth*(column + row/2 + 1/2) - pieceDiameter/2
y = Y_OFFSET + hexagonheight*row + hexagonheight/2 - pieceDiameter/math.sqrt(2) y = Y_OFFSET + hexagonheight*row + hexagonheight/2 - pieceDiameter/math.sqrt(2)
d.ellipse([(x,y),(x + pieceDiameter,y + pieceDiameter)], fill = pieceColor[player])
# 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
def generate_unit_hexagons(): im.save("board"+channel+".png")
h = math.sqrt(3)/2 # Half the width of the hexagon except:
for y in range(11): #logThis("Error loading board-image (error code 1541")
for x in [i*2+y for i in range(11)]: print("Oh no, error error")
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__': if __name__ == '__main__':
drawHex("HexTest") drawBoard("HexTest2")
#drawHexPlacement("HexTest",1,"f7")
""" """
with open("resources/games/hexGames.json", "r") as f: with open("resources/games/hexGames.json", "r") as f: