✨ Framework for Hangman
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@ -154,6 +154,8 @@ resources/starWars/swcharacters.json
|
|||||||
resources/games/games.json
|
resources/games/games.json
|
||||||
resources/games/hexGames.json
|
resources/games/hexGames.json
|
||||||
resources/games/monopolyGames.json
|
resources/games/monopolyGames.json
|
||||||
|
resources/games/investments.json
|
||||||
|
resources/games/hangmanGames.json
|
||||||
resources/games/blackjackCards/
|
resources/games/blackjackCards/
|
||||||
resources/games/hilo/
|
resources/games/hilo/
|
||||||
resources/starWars/destinyPoints.txt
|
resources/starWars/destinyPoints.txt
|
||||||
@ -162,7 +164,7 @@ resources/games/oldImages/
|
|||||||
resources/games/4InARowBoards/
|
resources/games/4InARowBoards/
|
||||||
resources/games/hexBoards/
|
resources/games/hexBoards/
|
||||||
resources/games/monopolyBoards/
|
resources/games/monopolyBoards/
|
||||||
resources/games/investments.json
|
resources/games/hangmanBoards/
|
||||||
resources/lookup/monsters.json
|
resources/lookup/monsters.json
|
||||||
resources/lookup/spells.json
|
resources/lookup/spells.json
|
||||||
resources/movies.txt
|
resources/movies.txt
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import discord, asyncio, os, json
|
import discord, asyncio, os, json
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
from funcs import logThis, triviaAnswer, triviaCountPoints, triviaStart, deleteGame, checkBalance, giveMoney, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackHit, blackjackDouble, blackjackFinish, blackjackSplit, blackjackStand, parseInvest, blackjackLoop, fiar, runMonopoly, runHex
|
from funcs import logThis, triviaAnswer, triviaCountPoints, triviaStart, deleteGame, checkBalance, giveMoney, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackHit, blackjackDouble, blackjackFinish, blackjackSplit, blackjackStand, parseInvest, blackjackLoop, fiar, runMonopoly, runHex, runHangman
|
||||||
|
|
||||||
class GamesCog(commands.Cog):
|
class GamesCog(commands.Cog):
|
||||||
|
|
||||||
@ -273,5 +273,10 @@ class GamesCog(commands.Cog):
|
|||||||
async def monopoly(self, ctx, *, content = ""):
|
async def monopoly(self, ctx, *, content = ""):
|
||||||
await runMonopoly(ctx.message.channel,content,"#"+str(ctx.message.author.id))
|
await runMonopoly(ctx.message.channel,content,"#"+str(ctx.message.author.id))
|
||||||
|
|
||||||
|
# Runs a game of Hangman
|
||||||
|
@commands.command(aliases = ["hm"])
|
||||||
|
async def hangman(self, ctx, *, content = "start"):
|
||||||
|
await runHangman(ctx.message.channel,"#"+str(ctx.message.author.id),content)
|
||||||
|
|
||||||
def setup(client):
|
def setup(client):
|
||||||
client.add_cog(GamesCog(client))
|
client.add_cog(GamesCog(client))
|
@ -4,7 +4,7 @@ __all__ = ["helloFunc", "cap", "imageFunc", "logThis", "findWikiPage", "makeFile
|
|||||||
|
|
||||||
from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToCommand, fiarReactionTest, deleteGame, stopServer, addToDict, getName, getID, monopolyReactionTest
|
from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToCommand, fiarReactionTest, deleteGame, stopServer, addToDict, getName, getID, monopolyReactionTest
|
||||||
|
|
||||||
from .games import checkBalance, giveMoney, addMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseInvest, blackjackLoop, fiar, runMonopoly, runHex
|
from .games import checkBalance, giveMoney, addMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseInvest, blackjackLoop, fiar, runMonopoly, runHex, runHangman
|
||||||
|
|
||||||
from .lookup import spellFunc, monsterFunc
|
from .lookup import spellFunc, monsterFunc
|
||||||
|
|
||||||
|
@ -6,4 +6,4 @@ from .money import checkBalance, giveMoney, addMoney
|
|||||||
from .trivia import triviaCountPoints, triviaStart, triviaAnswer
|
from .trivia import triviaCountPoints, triviaStart, triviaAnswer
|
||||||
from .blackjack import blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit
|
from .blackjack import blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit
|
||||||
from .invest import parseInvest
|
from .invest import parseInvest
|
||||||
from .gameLoops import blackjackLoop, fiar, runMonopoly, runHex
|
from .gameLoops import blackjackLoop, fiar, runMonopoly, runHex, runHangman
|
||||||
|
@ -8,6 +8,7 @@ from .fourInARow import parseFourInARow, fourInARowAI
|
|||||||
from .blackjack import blackjackContinue, blackjackFinish
|
from .blackjack import blackjackContinue, blackjackFinish
|
||||||
from .hex import parseHex, hexAI
|
from .hex import parseHex, hexAI
|
||||||
from .monopoly import parseMonopoly, monopolyContinue
|
from .monopoly import parseMonopoly, monopolyContinue
|
||||||
|
from .hangman import parseHangman
|
||||||
|
|
||||||
# Deletes a message
|
# Deletes a message
|
||||||
async def deleteMessage(imageLocation,channel):
|
async def deleteMessage(imageLocation,channel):
|
||||||
@ -234,3 +235,16 @@ async def runMonopoly(channel, command, user):
|
|||||||
except:
|
except:
|
||||||
logThis("Image deleted before I could react to all of them")
|
logThis("Image deleted before I could react to all of them")
|
||||||
|
|
||||||
|
async def runHangman(channel,user,command = "start"):
|
||||||
|
try:
|
||||||
|
response, showImage, deleteImage, remainingLetters = parseHangman(str(channel.id),user,command)
|
||||||
|
except:
|
||||||
|
logThis("Error parsing command (error code 1701)")
|
||||||
|
await channel.send(response)
|
||||||
|
logThis(response,str(channel.id))
|
||||||
|
if showImage:
|
||||||
|
if deleteImage:
|
||||||
|
oldImage = await deleteMessage("hangman"+str(channel.id),channel)
|
||||||
|
oldImage = await channel.send(file = discord.File("resources/games/hangmanBoards/hangmanBoard"+str(channel.id)+".png"))
|
||||||
|
with open("resources/games/oldImages/hangman"+str(channel.id), "w") as f:
|
||||||
|
f.write(str(oldImage.id))
|
||||||
|
43
funcs/games/hangman.py
Normal file
43
funcs/games/hangman.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import json, urllib, random, datetime, string
|
||||||
|
|
||||||
|
from . import hangmanDraw
|
||||||
|
from funcs import getName, logThis
|
||||||
|
|
||||||
|
def hangmanStart(channel,user):
|
||||||
|
with open("resources/games/hangmanGames.json", "r") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
if channel not in data:
|
||||||
|
with urllib.request.urlopen("https://random-word-api.herokuapp.com/word?number=10") as p:
|
||||||
|
words = json.load(p)
|
||||||
|
word = list(random.choice(words).upper())
|
||||||
|
gameID = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
|
||||||
|
data[channel] = {"player" : user,"guessed letters" : [],"word" : word,"game ID" : gameID}
|
||||||
|
|
||||||
|
remainingLetters = list(string.ascii_uppercase)
|
||||||
|
|
||||||
|
with open("resources/games/hangmanGames.json", "w") as f:
|
||||||
|
json.dump(data,f)
|
||||||
|
|
||||||
|
try:
|
||||||
|
hangmanDraw.drawImage(channel)
|
||||||
|
except:
|
||||||
|
logThis("Error drawing image (error code 1710)")
|
||||||
|
return f"{getName(user)} started game of hangman.", True, False, remainingLetters
|
||||||
|
else:
|
||||||
|
return "There's already a game going on in the channel", False, False, []
|
||||||
|
|
||||||
|
def parseHangman(channel,user,command):
|
||||||
|
if command == "start":
|
||||||
|
return hangmanStart(channel,user)
|
||||||
|
elif command == "stop":
|
||||||
|
with open("resources/games/hangmanGames.json", "r") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
del data[channel]
|
||||||
|
with open("resources/games/hangmanGames.json", "w") as f:
|
||||||
|
json.dump(data,f,indent=4)
|
||||||
|
|
||||||
|
return "Game stopped.", False, False, []
|
||||||
|
else:
|
||||||
|
return "I didn't understand that", False, False, []
|
188
funcs/games/hangmanDraw.py
Normal file
188
funcs/games/hangmanDraw.py
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
import math, random
|
||||||
|
|
||||||
|
from PIL import ImageDraw, Image, ImageFont
|
||||||
|
from funcs import logThis
|
||||||
|
|
||||||
|
circleDegrees = 360
|
||||||
|
circleSize = 400
|
||||||
|
lineWidth = 40
|
||||||
|
|
||||||
|
bodySize = 700
|
||||||
|
limbSize = 300
|
||||||
|
armPosition = 200
|
||||||
|
|
||||||
|
manx = (limbSize*2)+lineWidth*4
|
||||||
|
many = (circleSize+bodySize+limbSize)+lineWidth*4
|
||||||
|
|
||||||
|
letterSize = 200
|
||||||
|
textSize = 50
|
||||||
|
|
||||||
|
gallowx, gallowy = 1200,2000
|
||||||
|
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)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
dev = preDev + devAcc
|
||||||
|
if dev > maxmin: dev = maxmin
|
||||||
|
elif dev < -maxmin: dev = -maxmin
|
||||||
|
return dev, devAcc
|
||||||
|
|
||||||
|
def badCircle():
|
||||||
|
background = Image.new("RGBA",(circleSize+(lineWidth*3),circleSize+(lineWidth*3)),color=(0,0,0,0))
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
d.ellipse([(x,y),(x+lineWidth,y+lineWidth)],fill=(0,0,0,255))
|
||||||
|
|
||||||
|
return background
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
if rotated:
|
||||||
|
x = lineWidth + pixel + devx
|
||||||
|
y = lineWidth + devy
|
||||||
|
else:
|
||||||
|
x = lineWidth + devx
|
||||||
|
y = lineWidth + pixel + devy
|
||||||
|
|
||||||
|
d.ellipse([(x,y),(x+lineWidth,y+lineWidth)],fill=(0,0,0,255))
|
||||||
|
|
||||||
|
return background
|
||||||
|
|
||||||
|
def drawMan(misses):
|
||||||
|
background = Image.new("RGBA",(manx,many),color=(0,0,0,0))
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
if misses >= 3:
|
||||||
|
limbs = random.sample(["rl","ll","ra","la"],min(misses-2,4))
|
||||||
|
else: limbs = []
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
return background
|
||||||
|
|
||||||
|
def badText(text, big):
|
||||||
|
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))
|
||||||
|
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)
|
||||||
|
|
||||||
|
lineTwo = badLine(gallowy-lineWidth*4)
|
||||||
|
background.paste(lineTwo,(int(gallowx*(0.75*goldenRatio)),lineWidth*2),lineTwo)
|
||||||
|
|
||||||
|
topLine = badLine(int(gallowy*0.30),True)
|
||||||
|
background.paste(topLine,(int(gallowx*(0.75*goldenRatio))-lineWidth,lineWidth*3),topLine)
|
||||||
|
|
||||||
|
lastLine = badLine(int(gallowy*0.125))
|
||||||
|
background.paste(lastLine,((int(gallowx*(0.75*goldenRatio))+int(gallowy*0.30)-lineWidth),lineWidth*3),lastLine)
|
||||||
|
return background
|
||||||
|
|
||||||
|
def drawImage(channel):
|
||||||
|
background = Image.new("RGBA",(1920,1080),color=backgroundColor)
|
||||||
|
try:
|
||||||
|
gallow = drawGallows()
|
||||||
|
except:
|
||||||
|
logThis("Error drawing gallows (error code 1711)")
|
||||||
|
|
||||||
|
gallow = gallow.resize((smolGallowx,smolGallowy))
|
||||||
|
|
||||||
|
try:
|
||||||
|
man = drawMan(6)
|
||||||
|
except:
|
||||||
|
logThis("Error drawing stick figure (error code 1712)")
|
||||||
|
|
||||||
|
try:
|
||||||
|
man = man.resize((smolManx,smolMany))
|
||||||
|
except:
|
||||||
|
logThis("Error resizing")
|
||||||
|
|
||||||
|
background.paste(gallow,(100,100),gallow)
|
||||||
|
background.paste(man,(280,210),man)
|
||||||
|
|
||||||
|
background.save("resources/games/hangmanBoards/hangmanBoard"+channel+".png")
|
@ -252,6 +252,8 @@ def stopServer():
|
|||||||
with open("resources/games/hexGames.json", "w") as f:
|
with open("resources/games/hexGames.json", "w") as f:
|
||||||
json.dump(emptyDict,f,indent=4)
|
json.dump(emptyDict,f,indent=4)
|
||||||
|
|
||||||
|
with open("resources/games/hangmanGames.json", "w") as f:
|
||||||
|
json.dump(emptyDict,f,indent=4)
|
||||||
|
|
||||||
def deleteGame(gameType,channel):
|
def deleteGame(gameType,channel):
|
||||||
with open("resources/games/games.json", "r") as f:
|
with open("resources/games/games.json", "r") as f:
|
||||||
|
BIN
resources/comic-sans.ttf
Normal file
BIN
resources/comic-sans.ttf
Normal file
Binary file not shown.
@ -125,3 +125,10 @@
|
|||||||
1640 - Unspecified monopolyDraw error
|
1640 - Unspecified monopolyDraw error
|
||||||
1641 - Error in getPosition()
|
1641 - Error in getPosition()
|
||||||
1650 - Error in monopolyRoll()
|
1650 - Error in monopolyRoll()
|
||||||
|
|
||||||
|
17 - Hangman
|
||||||
|
1700 - Unspecified error
|
||||||
|
1701 - Error parsing command
|
||||||
|
1710 - Error in drawImage()
|
||||||
|
1711 - Error in drawGallows()
|
||||||
|
1712 - Error in drawMan()
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 23 KiB |
@ -3,6 +3,7 @@
|
|||||||
"resources/starWars/swcharacters.json" : {},
|
"resources/starWars/swcharacters.json" : {},
|
||||||
"resources/games/hexGames.json": {},
|
"resources/games/hexGames.json": {},
|
||||||
"resources/games/monopolyGames.json": {},
|
"resources/games/monopolyGames.json": {},
|
||||||
|
"resources/games/hangmanGames.json": {},
|
||||||
"resources/users.json" : {},
|
"resources/users.json" : {},
|
||||||
"resources/games/investments.json" : {},
|
"resources/games/investments.json" : {},
|
||||||
"resources/games/games.json" : {
|
"resources/games/games.json" : {
|
||||||
@ -78,6 +79,7 @@
|
|||||||
"resources/games/oldImages",
|
"resources/games/oldImages",
|
||||||
"resources/games/blackjackCards",
|
"resources/games/blackjackCards",
|
||||||
"resources/games/hilo",
|
"resources/games/hilo",
|
||||||
"resources/games/monopolyBoards"
|
"resources/games/monopolyBoards",
|
||||||
|
"resources/games/hangmanBoards"
|
||||||
]
|
]
|
||||||
}
|
}
|
Reference in New Issue
Block a user