diff --git a/boardHexTest.png b/boardHexTest.png deleted file mode 100644 index 128fd0f..0000000 Binary files a/boardHexTest.png and /dev/null differ diff --git a/funcs/games/hex.py b/funcs/games/hex.py index 5962855..cf13955 100644 --- a/funcs/games/hex.py +++ b/funcs/games/hex.py @@ -93,16 +93,12 @@ def hexStart(channel, user, opponent): json.dump(data,f,indent=4) # draw the board - #fourInARowDraw.drawImage(channel) - # hexDraw() # something something + hexDraw.drawBoard(channel) # something something + showImage = True - gwendoTurn = False - - if players[0] == "Gwendolyn": - # 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 + gwendoTurn = True if players[0] == "Gwendolyn" else False + + return "Started Hex game against "+getName(opponent)+ diffText+". It's "+getName(players[0])+"'s turn", showImage, False, False, gwendoTurn else: return "There's already a hex game going on in this channel", False, False, False, False @@ -157,7 +153,7 @@ def placeHex(channel : str,player : int,position : str): # Update the board drawHexPlacement(channel,player,position) - + return message, True, True, gameWon, gwendoTurn else: # Invalid move. "board" is the error message diff --git a/funcs/games/hexDraw.py b/funcs/games/hexDraw.py index e41243f..3d57b6f 100644 --- a/funcs/games/hexDraw.py +++ b/funcs/games/hexDraw.py @@ -5,73 +5,77 @@ import os from PIL import Image, ImageDraw, ImageFont #from funcs import logThis, getName -def drawHex(channel): +def drawBoard(channel): #logThis("Drawing Hex board") # Defining all the variables - CANVAS_WIDTH = 800 - CANVAS_HEIGHT = 600 - SIDELENGTH = 25 + 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 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 + 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") - # 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') + # 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] + - # TESTING BOARD - 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) + 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 - 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 - 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 - 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(filename="resources/games/hexBoards/board"+channel+".png") + #im.save("resources/games/hexBoards/board"+channel+".png") + + + def drawHexPlacement(channel,player,position): - # Opens the image - 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") - + # Same variables as above CANVAS_WIDTH = 800 CANVAS_HEIGHT = 600 SIDELENGTH = 25 @@ -83,44 +87,35 @@ def drawHexPlacement(channel,player,position): pieceDiameter = 30 # 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() 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 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 -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] - - + # Save + im.save("board"+channel+".png") + except: + #logThis("Error loading board-image (error code 1541") + print("Oh no, error error") if __name__ == '__main__': - drawHex("HexTest") - + drawBoard("HexTest2") + #drawHexPlacement("HexTest",1,"f7") """ with open("resources/games/hexGames.json", "r") as f: