Hangman

This commit is contained in:
NikolajDanger
2020-08-08 19:13:43 +02:00
parent 1490cf81eb
commit 2daefb9585
15 changed files with 225 additions and 71 deletions

View File

@ -4,30 +4,27 @@ from PIL import ImageDraw, Image, ImageFont
from funcs import logThis
circleDegrees = 360
circleSize = 400
lineWidth = 40
circleSize = 120
lineWidth = 12
bodySize = 700
limbSize = 300
armPosition = 200
bodySize =210
limbSize = 60
armPosition = 60
manx = (limbSize*2)+lineWidth*4
many = (circleSize+bodySize+limbSize)+lineWidth*4
letterSize = 250
letterSize = 75
textSize = 70
letterLineLength = 300
letterLineDistance = 100
letterLineLength = 90
letterLineDistance = 30
gallowx, gallowy = 1200,2000
gallowx, gallowy = 360,600
goldenRatio = 1-(1 / ((1 + 5 ** 0.5) / 2))
smolGallowx, smolGallowy = int(gallowx * (3/10)),int(gallowy * (3/10))
smolManx, smolMany = int(manx * (3/10)),int(many * (3/10))
fnt = ImageFont.truetype('resources/comic-sans.ttf', letterSize)
smolfnt = ImageFont.truetype('resources/comic-sans.ttf', textSize)
fnt = ImageFont.truetype('resources/comic-sans-bold.ttf', letterSize)
smolfnt = ImageFont.truetype('resources/comic-sans-bold.ttf', textSize)
backgroundColor = (255,255,255,255)
@ -140,23 +137,23 @@ def drawMan(misses):
return background
def badText(text, big):
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")
d.text((0,0),text,font=font,fill=(0,0,0,255))
d.text((0,0),text,font=font,fill=color)
return img
def drawGallows():
background = Image.new("RGBA",(gallowx,gallowy),color=(0,0,0,0))
bottomLine = badLine(int(gallowx*0.75),True)
background.paste(bottomLine,(int(gallowx*0.125),gallowy-(lineWidth*3)),bottomLine)
background.paste(bottomLine,(int(gallowx*0.125),gallowy-(lineWidth*4)),bottomLine)
lineTwo = badLine(gallowy-lineWidth*4)
lineTwo = badLine(gallowy-lineWidth*6)
background.paste(lineTwo,(int(gallowx*(0.75*goldenRatio)),lineWidth*2),lineTwo)
topLine = badLine(int(gallowy*0.30),True)
@ -166,49 +163,87 @@ def drawGallows():
background.paste(lastLine,((int(gallowx*(0.75*goldenRatio))+int(gallowy*0.30)-lineWidth),lineWidth*3),lastLine)
return background
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)
return letterLines
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
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 drawImage(channel):
with open("resources/games/hangmanGames.json", "r") as f:
data = json.load(f)
random.seed(data[channel]["game ID"])
background = Image.new("RGBA",(1600,1000),color=backgroundColor)
background = Image.open("resources/paper.jpg")
try:
gallow = drawGallows()
except:
logThis("Error drawing gallows (error code 1711)")
gallow = gallow.resize((smolGallowx,smolGallowy))
try:
man = drawMan(data[channel]["misses"])
except:
logThis("Error drawing stick figure (error code 1712)")
random.seed(data[channel]["game ID"])
try:
man = man.resize((smolManx,smolMany))
letterLines = drawLetterLines(data[channel]["word"],data[channel]["guessed"],data[channel]["misses"])
except:
logThis("Error resizing")
logThis("error drawing letter lines (error code 1713)")
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.paste(gallow,(100,100),gallow)
background.paste(man,(280,210),man)
background.paste(man,(300,210),man)
background.paste(letterLines,(120,840),letterLines)
background.paste(misses,(600,150),misses)
letterLines = Image.new("RGBA",((letterLineLength+letterLineDistance)*len(data[channel]["word"]),300+lineWidth*3),color=(0,0,0,0))
for x, letter in enumerate(data[channel]["word"]):
line = badLine(letterLineLength,True)
letterLines.paste(line,(x*(letterLineLength+letterLineDistance),300),line)
if data[channel]["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)
llw, llh = letterLines.size
letterLines = letterLines.resize((int(llw*(3/10)),int(llh*(3/10))))
background.paste(letterLines,(120,860),letterLines)
d = ImageDraw.Draw(background,"RGBA")
d.text((850,50),"Misses",font=smolfnt,fill=(0,0,0,255))
missesText = badText("MISSES",False)
missesTextWidth = missesText.size[0]
background.paste(missesText,(850-int(missesTextWidth/2),50),missesText)
background.save("resources/games/hangmanBoards/hangmanBoard"+channel+".png")