:spakles: Database and OOP
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import math, random, json
|
||||
import math, random
|
||||
|
||||
from PIL import ImageDraw, Image, ImageFont
|
||||
from funcs import logThis
|
||||
@ -28,222 +28,226 @@ smolfnt = ImageFont.truetype('resources/comic-sans-bold.ttf', textSize)
|
||||
|
||||
backgroundColor = (255,255,255,255)
|
||||
|
||||
def calcDeviance(preDev,preDevAcc,posChange,maxmin,maxAcceleration):
|
||||
devAcc = preDevAcc + random.uniform(-posChange,posChange)
|
||||
if devAcc > maxmin * maxAcceleration: devAcc = maxmin * maxAcceleration
|
||||
elif devAcc < -maxmin * maxAcceleration: devAcc = -maxmin * maxAcceleration
|
||||
class DrawHangman():
|
||||
def __init__(self,bot):
|
||||
self.bot = bot
|
||||
|
||||
dev = preDev + devAcc
|
||||
if dev > maxmin: dev = maxmin
|
||||
elif dev < -maxmin: dev = -maxmin
|
||||
return dev, devAcc
|
||||
def calcDeviance(self,preDev,preDevAcc,posChange,maxmin,maxAcceleration):
|
||||
devAcc = preDevAcc + random.uniform(-posChange,posChange)
|
||||
if devAcc > maxmin * maxAcceleration: devAcc = maxmin * maxAcceleration
|
||||
elif devAcc < -maxmin * maxAcceleration: devAcc = -maxmin * maxAcceleration
|
||||
|
||||
def badCircle():
|
||||
background = Image.new("RGBA",(circleSize+(lineWidth*3),circleSize+(lineWidth*3)),color=(0,0,0,0))
|
||||
dev = preDev + devAcc
|
||||
if dev > maxmin: dev = maxmin
|
||||
elif dev < -maxmin: dev = -maxmin
|
||||
return dev, devAcc
|
||||
|
||||
d = ImageDraw.Draw(background,"RGBA")
|
||||
middle = (circleSize+(lineWidth*3))/2
|
||||
devx = 0
|
||||
devy = 0
|
||||
devAccx = 0
|
||||
devAccy = 0
|
||||
start = random.randint(-100,-80)
|
||||
degreesAmount = circleDegrees + random.randint(-10,30)
|
||||
def badCircle(self):
|
||||
background = Image.new("RGBA",(circleSize+(lineWidth*3),circleSize+(lineWidth*3)),color=(0,0,0,0))
|
||||
|
||||
for degree in range(degreesAmount):
|
||||
devx, devAccx = calcDeviance(devx,devAccx,lineWidth/100,lineWidth,0.03)
|
||||
devy, devAccy = calcDeviance(devy,devAccy,lineWidth/100,lineWidth,0.03)
|
||||
d = ImageDraw.Draw(background,"RGBA")
|
||||
middle = (circleSize+(lineWidth*3))/2
|
||||
devx = 0
|
||||
devy = 0
|
||||
devAccx = 0
|
||||
devAccy = 0
|
||||
start = random.randint(-100,-80)
|
||||
degreesAmount = circleDegrees + random.randint(-10,30)
|
||||
|
||||
x = middle + (math.cos(math.radians(degree+start)) * (circleSize/2)) - (lineWidth/2) + devx
|
||||
y = middle + (math.sin(math.radians(degree+start)) * (circleSize/2)) - (lineWidth/2) + devy
|
||||
for degree in range(degreesAmount):
|
||||
devx, devAccx = self.calcDeviance(devx,devAccx,lineWidth/100,lineWidth,0.03)
|
||||
devy, devAccy = self.calcDeviance(devy,devAccy,lineWidth/100,lineWidth,0.03)
|
||||
|
||||
d.ellipse([(x,y),(x+lineWidth,y+lineWidth)],fill=(0,0,0,255))
|
||||
x = middle + (math.cos(math.radians(degree+start)) * (circleSize/2)) - (lineWidth/2) + devx
|
||||
y = middle + (math.sin(math.radians(degree+start)) * (circleSize/2)) - (lineWidth/2) + devy
|
||||
|
||||
return background
|
||||
d.ellipse([(x,y),(x+lineWidth,y+lineWidth)],fill=(0,0,0,255))
|
||||
|
||||
def badLine(length, rotated = False):
|
||||
if rotated:
|
||||
w, h = length+lineWidth*3, lineWidth*3
|
||||
else:
|
||||
w, h = lineWidth*3,length+lineWidth*3
|
||||
background = Image.new("RGBA",(w,h),color=(0,0,0,0))
|
||||
|
||||
d = ImageDraw.Draw(background,"RGBA")
|
||||
devx = random.randint(-int(lineWidth/3),int(lineWidth/3))
|
||||
devy = 0
|
||||
devAccx = 0
|
||||
devAccy = 0
|
||||
|
||||
for pixel in range(length):
|
||||
devx, devAccx = calcDeviance(devx,devAccx,lineWidth/1000,lineWidth,0.004)
|
||||
devy, devAccy = calcDeviance(devy,devAccy,lineWidth/1000,lineWidth,0.004)
|
||||
return background
|
||||
|
||||
def badLine(self, length, rotated = False):
|
||||
if rotated:
|
||||
x = lineWidth + pixel + devx
|
||||
y = lineWidth + devy
|
||||
w, h = length+lineWidth*3, lineWidth*3
|
||||
else:
|
||||
x = lineWidth + devx
|
||||
y = lineWidth + pixel + devy
|
||||
w, h = lineWidth*3,length+lineWidth*3
|
||||
background = Image.new("RGBA",(w,h),color=(0,0,0,0))
|
||||
|
||||
d.ellipse([(x,y),(x+lineWidth,y+lineWidth)],fill=(0,0,0,255))
|
||||
d = ImageDraw.Draw(background,"RGBA")
|
||||
devx = random.randint(-int(lineWidth/3),int(lineWidth/3))
|
||||
devy = 0
|
||||
devAccx = 0
|
||||
devAccy = 0
|
||||
|
||||
return background
|
||||
for pixel in range(length):
|
||||
devx, devAccx = self.calcDeviance(devx,devAccx,lineWidth/1000,lineWidth,0.004)
|
||||
devy, devAccy = self.calcDeviance(devy,devAccy,lineWidth/1000,lineWidth,0.004)
|
||||
|
||||
def drawMan(misses):
|
||||
background = Image.new("RGBA",(manx,many),color=(0,0,0,0))
|
||||
if rotated:
|
||||
x = lineWidth + pixel + devx
|
||||
y = lineWidth + devy
|
||||
else:
|
||||
x = lineWidth + devx
|
||||
y = lineWidth + pixel + devy
|
||||
|
||||
if misses >= 1:
|
||||
head = badCircle()
|
||||
background.paste(head,(int((manx-(circleSize+(lineWidth*3)))/2),0),head)
|
||||
if misses >= 2:
|
||||
body = badLine(bodySize)
|
||||
background.paste(body,(int((manx-(lineWidth*3))/2),circleSize),body)
|
||||
d.ellipse([(x,y),(x+lineWidth,y+lineWidth)],fill=(0,0,0,255))
|
||||
|
||||
if misses >= 3:
|
||||
limbs = random.sample(["rl","ll","ra","la"],min(misses-2,4))
|
||||
else: limbs = []
|
||||
return background
|
||||
|
||||
for limb in limbs:
|
||||
if limb == "ra":
|
||||
limbDrawing = badLine(limbSize,True)
|
||||
rotation = random.randint(-45,45)
|
||||
xpos = int((manx-(lineWidth*3))/2)
|
||||
rotationCompensation = min(-int(math.sin(math.radians(rotation))*(limbSize+(lineWidth*3))),0)
|
||||
ypos = circleSize+armPosition + rotationCompensation
|
||||
limbDrawing = limbDrawing.rotate(rotation,expand=1)
|
||||
background.paste(limbDrawing,(xpos,ypos),limbDrawing)
|
||||
elif limb == "la":
|
||||
limbDrawing = badLine(limbSize,True)
|
||||
rotation = random.randint(-45,45)
|
||||
xpos = int((manx-(lineWidth*3))/2)-limbSize
|
||||
rotationCompensation = min(int(math.sin(math.radians(rotation))*(limbSize+(lineWidth*3))),0)
|
||||
ypos = circleSize+armPosition + rotationCompensation
|
||||
limbDrawing = limbDrawing.rotate(rotation,expand=1)
|
||||
background.paste(limbDrawing,(xpos,ypos),limbDrawing)
|
||||
elif limb == "rl":
|
||||
limbDrawing = badLine(limbSize,True)
|
||||
rotation = random.randint(-15,15)
|
||||
xpos = int((manx-(lineWidth*3))/2)-lineWidth
|
||||
ypos = circleSize+bodySize-lineWidth
|
||||
limbDrawing = limbDrawing.rotate(rotation-45,expand=1)
|
||||
background.paste(limbDrawing,(xpos,ypos),limbDrawing)
|
||||
elif limb == "ll":
|
||||
limbDrawing = badLine(limbSize,True)
|
||||
rotation = random.randint(-15,15)
|
||||
limbDrawing = limbDrawing.rotate(rotation+45,expand=1)
|
||||
xpos = int((manx-(lineWidth*3))/2)-limbDrawing.size[0]+lineWidth*3
|
||||
ypos = circleSize+bodySize
|
||||
background.paste(limbDrawing,(xpos,ypos),limbDrawing)
|
||||
def drawMan(self, misses):
|
||||
background = Image.new("RGBA",(manx,many),color=(0,0,0,0))
|
||||
|
||||
return background
|
||||
if misses >= 1:
|
||||
head = self.badCircle()
|
||||
background.paste(head,(int((manx-(circleSize+(lineWidth*3)))/2),0),head)
|
||||
if misses >= 2:
|
||||
body = self.badLine(bodySize)
|
||||
background.paste(body,(int((manx-(lineWidth*3))/2),circleSize),body)
|
||||
|
||||
def badText(text, big, color=(0,0,0,255)):
|
||||
if big: font = fnt
|
||||
else: font = smolfnt
|
||||
w, h = font.getsize(text)
|
||||
img = Image.new("RGBA",(w,h),color=(0,0,0,0))
|
||||
d = ImageDraw.Draw(img,"RGBA")
|
||||
if misses >= 3:
|
||||
limbs = random.sample(["rl","ll","ra","la"],min(misses-2,4))
|
||||
else: limbs = []
|
||||
|
||||
d.text((0,0),text,font=font,fill=color)
|
||||
return img
|
||||
for limb in limbs:
|
||||
if limb == "ra":
|
||||
limbDrawing = self.badLine(limbSize,True)
|
||||
rotation = random.randint(-45,45)
|
||||
xpos = int((manx-(lineWidth*3))/2)
|
||||
rotationCompensation = min(-int(math.sin(math.radians(rotation))*(limbSize+(lineWidth*3))),0)
|
||||
ypos = circleSize+armPosition + rotationCompensation
|
||||
limbDrawing = limbDrawing.rotate(rotation,expand=1)
|
||||
background.paste(limbDrawing,(xpos,ypos),limbDrawing)
|
||||
elif limb == "la":
|
||||
limbDrawing = self.badLine(limbSize,True)
|
||||
rotation = random.randint(-45,45)
|
||||
xpos = int((manx-(lineWidth*3))/2)-limbSize
|
||||
rotationCompensation = min(int(math.sin(math.radians(rotation))*(limbSize+(lineWidth*3))),0)
|
||||
ypos = circleSize+armPosition + rotationCompensation
|
||||
limbDrawing = limbDrawing.rotate(rotation,expand=1)
|
||||
background.paste(limbDrawing,(xpos,ypos),limbDrawing)
|
||||
elif limb == "rl":
|
||||
limbDrawing = self.badLine(limbSize,True)
|
||||
rotation = random.randint(-15,15)
|
||||
xpos = int((manx-(lineWidth*3))/2)-lineWidth
|
||||
ypos = circleSize+bodySize-lineWidth
|
||||
limbDrawing = limbDrawing.rotate(rotation-45,expand=1)
|
||||
background.paste(limbDrawing,(xpos,ypos),limbDrawing)
|
||||
elif limb == "ll":
|
||||
limbDrawing = self.badLine(limbSize,True)
|
||||
rotation = random.randint(-15,15)
|
||||
limbDrawing = limbDrawing.rotate(rotation+45,expand=1)
|
||||
xpos = int((manx-(lineWidth*3))/2)-limbDrawing.size[0]+lineWidth*3
|
||||
ypos = circleSize+bodySize
|
||||
background.paste(limbDrawing,(xpos,ypos),limbDrawing)
|
||||
|
||||
def drawGallows():
|
||||
background = Image.new("RGBA",(gallowx,gallowy),color=(0,0,0,0))
|
||||
return background
|
||||
|
||||
bottomLine = badLine(int(gallowx*0.75),True)
|
||||
background.paste(bottomLine,(int(gallowx*0.125),gallowy-(lineWidth*4)),bottomLine)
|
||||
def badText(self, text, big, color=(0,0,0,255)):
|
||||
if big: font = fnt
|
||||
else: font = smolfnt
|
||||
w, h = font.getsize(text)
|
||||
img = Image.new("RGBA",(w,h),color=(0,0,0,0))
|
||||
d = ImageDraw.Draw(img,"RGBA")
|
||||
|
||||
lineTwo = badLine(gallowy-lineWidth*6)
|
||||
background.paste(lineTwo,(int(gallowx*(0.75*goldenRatio)),lineWidth*2),lineTwo)
|
||||
d.text((0,0),text,font=font,fill=color)
|
||||
return img
|
||||
|
||||
topLine = badLine(int(gallowy*0.30),True)
|
||||
background.paste(topLine,(int(gallowx*(0.75*goldenRatio))-lineWidth,lineWidth*3),topLine)
|
||||
def drawGallows(self):
|
||||
background = Image.new("RGBA",(gallowx,gallowy),color=(0,0,0,0))
|
||||
|
||||
lastLine = badLine(int(gallowy*0.125))
|
||||
background.paste(lastLine,((int(gallowx*(0.75*goldenRatio))+int(gallowy*0.30)-lineWidth),lineWidth*3),lastLine)
|
||||
return background
|
||||
bottomLine = self.badLine(int(gallowx*0.75),True)
|
||||
background.paste(bottomLine,(int(gallowx*0.125),gallowy-(lineWidth*4)),bottomLine)
|
||||
|
||||
def drawLetterLines(word,guessed,misses):
|
||||
letterLines = Image.new("RGBA",((letterLineLength+letterLineDistance)*len(word),letterLineLength+lineWidth*3),color=(0,0,0,0))
|
||||
for x, letter in enumerate(word):
|
||||
line = badLine(letterLineLength,True)
|
||||
letterLines.paste(line,(x*(letterLineLength+letterLineDistance),letterLineLength),line)
|
||||
if guessed[x]:
|
||||
letterDrawing = badText(letter,True)
|
||||
letterWidth = fnt.getsize(letter)[0]
|
||||
letterx = int(x*(letterLineLength+letterLineDistance)-(letterWidth/2)+(letterLineLength*0.5)+(lineWidth*2))
|
||||
letterLines.paste(letterDrawing,(letterx,0),letterDrawing)
|
||||
elif misses == 6:
|
||||
letterDrawing = badText(letter,True,(242,66,54))
|
||||
letterWidth = fnt.getsize(letter)[0]
|
||||
letterx = int(x*(letterLineLength+letterLineDistance)-(letterWidth/2)+(letterLineLength*0.5)+(lineWidth*2))
|
||||
letterLines.paste(letterDrawing,(letterx,0),letterDrawing)
|
||||
lineTwo = self.badLine(gallowy-lineWidth*6)
|
||||
background.paste(lineTwo,(int(gallowx*(0.75*goldenRatio)),lineWidth*2),lineTwo)
|
||||
|
||||
return letterLines
|
||||
topLine = self.badLine(int(gallowy*0.30),True)
|
||||
background.paste(topLine,(int(gallowx*(0.75*goldenRatio))-lineWidth,lineWidth*3),topLine)
|
||||
|
||||
def shortestDist(positions,newPosition):
|
||||
shortestDist = math.inf
|
||||
x, y = newPosition
|
||||
for i, j in positions:
|
||||
xdist = abs(i-x)
|
||||
ydist = abs(j-y)
|
||||
dist = math.sqrt(xdist**2+ydist**2)
|
||||
if shortestDist > dist: shortestDist = dist
|
||||
return shortestDist
|
||||
lastLine = self.badLine(int(gallowy*0.125))
|
||||
background.paste(lastLine,((int(gallowx*(0.75*goldenRatio))+int(gallowy*0.30)-lineWidth),lineWidth*3),lastLine)
|
||||
return background
|
||||
|
||||
def drawMisses(guesses,word):
|
||||
background = Image.new("RGBA",(600,400),color=(0,0,0,0))
|
||||
pos = []
|
||||
for guess in guesses:
|
||||
if guess not in word:
|
||||
placed = False
|
||||
while placed == False:
|
||||
letter = badText(guess,True)
|
||||
w, h = fnt.getsize(guess)
|
||||
x = random.randint(0,600-w)
|
||||
y = random.randint(0,400-h)
|
||||
if shortestDist(pos,(x,y)) > 70:
|
||||
pos.append((x,y))
|
||||
background.paste(letter,(x,y),letter)
|
||||
placed = True
|
||||
return background
|
||||
def drawLetterLines(self, word,guessed,misses):
|
||||
letterLines = Image.new("RGBA",((letterLineLength+letterLineDistance)*len(word),letterLineLength+lineWidth*3),color=(0,0,0,0))
|
||||
for x, letter in enumerate(word):
|
||||
line = self.badLine(letterLineLength,True)
|
||||
letterLines.paste(line,(x*(letterLineLength+letterLineDistance),letterLineLength),line)
|
||||
if guessed[x]:
|
||||
letterDrawing = self.badText(letter,True)
|
||||
letterWidth = fnt.getsize(letter)[0]
|
||||
letterx = int(x*(letterLineLength+letterLineDistance)-(letterWidth/2)+(letterLineLength*0.5)+(lineWidth*2))
|
||||
letterLines.paste(letterDrawing,(letterx,0),letterDrawing)
|
||||
elif misses == 6:
|
||||
letterDrawing = self.badText(letter,True,(242,66,54))
|
||||
letterWidth = fnt.getsize(letter)[0]
|
||||
letterx = int(x*(letterLineLength+letterLineDistance)-(letterWidth/2)+(letterLineLength*0.5)+(lineWidth*2))
|
||||
letterLines.paste(letterDrawing,(letterx,0),letterDrawing)
|
||||
|
||||
def drawImage(channel):
|
||||
with open("resources/games/hangmanGames.json", "r") as f:
|
||||
data = json.load(f)
|
||||
return letterLines
|
||||
|
||||
random.seed(data[channel]["game ID"])
|
||||
def shortestDist(self,positions,newPosition):
|
||||
shortestDist = math.inf
|
||||
x, y = newPosition
|
||||
for i, j in positions:
|
||||
xdist = abs(i-x)
|
||||
ydist = abs(j-y)
|
||||
dist = math.sqrt(xdist**2+ydist**2)
|
||||
if shortestDist > dist: shortestDist = dist
|
||||
return shortestDist
|
||||
|
||||
background = Image.open("resources/paper.jpg")
|
||||
try:
|
||||
gallow = drawGallows()
|
||||
except:
|
||||
logThis("Error drawing gallows (error code 1711)")
|
||||
def drawMisses(self,guesses,word):
|
||||
background = Image.new("RGBA",(600,400),color=(0,0,0,0))
|
||||
pos = []
|
||||
for guess in guesses:
|
||||
if guess not in word:
|
||||
placed = False
|
||||
while placed == False:
|
||||
letter = self.badText(guess,True)
|
||||
w, h = fnt.getsize(guess)
|
||||
x = random.randint(0,600-w)
|
||||
y = random.randint(0,400-h)
|
||||
if self.shortestDist(pos,(x,y)) > 70:
|
||||
pos.append((x,y))
|
||||
background.paste(letter,(x,y),letter)
|
||||
placed = True
|
||||
return background
|
||||
|
||||
try:
|
||||
man = drawMan(data[channel]["misses"])
|
||||
except:
|
||||
logThis("Error drawing stick figure (error code 1712)")
|
||||
def drawImage(self,channel):
|
||||
logThis("Drawing hangman image",channel)
|
||||
game = self.bot.database["hangman games"].find_one({"_id":channel})
|
||||
|
||||
random.seed(data[channel]["game ID"])
|
||||
try:
|
||||
letterLines = drawLetterLines(data[channel]["word"],data[channel]["guessed"],data[channel]["misses"])
|
||||
except:
|
||||
logThis("error drawing letter lines (error code 1713)")
|
||||
random.seed(game["game ID"])
|
||||
|
||||
random.seed(data[channel]["game ID"])
|
||||
try:
|
||||
misses = drawMisses(data[channel]["guessed letters"],data[channel]["word"])
|
||||
except:
|
||||
logThis("Error drawing misses (error code 1714)")
|
||||
background = Image.open("resources/paper.jpg")
|
||||
try:
|
||||
gallow = self.drawGallows()
|
||||
except:
|
||||
logThis("Error drawing gallows (error code 1711)")
|
||||
|
||||
background.paste(gallow,(100,100),gallow)
|
||||
background.paste(man,(300,210),man)
|
||||
background.paste(letterLines,(120,840),letterLines)
|
||||
background.paste(misses,(600,150),misses)
|
||||
try:
|
||||
man = self.drawMan(game["misses"])
|
||||
except:
|
||||
logThis("Error drawing stick figure (error code 1712)")
|
||||
|
||||
missesText = badText("MISSES",False)
|
||||
missesTextWidth = missesText.size[0]
|
||||
background.paste(missesText,(850-int(missesTextWidth/2),50),missesText)
|
||||
random.seed(game["game ID"])
|
||||
try:
|
||||
letterLines = self.drawLetterLines(game["word"],game["guessed"],game["misses"])
|
||||
except:
|
||||
logThis("error drawing letter lines (error code 1713)")
|
||||
|
||||
background.save("resources/games/hangmanBoards/hangmanBoard"+channel+".png")
|
||||
random.seed(game["game ID"])
|
||||
try:
|
||||
misses = self.drawMisses(game["guessed letters"],game["word"])
|
||||
except:
|
||||
logThis("Error drawing misses (error code 1714)")
|
||||
|
||||
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)
|
||||
missesTextWidth = missesText.size[0]
|
||||
background.paste(missesText,(850-int(missesTextWidth/2),50),missesText)
|
||||
|
||||
background.save("resources/games/hangmanBoards/hangmanBoard"+channel+".png")
|
||||
|
Reference in New Issue
Block a user