✨ hangman
This commit is contained in:
@ -1,22 +1,53 @@
|
||||
import json
|
||||
import requests
|
||||
import datetime
|
||||
import string
|
||||
import discord
|
||||
import math
|
||||
import random
|
||||
"""
|
||||
Deals with commands and logic for hangman games.
|
||||
|
||||
from discord_slash.context import SlashContext
|
||||
from PIL import ImageDraw, Image, ImageFont
|
||||
*Classes*
|
||||
---------
|
||||
Hangman()
|
||||
Deals with the game logic of hangman.
|
||||
DrawHangman()
|
||||
Draws the image shown to the player.
|
||||
"""
|
||||
import requests # Used for getting the word in Hangman.start()
|
||||
import datetime # Used for generating the game id
|
||||
import string # string.ascii_uppercase used
|
||||
import discord # Used for discord.file and type hints
|
||||
import math # Used by DrawHangman(), mainly for drawing circles
|
||||
import random # Used to draw poorly
|
||||
|
||||
from discord_slash.context import SlashContext # Used for typehints
|
||||
from PIL import ImageDraw, Image, ImageFont # Used to draw the image
|
||||
|
||||
|
||||
class Hangman():
|
||||
"""
|
||||
Controls hangman commands and game logic.
|
||||
|
||||
*Methods*
|
||||
---------
|
||||
start(ctx: SlashContext)
|
||||
stop(ctx: SlashContext)
|
||||
guess(message: discord.message, user: str, guess: str)
|
||||
"""
|
||||
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
self.draw = DrawHangman(bot)
|
||||
self.APIURL = "https://api.wordnik.com/v4/words.json/randomWords?"
|
||||
apiKey = self.bot.credentials.wordnikKey
|
||||
self.APIPARAMS = {
|
||||
"""
|
||||
Initialize the class.
|
||||
|
||||
*Attributes*
|
||||
------------
|
||||
draw: DrawHangman
|
||||
The DrawHangman used to draw the hangman image.
|
||||
APIURL: str
|
||||
The url to get the words from.
|
||||
APIPARAMS: dict
|
||||
The parameters to pass to every api call.
|
||||
"""
|
||||
self.__bot = bot
|
||||
self.__draw = DrawHangman(bot)
|
||||
self.__APIURL = "https://api.wordnik.com/v4/words.json/randomWords?"
|
||||
apiKey = self.__bot.credentials.wordnikKey
|
||||
self.__APIPARAMS = {
|
||||
"hasDictionaryDef": True,
|
||||
"minCorpusCount": 5000,
|
||||
"maxCorpusCount": -1,
|
||||
@ -29,20 +60,28 @@ class Hangman():
|
||||
}
|
||||
|
||||
async def start(self, ctx: SlashContext):
|
||||
await self.bot.defer(ctx)
|
||||
"""
|
||||
Start a game of hangman.
|
||||
|
||||
*Parameters*
|
||||
------------
|
||||
ctx: SlashContext
|
||||
The context of the command.
|
||||
"""
|
||||
await self.__bot.defer(ctx)
|
||||
channel = str(ctx.channel_id)
|
||||
user = f"#{ctx.author.id}"
|
||||
game = self.bot.database["hangman games"].find_one({"_id": channel})
|
||||
userName = self.bot.databaseFuncs.getName(user)
|
||||
game = self.__bot.database["hangman games"].find_one({"_id": channel})
|
||||
userName = self.__bot.databaseFuncs.getName(user)
|
||||
startedGame = False
|
||||
|
||||
if game is None:
|
||||
word = "-"
|
||||
while "-" in word or "." in word:
|
||||
response = requests.get(self.APIURL, params=self.APIPARAMS)
|
||||
response = requests.get(self.__APIURL, params=self.__APIPARAMS)
|
||||
word = list(response.json()[0]["word"].upper())
|
||||
|
||||
self.bot.log("Found the word \""+"".join(word)+"\"")
|
||||
self.__bot.log("Found the word \""+"".join(word)+"\"")
|
||||
guessed = [False] * len(word)
|
||||
gameID = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
newGame = {
|
||||
@ -54,20 +93,20 @@ class Hangman():
|
||||
"misses": 0,
|
||||
"guessed": guessed
|
||||
}
|
||||
self.bot.database["hangman games"].insert_one(newGame)
|
||||
self.__bot.database["hangman games"].insert_one(newGame)
|
||||
|
||||
remainingLetters = list(string.ascii_uppercase)
|
||||
|
||||
self.draw.drawImage(channel)
|
||||
self.__draw.drawImage(channel)
|
||||
|
||||
logMessage = "Game started"
|
||||
sendMessage = f"{userName} started game of hangman."
|
||||
startedGame = True
|
||||
else:
|
||||
logMessage = "There was already a game going on"
|
||||
sendMessage = self.bot.longStrings["Hangman going on"]
|
||||
sendMessage = self.__bot.longStrings["Hangman going on"]
|
||||
|
||||
self.bot.log(logMessage)
|
||||
self.__bot.log(logMessage)
|
||||
await ctx.send(sendMessage)
|
||||
|
||||
if startedGame:
|
||||
@ -92,29 +131,49 @@ class Hangman():
|
||||
await message.add_reaction(emoji)
|
||||
|
||||
async def stop(self, ctx: SlashContext):
|
||||
"""
|
||||
Stop the game of hangman.
|
||||
|
||||
*Parameters*
|
||||
------------
|
||||
ctx: SlashContext
|
||||
The context of the command.
|
||||
"""
|
||||
channel = str(ctx.channel.id)
|
||||
game = self.bot.database["hangman games"].find_one({"_id": channel})
|
||||
game = self.__bot.database["hangman games"].find_one({"_id": channel})
|
||||
|
||||
if game is None:
|
||||
await ctx.send("There's no game going on")
|
||||
elif f"#{ctx.author.id}" != game["player"]:
|
||||
await ctx.send("You can't end a game you're not in")
|
||||
else:
|
||||
self.bot.database["hangman games"].delete_one({"_id": channel})
|
||||
self.__bot.database["hangman games"].delete_one({"_id": channel})
|
||||
|
||||
with open(f"resources/games/oldImages/hangman{channel}", "r") as f:
|
||||
messages = f.read().splitlines()
|
||||
|
||||
for message in messages:
|
||||
oldMessage = await ctx.channel.fetch_message(int(message))
|
||||
self.bot.log("Deleting old message")
|
||||
self.__bot.log("Deleting old message")
|
||||
await oldMessage.delete()
|
||||
|
||||
await ctx.send("Game stopped")
|
||||
|
||||
async def guess(self, message: discord.Message, user: str, guess: str):
|
||||
"""
|
||||
Guess a letter.
|
||||
|
||||
*Parameters*
|
||||
------------
|
||||
message: discord.Message
|
||||
The message that the reaction was placed on.
|
||||
user: str
|
||||
The id of the user.
|
||||
guess: str
|
||||
The guess.
|
||||
"""
|
||||
channel = str(message.channel.id)
|
||||
hangmanGames = self.bot.database["hangman games"]
|
||||
hangmanGames = self.__bot.database["hangman games"]
|
||||
game = hangmanGames.find_one({"_id": channel})
|
||||
|
||||
gameExists = (game is not None)
|
||||
@ -123,7 +182,7 @@ class Hangman():
|
||||
validGuess = (gameExists and singleLetter and newGuess)
|
||||
|
||||
if validGuess:
|
||||
self.bot.log("Guessed the letter")
|
||||
self.__bot.log("Guessed the letter")
|
||||
correctGuess = 0
|
||||
|
||||
for x, letter in enumerate(game["word"]):
|
||||
@ -153,16 +212,16 @@ class Hangman():
|
||||
sendMessage = "Guessed {}. There were {} {}s in the word."
|
||||
sendMessage = sendMessage.format(guess, correctGuess, guess)
|
||||
|
||||
self.draw.drawImage(channel)
|
||||
self.__draw.drawImage(channel)
|
||||
|
||||
if game["misses"] == 6:
|
||||
hangmanGames.delete_one({"_id": channel})
|
||||
sendMessage += self.bot.longStrings["Hangman lost game"]
|
||||
sendMessage += self.__bot.longStrings["Hangman lost game"]
|
||||
remainingLetters = []
|
||||
elif all(game["guessed"]):
|
||||
hangmanGames.delete_one({"_id": channel})
|
||||
self.bot.money.addMoney(user, 15)
|
||||
sendMessage += self.bot.longStrings["Hangman guessed word"]
|
||||
self.__bot.money.addMoney(user, 15)
|
||||
sendMessage += self.__bot.longStrings["Hangman guessed word"]
|
||||
remainingLetters = []
|
||||
|
||||
await message.channel.send(sendMessage)
|
||||
@ -172,7 +231,7 @@ class Hangman():
|
||||
|
||||
for oldID in oldMessageIDs:
|
||||
oldMessage = await message.channel.fetch_message(int(oldID))
|
||||
self.bot.log("Deleting old message")
|
||||
self.__bot.log("Deleting old message")
|
||||
await oldMessage.delete()
|
||||
|
||||
boardsPath = "resources/games/hangmanBoards/"
|
||||
@ -206,34 +265,63 @@ class Hangman():
|
||||
|
||||
|
||||
class DrawHangman():
|
||||
"""
|
||||
Draws the image of the hangman game.
|
||||
|
||||
*Methods*
|
||||
---------
|
||||
drawImage(channel: str)
|
||||
"""
|
||||
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
self.CIRCLESIZE = 120
|
||||
self.LINEWIDTH = 12
|
||||
"""
|
||||
Initialize the class.
|
||||
|
||||
self.BODYSIZE = 210
|
||||
self.LIMBSIZE = 60
|
||||
self.ARMPOSITION = 60
|
||||
*Attributes*
|
||||
------------
|
||||
CIRCLESIZE
|
||||
LINEWIDTH
|
||||
BODYSIZE
|
||||
LIMBSIZE
|
||||
ARMPOSITION
|
||||
MANX, MANY
|
||||
LETTERLINELENGTH
|
||||
LETTERLINEDISTANCE
|
||||
GALLOWX, GALLOWY
|
||||
PHI
|
||||
FONT
|
||||
SMALLFONT
|
||||
"""
|
||||
self.__bot = bot
|
||||
self.__CIRCLESIZE = 120
|
||||
self.__LINEWIDTH = 12
|
||||
|
||||
MANPADDING = self.LINEWIDTH*4
|
||||
self.MANX = (self.LIMBSIZE*2)+MANPADDING
|
||||
self.MANY = (self.CIRCLESIZE+self.BODYSIZE+self.LIMBSIZE)+MANPADDING
|
||||
self.__BODYSIZE = 210
|
||||
self.__LIMBSIZE = 60
|
||||
self.__ARMPOSITION = 60
|
||||
|
||||
self.LETTERLINELENGTH = 90
|
||||
self.LETTERLINEDISTANCE = 30
|
||||
self.__MANX = (self.__LIMBSIZE*2)
|
||||
self.__MANY = (self.__CIRCLESIZE+self.__BODYSIZE+self.__LIMBSIZE)
|
||||
MANPADDING = self.__LINEWIDTH*4
|
||||
self.__MANX += MANPADDING
|
||||
self.__MANY += MANPADDING
|
||||
|
||||
self.GALLOWX, self.GALLOWY = 360, 600
|
||||
self.GOLDENRATIO = 1-(1 / ((1 + 5 ** 0.5) / 2))
|
||||
self.__LETTERLINELENGTH = 90
|
||||
self.__LETTERLINEDISTANCE = 30
|
||||
|
||||
LETTERSIZE = 75
|
||||
TEXTSIZE = 70
|
||||
self.__GALLOWX, self.__GALLOWY = 360, 600
|
||||
self.__PHI = 1-(1 / ((1 + 5 ** 0.5) / 2))
|
||||
|
||||
LETTERSIZE = 75 # Wrong guesses letter size
|
||||
WORDSIZE = 70 # Correct guesses letter size
|
||||
|
||||
FONTPATH = "resources/fonts/comic-sans-bold.ttf"
|
||||
self.FONT = ImageFont.truetype(FONTPATH, LETTERSIZE)
|
||||
self.SMALLFONT = ImageFont.truetype(FONTPATH, TEXTSIZE)
|
||||
self.__FONT = ImageFont.truetype(FONTPATH, LETTERSIZE)
|
||||
self.__SMALLFONT = ImageFont.truetype(FONTPATH, WORDSIZE)
|
||||
|
||||
def calcDeviance(self, preDeviance, preDevianceAccuracy, positionChange,
|
||||
maxmin, maxAcceleration):
|
||||
def __deviate(self, preDeviance: int, preDevianceAccuracy: int,
|
||||
positionChange: float, maxmin: int,
|
||||
maxAcceleration: float):
|
||||
randomDeviance = random.uniform(-positionChange, positionChange)
|
||||
devianceAccuracy = preDevianceAccuracy + randomDeviance
|
||||
if devianceAccuracy > maxmin * maxAcceleration:
|
||||
@ -248,14 +336,14 @@ class DrawHangman():
|
||||
deviance = -maxmin
|
||||
return deviance, devianceAccuracy
|
||||
|
||||
def badCircle(self):
|
||||
circlePadding = (self.LINEWIDTH*3)
|
||||
imageWidth = self.CIRCLESIZE+circlePadding
|
||||
def __badCircle(self):
|
||||
circlePadding = (self.__LINEWIDTH*3)
|
||||
imageWidth = self.__CIRCLESIZE+circlePadding
|
||||
imageSize = (imageWidth, imageWidth)
|
||||
background = Image.new("RGBA", imageSize, color=(0, 0, 0, 0))
|
||||
|
||||
d = ImageDraw.Draw(background, "RGBA")
|
||||
middle = (self.CIRCLESIZE+(self.LINEWIDTH*3))/2
|
||||
middle = (self.__CIRCLESIZE+(self.__LINEWIDTH*3))/2
|
||||
devianceX = 0
|
||||
devianceY = 0
|
||||
devianceAccuracyX = 0
|
||||
@ -267,42 +355,42 @@ class DrawHangman():
|
||||
devianceXParams = [
|
||||
devianceX,
|
||||
devianceAccuracyX,
|
||||
self.LINEWIDTH/100,
|
||||
self.LINEWIDTH,
|
||||
self.__LINEWIDTH/100,
|
||||
self.__LINEWIDTH,
|
||||
0.03
|
||||
]
|
||||
devianceYParams = [
|
||||
devianceY,
|
||||
devianceAccuracyY,
|
||||
self.LINEWIDTH/100,
|
||||
self.LINEWIDTH,
|
||||
self.__LINEWIDTH/100,
|
||||
self.__LINEWIDTH,
|
||||
0.03
|
||||
]
|
||||
devianceX, devianceAccuracyX = self.calcDeviance(*devianceXParams)
|
||||
devianceY, devianceAccuracyY = self.calcDeviance(*devianceYParams)
|
||||
devianceX, devianceAccuracyX = self.__deviate(*devianceXParams)
|
||||
devianceY, devianceAccuracyY = self.__deviate(*devianceYParams)
|
||||
|
||||
radians = math.radians(degree+start)
|
||||
circleX = (math.cos(radians) * (self.CIRCLESIZE/2))
|
||||
circleY = (math.sin(radians) * (self.CIRCLESIZE/2))
|
||||
circleX = (math.cos(radians) * (self.__CIRCLESIZE/2))
|
||||
circleY = (math.sin(radians) * (self.__CIRCLESIZE/2))
|
||||
|
||||
x = middle + circleX - (self.LINEWIDTH/2) + devianceX
|
||||
y = middle + circleY - (self.LINEWIDTH/2) + devianceY
|
||||
x = middle + circleX - (self.__LINEWIDTH/2) + devianceX
|
||||
y = middle + circleY - (self.__LINEWIDTH/2) + devianceY
|
||||
|
||||
circlePosition = [(x, y), (x+self.LINEWIDTH, y+self.LINEWIDTH)]
|
||||
circlePosition = [(x, y), (x+self.__LINEWIDTH, y+self.__LINEWIDTH)]
|
||||
d.ellipse(circlePosition, fill=(0, 0, 0, 255))
|
||||
|
||||
return background
|
||||
|
||||
def badLine(self, length: int, rotated: bool = False):
|
||||
def __badLine(self, length: int, rotated: bool = False):
|
||||
if rotated:
|
||||
w, h = length+self.LINEWIDTH*3, self.LINEWIDTH*3
|
||||
w, h = length+self.__LINEWIDTH*3, self.__LINEWIDTH*3
|
||||
else:
|
||||
w, h = self.LINEWIDTH*3, length+self.LINEWIDTH*3
|
||||
w, h = self.__LINEWIDTH*3, length+self.__LINEWIDTH*3
|
||||
background = Image.new("RGBA", (w, h), color=(0, 0, 0, 0))
|
||||
|
||||
d = ImageDraw.Draw(background, "RGBA")
|
||||
|
||||
possibleDeviance = int(self.LINEWIDTH/3)
|
||||
possibleDeviance = int(self.__LINEWIDTH/3)
|
||||
devianceX = random.randint(-possibleDeviance, possibleDeviance)
|
||||
devianceY = 0
|
||||
devianceAccuracyX = 0
|
||||
@ -312,45 +400,46 @@ class DrawHangman():
|
||||
devianceParamsX = [
|
||||
devianceX,
|
||||
devianceAccuracyX,
|
||||
self.LINEWIDTH/1000,
|
||||
self.LINEWIDTH,
|
||||
self.__LINEWIDTH/1000,
|
||||
self.__LINEWIDTH,
|
||||
0.004
|
||||
]
|
||||
devianceParamsY = [
|
||||
devianceY,
|
||||
devianceAccuracyY,
|
||||
self.LINEWIDTH/1000,
|
||||
self.LINEWIDTH,
|
||||
self.__LINEWIDTH/1000,
|
||||
self.__LINEWIDTH,
|
||||
0.004
|
||||
]
|
||||
devianceX, devianceAccuracyX = self.calcDeviance(*devianceParamsX)
|
||||
devianceY, devianceAccuracyY = self.calcDeviance(*devianceParamsY)
|
||||
devianceX, devianceAccuracyX = self.__deviate(*devianceParamsX)
|
||||
devianceY, devianceAccuracyY = self.__deviate(*devianceParamsY)
|
||||
|
||||
if rotated:
|
||||
x = self.LINEWIDTH + pixel + devianceX
|
||||
y = self.LINEWIDTH + devianceY
|
||||
x = self.__LINEWIDTH + pixel + devianceX
|
||||
y = self.__LINEWIDTH + devianceY
|
||||
else:
|
||||
x = self.LINEWIDTH + devianceX
|
||||
y = self.LINEWIDTH + pixel + devianceY
|
||||
x = self.__LINEWIDTH + devianceX
|
||||
y = self.__LINEWIDTH + pixel + devianceY
|
||||
|
||||
circlePosition = [(x, y), (x+self.LINEWIDTH, y+self.LINEWIDTH)]
|
||||
circlePosition = [(x, y), (x+self.__LINEWIDTH, y+self.__LINEWIDTH)]
|
||||
d.ellipse(circlePosition, fill=(0, 0, 0, 255))
|
||||
|
||||
return background
|
||||
|
||||
def drawMan(self, misses: int):
|
||||
manSize = (self.MANX, self.MANY)
|
||||
def __drawMan(self, misses: int, seed: str):
|
||||
random.seed(seed)
|
||||
manSize = (self.__MANX, self.__MANY)
|
||||
background = Image.new("RGBA", manSize, color=(0, 0, 0, 0))
|
||||
|
||||
if misses >= 1:
|
||||
head = self.badCircle()
|
||||
pasteX = (self.MANX-(self.CIRCLESIZE+(self.LINEWIDTH*3)))//2
|
||||
head = self.__badCircle()
|
||||
pasteX = (self.__MANX-(self.__CIRCLESIZE+(self.__LINEWIDTH*3)))//2
|
||||
pastePosition = (pasteX, 0)
|
||||
background.paste(head, pastePosition, head)
|
||||
if misses >= 2:
|
||||
body = self.badLine(self.BODYSIZE)
|
||||
pasteX = (self.MANX-(self.LINEWIDTH*3))//2
|
||||
pastePosition = (pasteX, self.CIRCLESIZE)
|
||||
body = self.__badLine(self.__BODYSIZE)
|
||||
pasteX = (self.__MANX-(self.__LINEWIDTH*3))//2
|
||||
pastePosition = (pasteX, self.__CIRCLESIZE)
|
||||
background.paste(body, pastePosition, body)
|
||||
|
||||
if misses >= 3:
|
||||
@ -358,30 +447,33 @@ class DrawHangman():
|
||||
else:
|
||||
limbs = []
|
||||
|
||||
random.seed(seed)
|
||||
|
||||
for limb in limbs:
|
||||
limbDrawing = self.badLine(self.LIMBSIZE, True)
|
||||
xPosition = (self.MANX-(self.LINEWIDTH*3))//2
|
||||
limbDrawing = self.__badLine(self.__LIMBSIZE, True)
|
||||
xPosition = (self.__MANX-(self.__LINEWIDTH*3))//2
|
||||
|
||||
if limb[1] == "a":
|
||||
rotation = random.randint(-45, 45)
|
||||
shift = math.sin(math.radians(rotation))
|
||||
compensation = int(shift*(self.LIMBSIZE+(self.LINEWIDTH*3)))
|
||||
lineLength = self.__LIMBSIZE+(self.__LINEWIDTH*3)
|
||||
compensation = int(shift*lineLength)
|
||||
limbDrawing = limbDrawing.rotate(rotation, expand=1)
|
||||
yPosition = self.CIRCLESIZE + self.ARMPOSITION + compensation
|
||||
yPosition = self.__CIRCLESIZE + self.__ARMPOSITION
|
||||
if limb == "ra":
|
||||
compensation = min(-compensation, 0)
|
||||
else:
|
||||
xPosition -= self.LIMBSIZE
|
||||
xPosition -= self.__LIMBSIZE
|
||||
compensation = min(compensation, 0)
|
||||
|
||||
yPosition += compensation
|
||||
else:
|
||||
rotation = random.randint(-15, 15)
|
||||
yPosition = self.CIRCLESIZE+self.BODYSIZE
|
||||
yPosition = self.__CIRCLESIZE+self.__BODYSIZE-self.__LINEWIDTH
|
||||
if limb == "rl":
|
||||
xPosition -= self.LINEWIDTH
|
||||
limbDrawing = limbDrawing.rotate(rotation-45, expand=1)
|
||||
else:
|
||||
yPosition -= self.LINEWIDTH
|
||||
xPosition += -limbDrawing.size[0]+self.LINEWIDTH*3
|
||||
xPosition += -limbDrawing.size[0]+self.__LINEWIDTH*3
|
||||
limbDrawing = limbDrawing.rotate(rotation+45, expand=1)
|
||||
|
||||
pastePosition = (xPosition, yPosition)
|
||||
@ -389,11 +481,11 @@ class DrawHangman():
|
||||
|
||||
return background
|
||||
|
||||
def badText(self, text: str, big: bool, color: tuple = (0, 0, 0, 255)):
|
||||
def __badText(self, text: str, big: bool, color: tuple = (0, 0, 0, 255)):
|
||||
if big:
|
||||
font = self.FONT
|
||||
font = self.__FONT
|
||||
else:
|
||||
font = self.SMALLFONT
|
||||
font = self.__SMALLFONT
|
||||
w, h = font.getsize(text)
|
||||
img = Image.new("RGBA", (w, h), color=(0, 0, 0, 0))
|
||||
d = ImageDraw.Draw(img, "RGBA")
|
||||
@ -401,109 +493,118 @@ class DrawHangman():
|
||||
d.text((0, 0), text, font=font, fill=color)
|
||||
return img
|
||||
|
||||
def drawGallows(self):
|
||||
gallowSize = (self.GALLOWX, self.GALLOWY)
|
||||
def __drawGallows(self):
|
||||
gallowSize = (self.__GALLOWX, self.__GALLOWY)
|
||||
background = Image.new("RGBA", gallowSize, color=(0, 0, 0, 0))
|
||||
|
||||
bottomLine = self.badLine(int(self.GALLOWX * 0.75), True)
|
||||
bottomLineX = int(self.GALLOWX * 0.125)
|
||||
bottomLineY = self.GALLOWY-(self.LINEWIDTH*4)
|
||||
bottomLine = self.__badLine(int(self.__GALLOWX * 0.75), True)
|
||||
bottomLineX = int(self.__GALLOWX * 0.125)
|
||||
bottomLineY = self.__GALLOWY-(self.__LINEWIDTH*4)
|
||||
pastePosition = (bottomLineX, bottomLineY)
|
||||
background.paste(bottomLine, pastePosition, bottomLine)
|
||||
|
||||
lineTwo = self.badLine(self.GALLOWY-self.LINEWIDTH*6)
|
||||
lineTwoX = int(self.GALLOWX*(0.75*self.GOLDENRATIO))
|
||||
lineTwoY = self.LINEWIDTH*2
|
||||
lineTwo = self.__badLine(self.__GALLOWY-self.__LINEWIDTH*6)
|
||||
lineTwoX = int(self.__GALLOWX*(0.75*self.__PHI))
|
||||
lineTwoY = self.__LINEWIDTH*2
|
||||
pastePosition = (lineTwoX, lineTwoY)
|
||||
background.paste(lineTwo, pastePosition, lineTwo)
|
||||
|
||||
topLine = self.badLine(int(self.GALLOWY*0.30), True)
|
||||
pasteX = int(self.GALLOWX*(0.75*self.GOLDENRATIO))-self.LINEWIDTH
|
||||
pastePosition = (pasteX, self.LINEWIDTH*3)
|
||||
topLine = self.__badLine(int(self.__GALLOWY*0.30), True)
|
||||
pasteX = int(self.__GALLOWX*(0.75*self.__PHI))-self.__LINEWIDTH
|
||||
pastePosition = (pasteX, self.__LINEWIDTH*3)
|
||||
background.paste(topLine, pastePosition, topLine)
|
||||
|
||||
lastLine = self.badLine(int(self.GALLOWY*0.125))
|
||||
pasteX += int(self.GALLOWY*0.30)
|
||||
background.paste(lastLine, (pasteX, self.LINEWIDTH*3), lastLine)
|
||||
lastLine = self.__badLine(int(self.__GALLOWY*0.125))
|
||||
pasteX += int(self.__GALLOWY*0.30)
|
||||
background.paste(lastLine, (pasteX, self.__LINEWIDTH*3), lastLine)
|
||||
return background
|
||||
|
||||
def drawLetterLines(self, word: str, guessed: list, misses: int):
|
||||
imageWidth = (self.LETTERLINELENGTH+self.LETTERLINEDISTANCE)*len(word)
|
||||
imageSize = (imageWidth, self.LETTERLINELENGTH+self.LINEWIDTH*3)
|
||||
def __drawLetterLines(self, word: str, guessed: list, misses: int):
|
||||
letterWidth = self.__LETTERLINELENGTH+self.__LETTERLINEDISTANCE
|
||||
imageWidth = letterWidth*len(word)
|
||||
imageSize = (imageWidth, self.__LETTERLINELENGTH+self.__LINEWIDTH*3)
|
||||
letterLines = Image.new("RGBA", imageSize, color=(0, 0, 0, 0))
|
||||
for x, letter in enumerate(word):
|
||||
line = self.badLine(self.LETTERLINELENGTH, True)
|
||||
pasteX = x*(self.LETTERLINELENGTH+self.LETTERLINEDISTANCE)
|
||||
pastePosition = (pasteX, self.LETTERLINELENGTH)
|
||||
line = self.__badLine(self.__LETTERLINELENGTH, True)
|
||||
pasteX = x*(self.__LETTERLINELENGTH+self.__LETTERLINEDISTANCE)
|
||||
pastePosition = (pasteX, self.__LETTERLINELENGTH)
|
||||
letterLines.paste(line, pastePosition, line)
|
||||
if guessed[x]:
|
||||
letterDrawing = self.badText(letter, True)
|
||||
letterWidth = self.FONT.getsize(letter)[0]
|
||||
letterX = x*(self.LETTERLINELENGTH+self.LETTERLINEDISTANCE)
|
||||
letterDrawing = self.__badText(letter, True)
|
||||
letterWidth = self.__FONT.getsize(letter)[0]
|
||||
letterX = x*(self.__LETTERLINELENGTH+self.__LETTERLINEDISTANCE)
|
||||
letterX -= (letterWidth//2)
|
||||
letterX += (self.LETTERLINELENGTH//2)+(self.LINEWIDTH*2)
|
||||
letterX += (self.__LETTERLINELENGTH//2)+(self.__LINEWIDTH*2)
|
||||
letterLines.paste(letterDrawing, (letterX, 0), letterDrawing)
|
||||
elif misses == 6:
|
||||
letterDrawing = self.badText(letter, True, (242, 66, 54))
|
||||
letterWidth = self.FONT.getsize(letter)[0]
|
||||
letterX = x*(self.LETTERLINELENGTH+self.LETTERLINEDISTANCE)
|
||||
letterDrawing = self.__badText(letter, True, (242, 66, 54))
|
||||
letterWidth = self.__FONT.getsize(letter)[0]
|
||||
letterX = x*(self.__LETTERLINELENGTH+self.__LETTERLINEDISTANCE)
|
||||
letterX -= (letterWidth//2)
|
||||
letterX += (self.LETTERLINELENGTH//2)+(self.LINEWIDTH*2)
|
||||
letterX += (self.__LETTERLINELENGTH//2)+(self.__LINEWIDTH*2)
|
||||
letterLines.paste(letterDrawing, (letterX, 0), letterDrawing)
|
||||
|
||||
return letterLines
|
||||
|
||||
def shortestDist(self, positions: list, newPosition: tuple):
|
||||
shortestDist = math.inf
|
||||
def __shortestDist(self, positions: list, newPosition: tuple):
|
||||
__shortestDist = math.inf
|
||||
x, y = newPosition
|
||||
for i, j in positions:
|
||||
xDistance = abs(i-x)
|
||||
yDistance = abs(j-y)
|
||||
dist = math.sqrt(xDistance**2+yDistance**2)
|
||||
if shortestDist > dist:
|
||||
shortestDist = dist
|
||||
return shortestDist
|
||||
if __shortestDist > dist:
|
||||
__shortestDist = dist
|
||||
return __shortestDist
|
||||
|
||||
def drawMisses(self, guesses: list, word: str):
|
||||
def __drawMisses(self, guesses: list, word: str):
|
||||
background = Image.new("RGBA", (600, 400), color=(0, 0, 0, 0))
|
||||
pos = []
|
||||
for guess in guesses:
|
||||
if guess not in word:
|
||||
placed = False
|
||||
while not placed:
|
||||
letter = self.badText(guess, True)
|
||||
w, h = self.FONT.getsize(guess)
|
||||
letter = self.__badText(guess, True)
|
||||
w, h = self.__FONT.getsize(guess)
|
||||
x = random.randint(0, 600-w)
|
||||
y = random.randint(0, 400-h)
|
||||
if self.shortestDist(pos, (x, y)) > 70:
|
||||
if self.__shortestDist(pos, (x, y)) > 70:
|
||||
pos.append((x, y))
|
||||
background.paste(letter, (x, y), letter)
|
||||
placed = True
|
||||
return background
|
||||
|
||||
def drawImage(self, channel: str):
|
||||
self.bot.log("Drawing hangman image", channel)
|
||||
game = self.bot.database["hangman games"].find_one({"_id": channel})
|
||||
"""
|
||||
Draw a hangman Image.
|
||||
|
||||
*Parameters*
|
||||
------------
|
||||
channel: str
|
||||
The id of the channel the game is in.
|
||||
"""
|
||||
self.__bot.log("Drawing hangman image", channel)
|
||||
game = self.__bot.database["hangman games"].find_one({"_id": channel})
|
||||
|
||||
random.seed(game["game ID"])
|
||||
|
||||
background = Image.open("resources/paper.jpg")
|
||||
gallow = self.drawGallows()
|
||||
man = self.drawMan(game["misses"])
|
||||
gallow = self.__drawGallows()
|
||||
man = self.__drawMan(game["misses"], game["game ID"])
|
||||
|
||||
random.seed(game["game ID"])
|
||||
letterLineParams = [game["word"], game["guessed"], game["misses"]]
|
||||
letterLines = self.drawLetterLines(*letterLineParams)
|
||||
letterLines = self.__drawLetterLines(*letterLineParams)
|
||||
|
||||
random.seed(game["game ID"])
|
||||
misses = self.drawMisses(game["guessed letters"], game["word"])
|
||||
misses = self.__drawMisses(game["guessed letters"], game["word"])
|
||||
|
||||
background.paste(gallow, (100, 100), gallow)
|
||||
background.paste(man, (300, 210), man)
|
||||
background.paste(letterLines, (120, 840), letterLines)
|
||||
background.paste(misses, (600, 150), misses)
|
||||
|
||||
missesText = self.badText("MISSES", False)
|
||||
missesText = self.__badText("MISSES", False)
|
||||
missesTextWidth = missesText.size[0]
|
||||
background.paste(missesText, (850-missesTextWidth//2, 50), missesText)
|
||||
|
||||
|
Reference in New Issue
Block a user