✨ Moved draw classes to the game files
This commit is contained in:
@ -3,11 +3,11 @@ import math
|
||||
import datetime
|
||||
import asyncio
|
||||
import discord
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
from shutil import copyfile
|
||||
|
||||
from utils import replaceMultiple
|
||||
from .blackjackDraw import DrawBlackjack
|
||||
|
||||
class Blackjack():
|
||||
def __init__(self,bot):
|
||||
@ -795,7 +795,7 @@ class Blackjack():
|
||||
else:
|
||||
self.bot.log("Ending loop on round "+str(gameRound),str(channel.id))
|
||||
|
||||
# Returning current hi-lo value
|
||||
# Returning current hi-lo value
|
||||
async def hilo(self, ctx):
|
||||
channel = ctx.channel_id
|
||||
data = self.bot.database["hilo"].find_one({"_id":str(channel)})
|
||||
@ -825,4 +825,142 @@ class Blackjack():
|
||||
decksLeft = round(cardsLeft/52,1)
|
||||
await ctx.send(f"Cards left:\n{cardsLeft} cards, {decksLeft} decks", hidden=True)
|
||||
|
||||
class DrawBlackjack():
|
||||
def __init__(self,bot):
|
||||
self.bot = bot
|
||||
self.BORDER = 100
|
||||
self.PLACEMENT = [0,0]
|
||||
self.ROTATION = 0
|
||||
|
||||
def drawImage(self,channel):
|
||||
self.bot.log("Drawing blackjack table",channel)
|
||||
game = self.bot.database["blackjack games"].find_one({"_id":channel})
|
||||
|
||||
fnt = ImageFont.truetype('resources/fonts/futura-bold.ttf', 50)
|
||||
fntSmol = ImageFont.truetype('resources/fonts/futura-bold.ttf', 40)
|
||||
self.BORDERSmol = int(self.BORDER/3.5)
|
||||
|
||||
table = Image.open("resources/games/blackjackTable.png")
|
||||
self.PLACEMENT = [2,1,3,0,4]
|
||||
textImage = ImageDraw.Draw(table)
|
||||
hands = game["user hands"]
|
||||
|
||||
dealerBusted = game["dealer busted"]
|
||||
dealerBlackjack = game["dealer blackjack"]
|
||||
|
||||
try:
|
||||
if game["all standing"] == False:
|
||||
dealerHand = self.drawHand(game["dealer hand"],True,False,False)
|
||||
else:
|
||||
dealerHand = self.drawHand(game["dealer hand"],False,dealerBusted,dealerBlackjack)
|
||||
except:
|
||||
self.bot.log("Error drawing dealer hand (error code 1341a)")
|
||||
|
||||
table.paste(dealerHand,(800-self.BORDERSmol,20-self.BORDERSmol),dealerHand)
|
||||
|
||||
for x in range(len(hands)):
|
||||
key, value = list(hands.items())[x]
|
||||
key = self.bot.databaseFuncs.getName(key)
|
||||
#self.bot.log("Drawing "+key+"'s hand")
|
||||
userHand = self.drawHand(value["hand"],False,value["busted"],value["blackjack"])
|
||||
try:
|
||||
if value["split"] == 3:
|
||||
table.paste(userHand,(32-self.BORDERSmol+(384*self.PLACEMENT[x]),280-self.BORDERSmol),userHand)
|
||||
userOtherHand = self.drawHand(value["other hand"]["hand"],False,value["other hand"]["busted"],value["other hand"]["blackjack"])
|
||||
table.paste(userOtherHand,(32-self.BORDERSmol+(384*self.PLACEMENT[x]),420-self.BORDERSmol),userOtherHand)
|
||||
userThirdHand = self.drawHand(value["third hand"]["hand"],False,value["third hand"]["busted"],value["third hand"]["blackjack"])
|
||||
table.paste(userThirdHand,(32-self.BORDERSmol+(384*self.PLACEMENT[x]),560-self.BORDERSmol),userThirdHand)
|
||||
userFourthHand = self.drawHand(value["fourth hand"]["hand"],False,value["fourth hand"]["busted"],value["fourth hand"]["blackjack"])
|
||||
table.paste(userFourthHand,(32-self.BORDERSmol+(384*self.PLACEMENT[x]),700-self.BORDERSmol),userFourthHand)
|
||||
elif value["split"] == 2:
|
||||
table.paste(userHand,(32-self.BORDERSmol+(384*self.PLACEMENT[x]),420-self.BORDERSmol),userHand)
|
||||
userOtherHand = self.drawHand(value["other hand"]["hand"],False,value["other hand"]["busted"],value["other hand"]["blackjack"])
|
||||
table.paste(userOtherHand,(32-self.BORDERSmol+(384*self.PLACEMENT[x]),560-self.BORDERSmol),userOtherHand)
|
||||
userThirdHand = self.drawHand(value["third hand"]["hand"],False,value["third hand"]["busted"],value["third hand"]["blackjack"])
|
||||
table.paste(userThirdHand,(32-self.BORDERSmol+(384*self.PLACEMENT[x]),700-self.BORDERSmol),userThirdHand)
|
||||
elif value["split"] == 1:
|
||||
table.paste(userHand,(32-self.BORDERSmol+(384*self.PLACEMENT[x]),560-self.BORDERSmol),userHand)
|
||||
userOtherHand = self.drawHand(value["other hand"]["hand"],False,value["other hand"]["busted"],value["other hand"]["blackjack"])
|
||||
table.paste(userOtherHand,(32-self.BORDERSmol+(384*self.PLACEMENT[x]),700-self.BORDERSmol),userOtherHand)
|
||||
else:
|
||||
table.paste(userHand,(32-self.BORDERSmol+(384*self.PLACEMENT[x]),680-self.BORDERSmol),userHand)
|
||||
except:
|
||||
self.bot.log("Error drawing player hands (error code 1341b)")
|
||||
|
||||
textWidth = fnt.getsize(key)[0]
|
||||
if textWidth < 360:
|
||||
textImage.text((32+(384*self.PLACEMENT[x])+117-int(textWidth/2)-3,1010-3),key,fill=(0,0,0), font=fnt)
|
||||
textImage.text((32+(384*self.PLACEMENT[x])+117-int(textWidth/2)+3,1010-3),key,fill=(0,0,0), font=fnt)
|
||||
textImage.text((32+(384*self.PLACEMENT[x])+117-int(textWidth/2)-3,1010+3),key,fill=(0,0,0), font=fnt)
|
||||
textImage.text((32+(384*self.PLACEMENT[x])+117-int(textWidth/2)+3,1010+3),key,fill=(0,0,0), font=fnt)
|
||||
textImage.text((32+(384*self.PLACEMENT[x])+117-int(textWidth/2),1005),key,fill=(255,255,255), font=fnt)
|
||||
else:
|
||||
textWidth = fntSmol.getsize(key)[0]
|
||||
textImage.text((32+(384*self.PLACEMENT[x])+117-int(textWidth/2)-2,1020-2),key,fill=(0,0,0), font=fntSmol)
|
||||
textImage.text((32+(384*self.PLACEMENT[x])+117-int(textWidth/2)+2,1020-2),key,fill=(0,0,0), font=fntSmol)
|
||||
textImage.text((32+(384*self.PLACEMENT[x])+117-int(textWidth/2)-2,1020+2),key,fill=(0,0,0), font=fntSmol)
|
||||
textImage.text((32+(384*self.PLACEMENT[x])+117-int(textWidth/2)+2,1020+2),key,fill=(0,0,0), font=fntSmol)
|
||||
textImage.text((32+(384*self.PLACEMENT[x])+117-int(textWidth/2),1015),key,fill=(255,255,255), font=fntSmol)
|
||||
|
||||
self.bot.log("Saving table image")
|
||||
table.save("resources/games/blackjackTables/blackjackTable"+channel+".png")
|
||||
|
||||
return
|
||||
|
||||
def drawHand(self, hand, dealer, busted, blackjack):
|
||||
self.bot.log("Drawing hand "+str(hand)+", "+str(busted)+", "+str(blackjack))
|
||||
fnt = ImageFont.truetype('resources/fonts/futura-bold.ttf', 200)
|
||||
fnt2 = ImageFont.truetype('resources/fonts/futura-bold.ttf', 120)
|
||||
length = len(hand)
|
||||
background = Image.new("RGBA", ((self.BORDER*2)+691+(125*(length-1)),(self.BORDER*2)+1065),(0,0,0,0))
|
||||
textImage = ImageDraw.Draw(background)
|
||||
|
||||
if dealer:
|
||||
img = Image.open("resources/games/cards/"+hand[0].upper()+".png")
|
||||
#self.ROTATION = (random.randint(-20,20)/10.0)
|
||||
img = img.rotate(self.ROTATION,expand = 1)
|
||||
#self.PLACEMENT = [random.randint(-20,20),random.randint(-20,20)]
|
||||
background.paste(img,(self.BORDER+self.PLACEMENT[0],self.BORDER+self.PLACEMENT[1]),img)
|
||||
img = Image.open("resources/games/cards/red_back.png")
|
||||
#self.ROTATION = (random.randint(-20,20)/10.0)
|
||||
img = img.rotate(self.ROTATION,expand = 1)
|
||||
#self.PLACEMENT = [random.randint(-20,20),random.randint(-20,20)]
|
||||
background.paste(img,(125+self.BORDER+self.PLACEMENT[0],self.BORDER+self.PLACEMENT[1]),img)
|
||||
else:
|
||||
for x in range(length):
|
||||
img = Image.open("resources/games/cards/"+hand[x].upper()+".png")
|
||||
#self.ROTATION = (random.randint(-20,20)/10.0)
|
||||
img = img.rotate(self.ROTATION,expand = 1)
|
||||
#self.PLACEMENT = [random.randint(-20,20),random.randint(-20,20)]
|
||||
background.paste(img,(self.BORDER+(x*125)+self.PLACEMENT[0],self.BORDER+self.PLACEMENT[1]),img)
|
||||
|
||||
w, h = background.size
|
||||
textHeight = 290+self.BORDER
|
||||
|
||||
#self.bot.log("Drawing busted/blackjack")
|
||||
if busted:
|
||||
textWidth = fnt.getsize("BUSTED")[0]
|
||||
textImage.text((int(w/2)-int(textWidth/2)-10,textHeight+20-10),"BUSTED",fill=(0,0,0), font=fnt)
|
||||
textImage.text((int(w/2)-int(textWidth/2)+10,textHeight+20-10),"BUSTED",fill=(0,0,0), font=fnt)
|
||||
textImage.text((int(w/2)-int(textWidth/2)-10,textHeight+20+10),"BUSTED",fill=(0,0,0), font=fnt)
|
||||
textImage.text((int(w/2)-int(textWidth/2)+10,textHeight+20+10),"BUSTED",fill=(0,0,0), font=fnt)
|
||||
textImage.text((int(w/2)-int(textWidth/2)-5,textHeight-5),"BUSTED",fill=(255,255,255), font=fnt)
|
||||
textImage.text((int(w/2)-int(textWidth/2)+5,textHeight-5),"BUSTED",fill=(255,255,255), font=fnt)
|
||||
textImage.text((int(w/2)-int(textWidth/2)-5,textHeight+5),"BUSTED",fill=(255,255,225), font=fnt)
|
||||
textImage.text((int(w/2)-int(textWidth/2)+5,textHeight+5),"BUSTED",fill=(255,255,255), font=fnt)
|
||||
textImage.text((int(w/2)-int(textWidth/2),textHeight),"BUSTED",fill=(255,50,50), font=fnt)
|
||||
elif blackjack:
|
||||
textWidth = fnt2.getsize("BLACKJACK")[0]
|
||||
textImage.text((int(w/2)-int(textWidth/2)-6,textHeight+20-6),"BLACKJACK",fill=(0,0,0), font=fnt2)
|
||||
textImage.text((int(w/2)-int(textWidth/2)+6,textHeight+20-6),"BLACKJACK",fill=(0,0,0), font=fnt2)
|
||||
textImage.text((int(w/2)-int(textWidth/2)-6,textHeight+20+6),"BLACKJACK",fill=(0,0,0), font=fnt2)
|
||||
textImage.text((int(w/2)-int(textWidth/2)+6,textHeight+20+6),"BLACKJACK",fill=(0,0,0), font=fnt2)
|
||||
textImage.text((int(w/2)-int(textWidth/2)-3,textHeight-3),"BLACKJACK",fill=(255,255,255), font=fnt2)
|
||||
textImage.text((int(w/2)-int(textWidth/2)+3,textHeight-3),"BLACKJACK",fill=(255,255,255), font=fnt2)
|
||||
textImage.text((int(w/2)-int(textWidth/2)-3,textHeight+3),"BLACKJACK",fill=(255,255,255), font=fnt2)
|
||||
textImage.text((int(w/2)-int(textWidth/2)+3,textHeight+3),"BLACKJACK",fill=(255,255,255), font=fnt2)
|
||||
textImage.text((int(w/2)-int(textWidth/2),textHeight),"BLACKJACK",fill=(155,123,0), font=fnt2)
|
||||
|
||||
#self.bot.log("Returning resized image")
|
||||
return background.resize((int(w/3.5),int(h/3.5)),resample=Image.BILINEAR)
|
||||
|
||||
|
Reference in New Issue
Block a user