game stuff

This commit is contained in:
NikolajDanger
2021-09-30 14:05:50 +02:00
parent 5330a51fe0
commit cbf2ca765e
12 changed files with 538 additions and 551 deletions

View File

@ -8,13 +8,13 @@ Deals with commands and logic for hangman games.
DrawHangman()
Draws the image shown to the player.
"""
import os
import datetime # Used for generating the game id
import string # string.ascii_uppercase used
import math # Used by DrawHangman(), mainly for drawing circles
import random # Used to draw poorly
import requests # Used for getting the word in Hangman.start()
import discord # Used for discord.file and type hints
import os
from discord_slash.utils.manage_components import (create_button,
create_actionrow)
@ -50,10 +50,10 @@ class Hangman():
The parameters to pass to every api call.
"""
self.bot = bot
self.__draw = DrawHangman(bot)
self.__API_url = "https://api.wordnik.com/v4/words.json/randomWords?" # pylint: disable=invalid-name
self._draw = DrawHangman(bot)
self._API_url = "https://api.wordnik.com/v4/words.json/randomWords?" # pylint: disable=invalid-name
api_key = self.bot.credentials["wordnik_key"]
self.__APIPARAMS = { # pylint: disable=invalid-name
self._APIPARAMS = { # pylint: disable=invalid-name
"hasDictionaryDef": True,
"minCorpusCount": 5000,
"maxCorpusCount": -1,
@ -78,7 +78,7 @@ class Hangman():
word = "-"
while "-" in word or "." in word:
response = requests.get(self.__API_url, params=self.__APIPARAMS)
response = requests.get(self._API_url, params=self._APIPARAMS)
word = list(response.json()[0]["word"].upper())
self.bot.log("Found the word \""+"".join(word)+"\"")
@ -87,7 +87,7 @@ class Hangman():
remaining_letters = list(string.ascii_uppercase)
self.__draw.draw_image(game_id, 0, word, guessed, [])
self._draw.draw_image(game_id, 0, word, guessed, [])
send_message = f"{ctx.author.display_name} started a game of hangman."
@ -219,7 +219,7 @@ class Hangman():
send_message = "Guessed {}. There were {} {}s in the word."
send_message = send_message.format(guess, correct_guess, guess)
self.__draw.draw_image(game_id, misses, word, guessed, guessed_letters)
self._draw.draw_image(game_id, misses, word, guessed, guessed_letters)
if misses == 6:
send_message += self.bot.long_strings["Hangman lost game"]
@ -325,36 +325,36 @@ class DrawHangman():
FONT
SMALLFONT
"""
self.__bot = bot
self._bot = bot
# pylint: disable=invalid-name
self.__CIRCLESIZE = 120
self.__LINEWIDTH = 12
self._CIRCLESIZE = 120
self._LINEWIDTH = 12
self.__BODYSIZE = 210
self.__LIMBSIZE = 60
self.__ARMPOSITION = 60
self._BODYSIZE = 210
self._LIMBSIZE = 60
self._ARMPOSITION = 60
self.__MANX = (self.__LIMBSIZE*2)
self.__MANY = (self.__CIRCLESIZE+self.__BODYSIZE+self.__LIMBSIZE)
MANPADDING = self.__LINEWIDTH*4
self.__MANX += MANPADDING
self.__MANY += MANPADDING
self._MANX = (self._LIMBSIZE*2)
self._MANY = (self._CIRCLESIZE+self._BODYSIZE+self._LIMBSIZE)
MANPADDING = self._LINEWIDTH*4
self._MANX += MANPADDING
self._MANY += MANPADDING
self.__LETTERLINELENGTH = 90
self.__LETTERLINEDISTANCE = 30
self._LETTERLINELENGTH = 90
self._LETTERLINEDISTANCE = 30
self.__GALLOWX, self.__GALLOWY = 360, 600
self.__PHI = 1-(1 / ((1 + 5 ** 0.5) / 2))
self._GALLOWX, self._GALLOWY = 360, 600
self._PHI = 1-(1 / ((1 + 5 ** 0.5) / 2))
LETTERSIZE = 75 # Wrong guesses letter size
WORDSIZE = 70 # Correct guesses letter size
FONTPATH = "gwendolyn/resources/fonts/comic-sans-bold.ttf"
self.__FONT = ImageFont.truetype(FONTPATH, LETTERSIZE)
self.__SMALLFONT = ImageFont.truetype(FONTPATH, WORDSIZE)
self._FONT = ImageFont.truetype(FONTPATH, LETTERSIZE)
self._SMALLFONT = ImageFont.truetype(FONTPATH, WORDSIZE)
# pylint: enable=invalid-name
def __deviate(self, pre_deviance: int, pre_deviance_accuracy: int,
def _deviate(self, pre_deviance: int, pre_deviance_accuracy: int,
position_change: float, maxmin: int,
max_acceleration: float):
random_deviance = random.uniform(-position_change, position_change)
@ -371,14 +371,14 @@ class DrawHangman():
deviance = -maxmin
return deviance, deviance_accuracy
def __bad_circle(self):
circle_padding = (self.__LINEWIDTH*3)
image_width = self.__CIRCLESIZE+circle_padding
def _bad_circle(self):
circle_padding = (self._LINEWIDTH*3)
image_width = self._CIRCLESIZE+circle_padding
image_size = (image_width, image_width)
background = Image.new("RGBA", image_size, color=(0, 0, 0, 0))
drawer = ImageDraw.Draw(background, "RGBA")
middle = (self.__CIRCLESIZE+(self.__LINEWIDTH*3))/2
middle = (self._CIRCLESIZE+(self._LINEWIDTH*3))/2
deviance_x = 0
deviance_y = 0
deviance_accuracy_x = 0
@ -387,96 +387,96 @@ class DrawHangman():
degrees_amount = 360 + random.randint(-10, 30)
for degree in range(degrees_amount):
deviance_x, deviance_accuracy_x = self.__deviate(
deviance_x, deviance_accuracy_x = self._deviate(
deviance_x,
deviance_accuracy_x,
self.__LINEWIDTH/100,
self.__LINEWIDTH,
self._LINEWIDTH/100,
self._LINEWIDTH,
0.03
)
deviance_y, deviance_accuracy_y = self.__deviate(
deviance_y, deviance_accuracy_y = self._deviate(
deviance_y,
deviance_accuracy_y,
self.__LINEWIDTH/100,
self.__LINEWIDTH,
self._LINEWIDTH/100,
self._LINEWIDTH,
0.03
)
radians = math.radians(degree+start)
circle_x = (math.cos(radians) * (self.__CIRCLESIZE/2))
circle_y = (math.sin(radians) * (self.__CIRCLESIZE/2))
circle_x = (math.cos(radians) * (self._CIRCLESIZE/2))
circle_y = (math.sin(radians) * (self._CIRCLESIZE/2))
position_x = middle + circle_x - (self.__LINEWIDTH/2) + deviance_x
position_y = middle + circle_y - (self.__LINEWIDTH/2) + deviance_y
position_x = middle + circle_x - (self._LINEWIDTH/2) + deviance_x
position_y = middle + circle_y - (self._LINEWIDTH/2) + deviance_y
circle_position = [
(position_x, position_y),
(position_x+self.__LINEWIDTH, position_y+self.__LINEWIDTH)
(position_x+self._LINEWIDTH, position_y+self._LINEWIDTH)
]
drawer.ellipse(circle_position, fill=(0, 0, 0, 255))
return background
def __bad_line(self, length: int, rotated: bool = False):
def _bad_line(self, length: int, rotated: bool = False):
if rotated:
width, height = length+self.__LINEWIDTH*3, self.__LINEWIDTH*3
width, height = length+self._LINEWIDTH*3, self._LINEWIDTH*3
else:
width, height = self.__LINEWIDTH*3, length+self.__LINEWIDTH*3
width, height = self._LINEWIDTH*3, length+self._LINEWIDTH*3
background = Image.new("RGBA", (width, height), color=(0, 0, 0, 0))
drawer = ImageDraw.Draw(background, "RGBA")
possible_deviance = int(self.__LINEWIDTH/3)
possible_deviance = int(self._LINEWIDTH/3)
deviance_x = random.randint(-possible_deviance, possible_deviance)
deviance_y = 0
deviance_accuracy_x = 0
deviance_accuracy_y = 0
for pixel in range(length):
deviance_x, deviance_accuracy_x = self.__deviate(
deviance_x, deviance_accuracy_x = self._deviate(
deviance_x,
deviance_accuracy_x,
self.__LINEWIDTH/1000,
self.__LINEWIDTH,
self._LINEWIDTH/1000,
self._LINEWIDTH,
0.004
)
deviance_y, deviance_accuracy_y = self.__deviate(
deviance_y, deviance_accuracy_y = self._deviate(
deviance_y,
deviance_accuracy_y,
self.__LINEWIDTH/1000,
self.__LINEWIDTH,
self._LINEWIDTH/1000,
self._LINEWIDTH,
0.004
)
if rotated:
position_x = self.__LINEWIDTH + pixel + deviance_x
position_y = self.__LINEWIDTH + deviance_y
position_x = self._LINEWIDTH + pixel + deviance_x
position_y = self._LINEWIDTH + deviance_y
else:
position_x = self.__LINEWIDTH + deviance_x
position_y = self.__LINEWIDTH + pixel + deviance_y
position_x = self._LINEWIDTH + deviance_x
position_y = self._LINEWIDTH + pixel + deviance_y
circle_position = [
(position_x, position_y),
(position_x+self.__LINEWIDTH, position_y+self.__LINEWIDTH)
(position_x+self._LINEWIDTH, position_y+self._LINEWIDTH)
]
drawer.ellipse(circle_position, fill=(0, 0, 0, 255))
return background
def __draw_man(self, misses: int, seed: str):
def _draw_man(self, misses: int, seed: str):
random.seed(seed)
man_size = (self.__MANX, self.__MANY)
man_size = (self._MANX, self._MANY)
background = Image.new("RGBA", man_size, color=(0, 0, 0, 0))
if misses >= 1:
head = self.__bad_circle()
paste_x = (self.__MANX-(self.__CIRCLESIZE+(self.__LINEWIDTH*3)))//2
head = self._bad_circle()
paste_x = (self._MANX-(self._CIRCLESIZE+(self._LINEWIDTH*3)))//2
paste_position = (paste_x, 0)
background.paste(head, paste_position, head)
if misses >= 2:
body = self.__bad_line(self.__BODYSIZE)
paste_x = (self.__MANX-(self.__LINEWIDTH*3))//2
paste_position = (paste_x, self.__CIRCLESIZE)
body = self._bad_line(self._BODYSIZE)
paste_x = (self._MANX-(self._LINEWIDTH*3))//2
paste_position = (paste_x, self._CIRCLESIZE)
background.paste(body, paste_position, body)
if misses >= 3:
@ -487,30 +487,30 @@ class DrawHangman():
random.seed(seed)
for limb in limbs:
limb_drawing = self.__bad_line(self.__LIMBSIZE, True)
x_position = (self.__MANX-(self.__LINEWIDTH*3))//2
limb_drawing = self._bad_line(self._LIMBSIZE, True)
x_position = (self._MANX-(self._LINEWIDTH*3))//2
if limb[1] == "a":
rotation = random.randint(-45, 45)
shift = math.sin(math.radians(rotation))
line_length = self.__LIMBSIZE+(self.__LINEWIDTH*3)
line_length = self._LIMBSIZE+(self._LINEWIDTH*3)
compensation = int(shift*line_length)
limb_drawing = limb_drawing.rotate(rotation, expand=1)
y_position = self.__CIRCLESIZE + self.__ARMPOSITION
y_position = self._CIRCLESIZE + self._ARMPOSITION
if limb == "ra":
compensation = min(-compensation, 0)
else:
x_position -= self.__LIMBSIZE
x_position -= self._LIMBSIZE
compensation = min(compensation, 0)
y_position += compensation
else:
rotation = random.randint(-15, 15)
y_position = self.__CIRCLESIZE+self.__BODYSIZE-self.__LINEWIDTH
y_position = self._CIRCLESIZE+self._BODYSIZE-self._LINEWIDTH
if limb == "rl":
limb_drawing = limb_drawing.rotate(rotation-45, expand=1)
else:
x_position += -limb_drawing.size[0]+self.__LINEWIDTH*3
x_position += -limb_drawing.size[0]+self._LINEWIDTH*3
limb_drawing = limb_drawing.rotate(rotation+45, expand=1)
paste_position = (x_position, y_position)
@ -518,11 +518,11 @@ class DrawHangman():
return background
def __bad_text(self, text: str, big: bool, color: tuple = (0, 0, 0, 255)):
def _bad_text(self, text: str, big: bool, color: tuple = (0, 0, 0, 255)):
if big:
font = self.__FONT
font = self._FONT
else:
font = self.__SMALLFONT
font = self._SMALLFONT
width, height = font.getsize(text)
img = Image.new("RGBA", (width, height), color=(0, 0, 0, 0))
drawer = ImageDraw.Draw(img, "RGBA")
@ -530,59 +530,59 @@ class DrawHangman():
drawer.text((0, 0), text, font=font, fill=color)
return img
def __draw_gallows(self):
gallow_size = (self.__GALLOWX, self.__GALLOWY)
def _draw_gallows(self):
gallow_size = (self._GALLOWX, self._GALLOWY)
background = Image.new("RGBA", gallow_size, color=(0, 0, 0, 0))
bottom_line = self.__bad_line(int(self.__GALLOWX * 0.75), True)
bottom_line_x = int(self.__GALLOWX * 0.125)
bottom_line_y = self.__GALLOWY-(self.__LINEWIDTH*4)
bottom_line = self._bad_line(int(self._GALLOWX * 0.75), True)
bottom_line_x = int(self._GALLOWX * 0.125)
bottom_line_y = self._GALLOWY-(self._LINEWIDTH*4)
paste_position = (bottom_line_x, bottom_line_y)
background.paste(bottom_line, paste_position, bottom_line)
line_two = self.__bad_line(self.__GALLOWY-self.__LINEWIDTH*6)
line_two_x = int(self.__GALLOWX*(0.75*self.__PHI))
line_two_y = self.__LINEWIDTH*2
line_two = self._bad_line(self._GALLOWY-self._LINEWIDTH*6)
line_two_x = int(self._GALLOWX*(0.75*self._PHI))
line_two_y = self._LINEWIDTH*2
paste_position = (line_two_x, line_two_y)
background.paste(line_two, paste_position, line_two)
top_line = self.__bad_line(int(self.__GALLOWY*0.30), True)
paste_x = int(self.__GALLOWX*(0.75*self.__PHI))-self.__LINEWIDTH
paste_position = (paste_x, self.__LINEWIDTH*3)
top_line = self._bad_line(int(self._GALLOWY*0.30), True)
paste_x = int(self._GALLOWX*(0.75*self._PHI))-self._LINEWIDTH
paste_position = (paste_x, self._LINEWIDTH*3)
background.paste(top_line, paste_position, top_line)
last_line = self.__bad_line(int(self.__GALLOWY*0.125))
paste_x += int(self.__GALLOWY*0.30)
background.paste(last_line, (paste_x, self.__LINEWIDTH*3), last_line)
last_line = self._bad_line(int(self._GALLOWY*0.125))
paste_x += int(self._GALLOWY*0.30)
background.paste(last_line, (paste_x, self._LINEWIDTH*3), last_line)
return background
def __draw_letter_lines(self, word: str, guessed: list, misses: int):
letter_width = self.__LETTERLINELENGTH+self.__LETTERLINEDISTANCE
def _draw_letter_lines(self, word: str, guessed: list, misses: int):
letter_width = self._LETTERLINELENGTH+self._LETTERLINEDISTANCE
image_width = letter_width*len(word)
image_size = (image_width, self.__LETTERLINELENGTH+self.__LINEWIDTH*3)
image_size = (image_width, self._LETTERLINELENGTH+self._LINEWIDTH*3)
letter_lines = Image.new("RGBA", image_size, color=(0, 0, 0, 0))
for i, letter in enumerate(word):
line = self.__bad_line(self.__LETTERLINELENGTH, True)
paste_x = i*(self.__LETTERLINELENGTH+self.__LETTERLINEDISTANCE)
paste_position = (paste_x, self.__LETTERLINELENGTH)
line = self._bad_line(self._LETTERLINELENGTH, True)
paste_x = i*(self._LETTERLINELENGTH+self._LETTERLINEDISTANCE)
paste_position = (paste_x, self._LETTERLINELENGTH)
letter_lines.paste(line, paste_position, line)
if guessed[i]:
letter_drawing = self.__bad_text(letter, True)
letter_width = self.__FONT.getsize(letter)[0]
letter_x = i*(self.__LETTERLINELENGTH+self.__LETTERLINEDISTANCE)
letter_drawing = self._bad_text(letter, True)
letter_width = self._FONT.getsize(letter)[0]
letter_x = i*(self._LETTERLINELENGTH+self._LETTERLINEDISTANCE)
letter_x -= (letter_width//2)
letter_x += (self.__LETTERLINELENGTH//2)+(self.__LINEWIDTH*2)
letter_x += (self._LETTERLINELENGTH//2)+(self._LINEWIDTH*2)
letter_lines.paste(
letter_drawing,
(letter_x, 0),
letter_drawing
)
elif misses == 6:
letter_drawing = self.__bad_text(letter, True, (242, 66, 54))
letter_width = self.__FONT.getsize(letter)[0]
letter_x = i*(self.__LETTERLINELENGTH+self.__LETTERLINEDISTANCE)
letter_drawing = self._bad_text(letter, True, (242, 66, 54))
letter_width = self._FONT.getsize(letter)[0]
letter_x = i*(self._LETTERLINELENGTH+self._LETTERLINEDISTANCE)
letter_x -= (letter_width//2)
letter_x += (self.__LETTERLINELENGTH//2)+(self.__LINEWIDTH*2)
letter_x += (self._LETTERLINELENGTH//2)+(self._LINEWIDTH*2)
letter_lines.paste(
letter_drawing,
(letter_x, 0),
@ -591,7 +591,7 @@ class DrawHangman():
return letter_lines
def __shortest_dist(self, positions: list, new_position: tuple):
def _shortest_dist(self, positions: list, new_position: tuple):
shortest_dist = math.inf
for i, j in positions:
x_distance = abs(i-new_position[0])
@ -601,18 +601,18 @@ class DrawHangman():
shortest_dist = dist
return shortest_dist
def __draw_misses(self, guesses: list, word: str):
def _draw_misses(self, guesses: list, word: str):
background = Image.new("RGBA", (600, 400), color=(0, 0, 0, 0))
pos = []
for guess in guesses:
if guess not in word:
placed = False
while not placed:
letter = self.__bad_text(guess, True)
w, h = self.__FONT.getsize(guess)
letter = self._bad_text(guess, True)
w, h = self._FONT.getsize(guess)
x = random.randint(0, 600-w)
y = random.randint(0, 400-h)
if self.__shortest_dist(pos, (x, y)) > 70:
if self._shortest_dist(pos, (x, y)) > 70:
pos.append((x, y))
background.paste(letter, (x, y), letter)
placed = True
@ -628,27 +628,27 @@ class DrawHangman():
channel: str
The id of the channel the game is in.
"""
self.__bot.log("Drawing hangman image")
self._bot.log("Drawing hangman image")
random.seed(game_id)
background = Image.open("gwendolyn/resources/paper.jpg")
gallow = self.__draw_gallows()
man = self.__draw_man(misses, game_id)
gallow = self._draw_gallows()
man = self._draw_man(misses, game_id)
random.seed(game_id)
letter_line_parameters = [word, guessed, misses]
letter_lines = self.__draw_letter_lines(*letter_line_parameters)
letter_lines = self._draw_letter_lines(*letter_line_parameters)
random.seed(game_id)
misses = self.__draw_misses(list(guessed_letters), word)
misses = self._draw_misses(list(guessed_letters), word)
background.paste(gallow, (100, 100), gallow)
background.paste(man, (300, 210), man)
background.paste(letter_lines, (120, 840), letter_lines)
background.paste(misses, (600, 150), misses)
misses_text = self.__bad_text("MISSES", False)
misses_text = self._bad_text("MISSES", False)
misses_text_width = misses_text.size[0]
background.paste(misses_text, (850-misses_text_width//2, 50), misses_text)