:Sparkles: Hangman
This commit is contained in:
@ -281,29 +281,50 @@ class DrawHangman():
|
|||||||
devianceX, devianceAccuracyX = self.calcDeviance(*devianceXParams)
|
devianceX, devianceAccuracyX = self.calcDeviance(*devianceXParams)
|
||||||
devianceY, devianceAccuracyY = self.calcDeviance(*devianceYParams)
|
devianceY, devianceAccuracyY = self.calcDeviance(*devianceYParams)
|
||||||
|
|
||||||
x = middle + (math.cos(math.radians(degree+start)) * (self.CIRCLESIZE/2)) - (self.LINEWIDTH/2) + devianceX
|
radians = math.radians(degree+start)
|
||||||
y = middle + (math.sin(math.radians(degree+start)) * (self.CIRCLESIZE/2)) - (self.LINEWIDTH/2) + devianceY
|
circleX = (math.cos(radians) * (self.CIRCLESIZE/2))
|
||||||
|
circleY = (math.sin(radians) * (self.CIRCLESIZE/2))
|
||||||
|
|
||||||
d.ellipse([(x,y),(x+self.LINEWIDTH,y+self.LINEWIDTH)],fill=(0,0,0,255))
|
x = middle + circleX - (self.LINEWIDTH/2) + devianceX
|
||||||
|
y = middle + circleY - (self.LINEWIDTH/2) + devianceY
|
||||||
|
|
||||||
|
circlePosition = [(x, y), (x+self.LINEWIDTH, y+self.LINEWIDTH)]
|
||||||
|
d.ellipse(circlePosition, fill=(0, 0, 0, 255))
|
||||||
|
|
||||||
return background
|
return background
|
||||||
|
|
||||||
def badLine(self, length, rotated = False):
|
def badLine(self, length: int, rotated: bool = False):
|
||||||
if rotated:
|
if rotated:
|
||||||
w, h = length+self.LINEWIDTH*3, self.LINEWIDTH*3
|
w, h = length+self.LINEWIDTH*3, self.LINEWIDTH*3
|
||||||
else:
|
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))
|
background = Image.new("RGBA", (w, h), color=(0, 0, 0, 0))
|
||||||
|
|
||||||
d = ImageDraw.Draw(background,"RGBA")
|
d = ImageDraw.Draw(background, "RGBA")
|
||||||
devianceX = random.randint(-int(self.LINEWIDTH/3),int(self.LINEWIDTH/3))
|
|
||||||
|
possibleDeviance = int(self.LINEWIDTH/3)
|
||||||
|
devianceX = random.randint(-possibleDeviance, possibleDeviance)
|
||||||
devianceY = 0
|
devianceY = 0
|
||||||
devianceAccuracyX = 0
|
devianceAccuracyX = 0
|
||||||
devianceAccuracyY = 0
|
devianceAccuracyY = 0
|
||||||
|
|
||||||
for pixel in range(length):
|
for pixel in range(length):
|
||||||
devianceX, devianceAccuracyX = self.calcDeviance(devianceX,devianceAccuracyX,self.LINEWIDTH/1000,self.LINEWIDTH,0.004)
|
devianceParamsX = [
|
||||||
devianceY, devianceAccuracyY = self.calcDeviance(devianceY,devianceAccuracyY,self.LINEWIDTH/1000,self.LINEWIDTH,0.004)
|
devianceX,
|
||||||
|
devianceAccuracyX,
|
||||||
|
self.LINEWIDTH/1000,
|
||||||
|
self.LINEWIDTH,
|
||||||
|
0.004
|
||||||
|
]
|
||||||
|
devianceParamsY = [
|
||||||
|
devianceY,
|
||||||
|
devianceAccuracyY,
|
||||||
|
self.LINEWIDTH/1000,
|
||||||
|
self.LINEWIDTH,
|
||||||
|
0.004
|
||||||
|
]
|
||||||
|
devianceX, devianceAccuracyX = self.calcDeviance(*devianceParamsX)
|
||||||
|
devianceY, devianceAccuracyY = self.calcDeviance(*devianceParamsY)
|
||||||
|
|
||||||
if rotated:
|
if rotated:
|
||||||
x = self.LINEWIDTH + pixel + devianceX
|
x = self.LINEWIDTH + pixel + devianceX
|
||||||
@ -312,165 +333,179 @@ class DrawHangman():
|
|||||||
x = self.LINEWIDTH + devianceX
|
x = self.LINEWIDTH + devianceX
|
||||||
y = self.LINEWIDTH + pixel + devianceY
|
y = self.LINEWIDTH + pixel + devianceY
|
||||||
|
|
||||||
d.ellipse([(x,y),(x+self.LINEWIDTH,y+self.LINEWIDTH)],fill=(0,0,0,255))
|
circlePosition = [(x, y), (x+self.LINEWIDTH, y+self.LINEWIDTH)]
|
||||||
|
d.ellipse(circlePosition, fill=(0, 0, 0, 255))
|
||||||
|
|
||||||
return background
|
return background
|
||||||
|
|
||||||
def drawMan(self, misses):
|
def drawMan(self, misses: int):
|
||||||
background = Image.new("RGBA",(self.MANX,self.MANY),color=(0,0,0,0))
|
manSize = (self.MANX, self.MANY)
|
||||||
|
background = Image.new("RGBA", manSize, color=(0, 0, 0, 0))
|
||||||
|
|
||||||
if misses >= 1:
|
if misses >= 1:
|
||||||
head = self.badCircle()
|
head = self.badCircle()
|
||||||
background.paste(head,(int((self.MANX-(self.CIRCLESIZE+(self.LINEWIDTH*3)))/2),0),head)
|
pasteX = (self.MANX-(self.CIRCLESIZE+(self.LINEWIDTH*3)))//2
|
||||||
|
pastePosition = (pasteX, 0)
|
||||||
|
background.paste(head, pastePosition, head)
|
||||||
if misses >= 2:
|
if misses >= 2:
|
||||||
body = self.badLine(self.BODYSIZE)
|
body = self.badLine(self.BODYSIZE)
|
||||||
background.paste(body,(int((self.MANX-(self.LINEWIDTH*3))/2),self.CIRCLESIZE),body)
|
pasteX = (self.MANX-(self.LINEWIDTH*3))//2
|
||||||
|
pastePosition = (pasteX, self.CIRCLESIZE)
|
||||||
|
background.paste(body, pastePosition, body)
|
||||||
|
|
||||||
if misses >= 3:
|
if misses >= 3:
|
||||||
limbs = random.sample(["rl","ll","ra","la"],min(misses-2,4))
|
limbs = random.sample(["rl", "ll", "ra", "la"], min(misses-2, 4))
|
||||||
else: limbs = []
|
else:
|
||||||
|
limbs = []
|
||||||
|
|
||||||
for limb in limbs:
|
for limb in limbs:
|
||||||
if limb == "ra":
|
limbDrawing = self.badLine(self.LIMBSIZE, True)
|
||||||
limbDrawing = self.badLine(self.LIMBSIZE,True)
|
xPosition = (self.MANX-(self.LINEWIDTH*3))//2
|
||||||
rotation = random.randint(-45,45)
|
|
||||||
xPosition = int((self.MANX-(self.LINEWIDTH*3))/2)
|
if limb[1] == "a":
|
||||||
rotationCompensation = min(-int(math.sin(math.radians(rotation))*(self.LIMBSIZE+(self.LINEWIDTH*3))),0)
|
rotation = random.randint(-45, 45)
|
||||||
yPosition = self.CIRCLESIZE+self.ARMPOSITION + rotationCompensation
|
shift = math.sin(math.radians(rotation))
|
||||||
limbDrawing = limbDrawing.rotate(rotation,expand=1)
|
compensation = int(shift*(self.LIMBSIZE+(self.LINEWIDTH*3)))
|
||||||
background.paste(limbDrawing,(xPosition,yPosition),limbDrawing)
|
limbDrawing = limbDrawing.rotate(rotation, expand=1)
|
||||||
elif limb == "la":
|
yPosition = self.CIRCLESIZE + self.ARMPOSITION + compensation
|
||||||
limbDrawing = self.badLine(self.LIMBSIZE,True)
|
if limb == "ra":
|
||||||
rotation = random.randint(-45,45)
|
compensation = min(-compensation, 0)
|
||||||
xPosition = int((self.MANX-(self.LINEWIDTH*3))/2)-self.LIMBSIZE
|
else:
|
||||||
rotationCompensation = min(int(math.sin(math.radians(rotation))*(self.LIMBSIZE+(self.LINEWIDTH*3))),0)
|
xPosition -= self.LIMBSIZE
|
||||||
yPosition = self.CIRCLESIZE+self.ARMPOSITION + rotationCompensation
|
compensation = min(compensation, 0)
|
||||||
limbDrawing = limbDrawing.rotate(rotation,expand=1)
|
else:
|
||||||
background.paste(limbDrawing,(xPosition,yPosition),limbDrawing)
|
rotation = random.randint(-15, 15)
|
||||||
elif limb == "rl":
|
|
||||||
limbDrawing = self.badLine(self.LIMBSIZE,True)
|
|
||||||
rotation = random.randint(-15,15)
|
|
||||||
xPosition = int((self.MANX-(self.LINEWIDTH*3))/2)-self.LINEWIDTH
|
|
||||||
yPosition = self.CIRCLESIZE+self.BODYSIZE-self.LINEWIDTH
|
|
||||||
limbDrawing = limbDrawing.rotate(rotation-45,expand=1)
|
|
||||||
background.paste(limbDrawing,(xPosition,yPosition),limbDrawing)
|
|
||||||
elif limb == "ll":
|
|
||||||
limbDrawing = self.badLine(self.LIMBSIZE,True)
|
|
||||||
rotation = random.randint(-15,15)
|
|
||||||
limbDrawing = limbDrawing.rotate(rotation+45,expand=1)
|
|
||||||
xPosition = int((self.MANX-(self.LINEWIDTH*3))/2)-limbDrawing.size[0]+self.LINEWIDTH*3
|
|
||||||
yPosition = self.CIRCLESIZE+self.BODYSIZE
|
yPosition = self.CIRCLESIZE+self.BODYSIZE
|
||||||
background.paste(limbDrawing,(xPosition,yPosition),limbDrawing)
|
if limb == "rl":
|
||||||
|
xPosition -= self.LINEWIDTH
|
||||||
|
limbDrawing = limbDrawing.rotate(rotation-45, expand=1)
|
||||||
|
else:
|
||||||
|
yPosition -= self.LINEWIDTH
|
||||||
|
xPosition += -limbDrawing.size[0]+self.LINEWIDTH*3
|
||||||
|
limbDrawing = limbDrawing.rotate(rotation+45, expand=1)
|
||||||
|
|
||||||
|
pastePosition = (xPosition, yPosition)
|
||||||
|
background.paste(limbDrawing, pastePosition, limbDrawing)
|
||||||
|
|
||||||
return background
|
return background
|
||||||
|
|
||||||
def badText(self, text, big, color=(0,0,0,255)):
|
def badText(self, text: str, big: bool, color: tuple = (0, 0, 0, 255)):
|
||||||
if big: font = self.FONT
|
if big:
|
||||||
else: font = self.SMALLFONT
|
font = self.FONT
|
||||||
|
else:
|
||||||
|
font = self.SMALLFONT
|
||||||
w, h = font.getsize(text)
|
w, h = font.getsize(text)
|
||||||
img = Image.new("RGBA",(w,h),color=(0,0,0,0))
|
img = Image.new("RGBA", (w, h), color=(0, 0, 0, 0))
|
||||||
d = ImageDraw.Draw(img,"RGBA")
|
d = ImageDraw.Draw(img, "RGBA")
|
||||||
|
|
||||||
d.text((0,0),text,font=font,fill=color)
|
d.text((0, 0), text, font=font, fill=color)
|
||||||
return img
|
return img
|
||||||
|
|
||||||
def drawGallows(self):
|
def drawGallows(self):
|
||||||
background = Image.new("RGBA",(self.GALLOWX,self.GALLOWY),color=(0,0,0,0))
|
gallowSize = (self.GALLOWX, self.GALLOWY)
|
||||||
|
background = Image.new("RGBA", gallowSize, color=(0, 0, 0, 0))
|
||||||
|
|
||||||
bottomLine = self.badLine(int(self.GALLOWX*0.75),True)
|
bottomLine = self.badLine(int(self.GALLOWX * 0.75), True)
|
||||||
background.paste(bottomLine,(int(self.GALLOWX*0.125),self.GALLOWY-(self.LINEWIDTH*4)),bottomLine)
|
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)
|
lineTwo = self.badLine(self.GALLOWY-self.LINEWIDTH*6)
|
||||||
background.paste(lineTwo,(int(self.GALLOWX*(0.75*self.GOLDENRATIO)),self.LINEWIDTH*2),lineTwo)
|
lineTwoX = int(self.GALLOWX*(0.75*self.GOLDENRATIO))
|
||||||
|
lineTwoY = self.LINEWIDTH*2
|
||||||
|
pastePosition = (lineTwoX, lineTwoY)
|
||||||
|
background.paste(lineTwo, pastePosition, lineTwo)
|
||||||
|
|
||||||
topLine = self.badLine(int(self.GALLOWY*0.30),True)
|
topLine = self.badLine(int(self.GALLOWY*0.30), True)
|
||||||
background.paste(topLine,(int(self.GALLOWX*(0.75*self.GOLDENRATIO))-self.LINEWIDTH,self.LINEWIDTH*3),topLine)
|
pasteX = int(self.GALLOWX*(0.75*self.GOLDENRATIO))-self.LINEWIDTH
|
||||||
|
pastePosition = (pasteX, self.LINEWIDTH*3)
|
||||||
|
background.paste(topLine, pastePosition, topLine)
|
||||||
|
|
||||||
lastLine = self.badLine(int(self.GALLOWY*0.125))
|
lastLine = self.badLine(int(self.GALLOWY*0.125))
|
||||||
background.paste(lastLine,((int(self.GALLOWX*(0.75*self.GOLDENRATIO))+int(self.GALLOWY*0.30)-self.LINEWIDTH),self.LINEWIDTH*3),lastLine)
|
pasteX += int(self.GALLOWY*0.30)
|
||||||
|
background.paste(lastLine, (pasteX, self.LINEWIDTH*3), lastLine)
|
||||||
return background
|
return background
|
||||||
|
|
||||||
def drawLetterLines(self, word,guessed,misses):
|
def drawLetterLines(self, word: str, guessed: list, misses: int):
|
||||||
letterLines = Image.new("RGBA",((self.LETTERLINELENGTH+self.LETTERLINEDISTANCE)*len(word),self.LETTERLINELENGTH+self.LINEWIDTH*3),color=(0,0,0,0))
|
imageWidth = (self.LETTERLINELENGTH+self.LETTERLINEDISTANCE)*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):
|
for x, letter in enumerate(word):
|
||||||
line = self.badLine(self.LETTERLINELENGTH,True)
|
line = self.badLine(self.LETTERLINELENGTH, True)
|
||||||
letterLines.paste(line,(x*(self.LETTERLINELENGTH+self.LETTERLINEDISTANCE),self.LETTERLINELENGTH),line)
|
pasteX = x*(self.LETTERLINELENGTH+self.LETTERLINEDISTANCE)
|
||||||
|
pastePosition = (pasteX, self.LETTERLINELENGTH)
|
||||||
|
letterLines.paste(line, pastePosition, line)
|
||||||
if guessed[x]:
|
if guessed[x]:
|
||||||
letterDrawing = self.badText(letter,True)
|
letterDrawing = self.badText(letter, True)
|
||||||
letterWidth = self.FONT.getsize(letter)[0]
|
letterWidth = self.FONT.getsize(letter)[0]
|
||||||
letterX = int(x*(self.LETTERLINELENGTH+self.LETTERLINEDISTANCE)-(letterWidth/2)+(self.LETTERLINELENGTH*0.5)+(self.LINEWIDTH*2))
|
letterX = x*(self.LETTERLINELENGTH+self.LETTERLINEDISTANCE)
|
||||||
letterLines.paste(letterDrawing,(letterX,0),letterDrawing)
|
letterX -= (letterWidth//2)
|
||||||
|
letterX += (self.LETTERLINELENGTH//2)+(self.LINEWIDTH*2)
|
||||||
|
letterLines.paste(letterDrawing, (letterX, 0), letterDrawing)
|
||||||
elif misses == 6:
|
elif misses == 6:
|
||||||
letterDrawing = self.badText(letter,True,(242,66,54))
|
letterDrawing = self.badText(letter, True, (242, 66, 54))
|
||||||
letterWidth = self.FONT.getsize(letter)[0]
|
letterWidth = self.FONT.getsize(letter)[0]
|
||||||
letterX = int(x*(self.LETTERLINELENGTH+self.LETTERLINEDISTANCE)-(letterWidth/2)+(self.LETTERLINELENGTH*0.5)+(self.LINEWIDTH*2))
|
letterX = x*(self.LETTERLINELENGTH+self.LETTERLINEDISTANCE)
|
||||||
letterLines.paste(letterDrawing,(letterX,0),letterDrawing)
|
letterX -= (letterWidth//2)
|
||||||
|
letterX += (self.LETTERLINELENGTH//2)+(self.LINEWIDTH*2)
|
||||||
|
letterLines.paste(letterDrawing, (letterX, 0), letterDrawing)
|
||||||
|
|
||||||
return letterLines
|
return letterLines
|
||||||
|
|
||||||
def shortestDist(self,positions,newPosition):
|
def shortestDist(self, positions: list, newPosition: tuple):
|
||||||
shortestDist = math.inf
|
shortestDist = math.inf
|
||||||
x, y = newPosition
|
x, y = newPosition
|
||||||
for i, j in positions:
|
for i, j in positions:
|
||||||
xDistance = abs(i-x)
|
xDistance = abs(i-x)
|
||||||
yDistance = abs(j-y)
|
yDistance = abs(j-y)
|
||||||
dist = math.sqrt(xDistance**2+yDistance**2)
|
dist = math.sqrt(xDistance**2+yDistance**2)
|
||||||
if shortestDist > dist: shortestDist = dist
|
if shortestDist > dist:
|
||||||
|
shortestDist = dist
|
||||||
return shortestDist
|
return shortestDist
|
||||||
|
|
||||||
def drawMisses(self,guesses,word):
|
def drawMisses(self, guesses: list, word: str):
|
||||||
background = Image.new("RGBA",(600,400),color=(0,0,0,0))
|
background = Image.new("RGBA", (600, 400), color=(0, 0, 0, 0))
|
||||||
pos = []
|
pos = []
|
||||||
for guess in guesses:
|
for guess in guesses:
|
||||||
if guess not in word:
|
if guess not in word:
|
||||||
placed = False
|
placed = False
|
||||||
while placed == False:
|
while not placed:
|
||||||
letter = self.badText(guess,True)
|
letter = self.badText(guess, True)
|
||||||
w, h = self.FONT.getsize(guess)
|
w, h = self.FONT.getsize(guess)
|
||||||
x = random.randint(0,600-w)
|
x = random.randint(0, 600-w)
|
||||||
y = random.randint(0,400-h)
|
y = random.randint(0, 400-h)
|
||||||
if self.shortestDist(pos,(x,y)) > 70:
|
if self.shortestDist(pos, (x, y)) > 70:
|
||||||
pos.append((x,y))
|
pos.append((x, y))
|
||||||
background.paste(letter,(x,y),letter)
|
background.paste(letter, (x, y), letter)
|
||||||
placed = True
|
placed = True
|
||||||
return background
|
return background
|
||||||
|
|
||||||
def drawImage(self,channel):
|
def drawImage(self, channel: str):
|
||||||
self.bot.log("Drawing hangman image", channel)
|
self.bot.log("Drawing hangman image", channel)
|
||||||
game = self.bot.database["hangman games"].find_one({"_id":channel})
|
game = self.bot.database["hangman games"].find_one({"_id": channel})
|
||||||
|
|
||||||
random.seed(game["game ID"])
|
random.seed(game["game ID"])
|
||||||
|
|
||||||
background = Image.open("resources/paper.jpg")
|
background = Image.open("resources/paper.jpg")
|
||||||
try:
|
gallow = self.drawGallows()
|
||||||
gallow = self.drawGallows()
|
man = self.drawMan(game["misses"])
|
||||||
except:
|
|
||||||
self.bot.log("Error drawing gallows (error code 1711)")
|
|
||||||
|
|
||||||
try:
|
|
||||||
man = self.drawMan(game["misses"])
|
|
||||||
except:
|
|
||||||
self.bot.log("Error drawing stick figure (error code 1712)")
|
|
||||||
|
|
||||||
random.seed(game["game ID"])
|
random.seed(game["game ID"])
|
||||||
try:
|
letterLineParams = [game["word"], game["guessed"], game["misses"]]
|
||||||
letterLines = self.drawLetterLines(game["word"],game["guessed"],game["misses"])
|
letterLines = self.drawLetterLines(*letterLineParams)
|
||||||
except:
|
|
||||||
self.bot.log("error drawing letter lines (error code 1713)")
|
|
||||||
|
|
||||||
random.seed(game["game ID"])
|
random.seed(game["game ID"])
|
||||||
try:
|
misses = self.drawMisses(game["guessed letters"], game["word"])
|
||||||
misses = self.drawMisses(game["guessed letters"],game["word"])
|
|
||||||
except:
|
|
||||||
self.bot.log("Error drawing misses (error code 1714)")
|
|
||||||
|
|
||||||
background.paste(gallow,(100,100),gallow)
|
background.paste(gallow, (100, 100), gallow)
|
||||||
background.paste(man,(300,210),man)
|
background.paste(man, (300, 210), man)
|
||||||
background.paste(letterLines,(120,840),letterLines)
|
background.paste(letterLines, (120, 840), letterLines)
|
||||||
background.paste(misses,(600,150),misses)
|
background.paste(misses, (600, 150), misses)
|
||||||
|
|
||||||
missesText = self.badText("MISSES",False)
|
missesText = self.badText("MISSES", False)
|
||||||
missesTextWidth = missesText.size[0]
|
missesTextWidth = missesText.size[0]
|
||||||
background.paste(missesText,(850-int(missesTextWidth/2),50),missesText)
|
background.paste(missesText, (850-missesTextWidth//2, 50), missesText)
|
||||||
|
|
||||||
background.save("resources/games/hangmanBoards/hangmanBoard"+channel+".png")
|
boardPath = f"resources/games/hangmanBoards/hangmanBoard{channel}.png"
|
||||||
|
background.save(boardPath)
|
||||||
|
Reference in New Issue
Block a user