diff --git a/funcs/games/hex.py b/funcs/games/hex.py index cf13955..542431c 100644 --- a/funcs/games/hex.py +++ b/funcs/games/hex.py @@ -93,11 +93,11 @@ def hexStart(channel, user, opponent): json.dump(data,f,indent=4) # draw the board - hexDraw.drawBoard(channel) # something something - showImage = True + hexDraw.drawBoard(channel) + gwendoTurn = True if players[0] == "Gwendolyn" else False - + showImage = True 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 @@ -112,7 +112,8 @@ def placeHex(channel : str,player : int,position : str): # Places on board board = placeOnHexBoard(board,player,position) - + # Updates the drawing + hexDraw.drawHexPlacement(channel,player,position) if isinstance(board, list): # If the move is valid: @@ -152,7 +153,7 @@ def placeHex(channel : str,player : int,position : str): gwendoTurn = True # Update the board - drawHexPlacement(channel,player,position) + hexDraw.drawHexPlacement(channel,player,position) return message, True, True, gameWon, gwendoTurn else: diff --git a/funcs/games/hexDraw.py b/funcs/games/hexDraw.py index f7333dd..04ae78f 100644 --- a/funcs/games/hexDraw.py +++ b/funcs/games/hexDraw.py @@ -19,12 +19,18 @@ def drawBoard(channel): FONTSIZE = 45 TEXTCOLOR = (0,0,0) fnt = ImageFont.truetype('resources/futura-bold.ttf', FONTSIZE) + PIECECOLOR = {1:(237,41,57),2:(0,165,255)} # player1 is red, player2 is blue + LINETHICKNESS = 15 HEXTHICKNESS = 6 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" + # 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 = BACKGROUND_COLOR) # 'd' is a shortcut to drawing on the image d = ImageDraw.Draw(im,"RGBA") @@ -35,27 +41,36 @@ def drawBoard(channel): 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") + (x, y), + (x+HEXAGONWIDTH/2, y-0.5*SIDELENGTH), + (x+HEXAGONWIDTH, y), + (x+HEXAGONWIDTH, y+SIDELENGTH), + (x+HEXAGONWIDTH/2, y+1.5*SIDELENGTH), + (x, y+SIDELENGTH), + ],fill = BETWEEN_COLOR) d.polygon([ - (x+X_THICKNESS, y + Y_THICKNESS), - (x+h, y-0.5*SIDELENGTH + HEXTHICKNESS), - (x+2*h-X_THICKNESS, y + Y_THICKNESS), - (x+2*h-X_THICKNESS, y+SIDELENGTH - Y_THICKNESS), - (x+h, y+1.5*SIDELENGTH - HEXTHICKNESS), - (x+X_THICKNESS, y+SIDELENGTH - Y_THICKNESS), - ],fill="white") + (x+X_THICKNESS, y + Y_THICKNESS), + (x+HEXAGONWIDTH/2, y-0.5*SIDELENGTH + HEXTHICKNESS), + (x+HEXAGONWIDTH-X_THICKNESS, y + Y_THICKNESS), + (x+HEXAGONWIDTH-X_THICKNESS, y+SIDELENGTH - Y_THICKNESS), + (x+HEXAGONWIDTH/2, y+1.5*SIDELENGTH - HEXTHICKNESS), + (x+X_THICKNESS, y+SIDELENGTH - Y_THICKNESS), + ],fill = BLANK_COLOR) # Draw color on the outside of the board - for point in boardCoordinates[0]: - "hi nikolaj" + # Top line, red + d.line(sum((sum([(point[0],point[1],point[0]+HEXAGONWIDTH/2,point[1]-HEXAGONHEIGHT+SIDELENGTH) for point in boardCoordinates[0]],()),(boardCoordinates[0][10][0]+HEXAGONWIDTH*3/4,boardCoordinates[0][10][1]-SIDELENGTH/4)),()), + fill = PIECECOLOR[1],width = LINETHICKNESS) + # Bottom line, red + d.line(sum(((boardCoordinates[10][0][0]+HEXAGONWIDTH/4,boardCoordinates[10][0][1]+SIDELENGTH*5/4),sum([(point[0]+HEXAGONWIDTH/2,point[1]+HEXAGONHEIGHT,point[0]+HEXAGONWIDTH,point[1]+SIDELENGTH) for point in boardCoordinates[10]],())),()), + fill = PIECECOLOR[1],width = LINETHICKNESS) + # Left line, blue + d.line(sum((sum([(row[0][0],row[0][1],row[0][0],row[0][1]+SIDELENGTH) for row in boardCoordinates],()),(boardCoordinates[10][0][0]+HEXAGONWIDTH/4,boardCoordinates[10][0][1]+SIDELENGTH*5/4)),()), + fill = PIECECOLOR[2],width = LINETHICKNESS) + # Right line, blue + d.line(sum(((boardCoordinates[0][10][0]+HEXAGONWIDTH*3/4,boardCoordinates[0][10][1]-SIDELENGTH/4),sum([(row[10][0]+HEXAGONWIDTH,row[10][1],row[10][0]+HEXAGONWIDTH,row[10][1]+SIDELENGTH) for row in boardCoordinates],())),()), + fill = PIECECOLOR[2],width = LINETHICKNESS) # Writes "abc..", "123.." on the columns and rows for i in range(11): @@ -68,57 +83,9 @@ def drawBoard(channel): # 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: + # Write player names and color + """ + with open("resources/games/hexGames.json", "r") as f: data = json.load(f) board = data[channel]["board"] @@ -132,6 +99,73 @@ with open("resources/games/hexGames.json", "r") as f: player2 = "Gwendolyn" else: player2 = getName(players[1]) +""" + #im.save("board"+channel+".png") + im.save("resources/games/hexBoards/board"+channel+".png") -""" \ No newline at end of file + + +def drawHexPlacement(channel,player,position): + # Same variables as above + 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:(237,41,57),2:(0,165,255)} # player1 is red, player2 is blue + #PIECEDIAMETER = 30 + HEXTHICKNESS = 4 + X_THICKNESS = HEXTHICKNESS * math.cos(math.pi/6) + Y_THICKNESS = HEXTHICKNESS * math.sin(math.pi/6) + + boardCoordinates = [ [(X_OFFSET + HEXAGONWIDTH*(column + row/2),Y_OFFSET + HEXAGONHEIGHT*row) for column in range(11)] for row in range(11)] + + # 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) """ + # Find the coordinates for the filled hex drawing + hexCoords = [ + (boardCoordinates[row][column][0]+X_THICKNESS, boardCoordinates[row][column][1] + Y_THICKNESS), + (boardCoordinates[row][column][0]+HEXAGONWIDTH/2, boardCoordinates[row][column][1]-0.5*SIDELENGTH + HEXTHICKNESS), + (boardCoordinates[row][column][0]+HEXAGONWIDTH-X_THICKNESS, boardCoordinates[row][column][1] + Y_THICKNESS), + (boardCoordinates[row][column][0]+HEXAGONWIDTH-X_THICKNESS, boardCoordinates[row][column][1]+SIDELENGTH - Y_THICKNESS), + (boardCoordinates[row][column][0]+HEXAGONWIDTH/2, boardCoordinates[row][column][1]+1.5*SIDELENGTH - HEXTHICKNESS), + (boardCoordinates[row][column][0]+X_THICKNESS, boardCoordinates[row][column][1]+SIDELENGTH - Y_THICKNESS), + ] + + # Opens the image + #logThis("Finding and opening the board-image at hex"+channel+".png") + try: + with Image.open("resources/games/hexBoards/board"+channel+".png") as im: + d = ImageDraw.Draw(im,"RGBA") + # Draws the piece + d.polygon(hexCoords,fill = PIECECOLOR[player]) + #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("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") diff --git a/funcs/miscFuncs.py b/funcs/miscFuncs.py index bf223ed..ce4233a 100644 --- a/funcs/miscFuncs.py +++ b/funcs/miscFuncs.py @@ -248,6 +248,10 @@ def stopServer(): with open("resources/games/monopolyGames.json", "w") as f: json.dump(emptyDict,f,indent=4) + + with open("resources/games/hexGames.json", "w") as f: + json.dump(emptyDict,f,indent=4) + def deleteGame(gameType,channel): with open("resources/games/games.json", "r") as f: diff --git a/gameLoops.py b/gameLoops.py index ac400be..f27b976 100644 --- a/gameLoops.py +++ b/gameLoops.py @@ -64,8 +64,6 @@ async def runHex(channel,command,user): winner = data["hex games"][str(channel.id)]["winner"] if winner != 0: addMoney(data["hex games"][str(channel.id)]["players"][winner-1].lower(),20) - with open("resources/games/games.json", "r") as f: - data = json.load(f) #why is this here? deleteGame("hex games",str(channel.id)) diff --git a/resources/games/hexBoards/board739463392431177832.png b/resources/games/hexBoards/board739463392431177832.png new file mode 100644 index 0000000..f10fd8f Binary files /dev/null and b/resources/games/hexBoards/board739463392431177832.png differ