In this project I have successfully randomly generated words from random_words python package, and I added a tooltip to explain the definition of the random words with nltk python package. However, how can I fix the tooltip to fit the screen? Image 1
Another issue is too add the random generated words to the word bord in a randomized orientation. Image 2
enter code here
# imports
import pygame,sys
import random
import nltk
from nltk.corpus import wordnet
from random_words import RandomWords
nltk.download('wordnet')
nltk.download('omw-1.4')
rw = RandomWords()
# using pygame to make a game
pygame.init()
screen_width,screen_height = 600,600
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
WHITE = (255, 255, 255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
AddTo_BordList = []
class Show_Words(object):
def __init__(self,x,y):
self.x,self.y = x,y
self.display_text = rw.random_word()
AddTo_BordList.append(self.display_text)
self.font = pygame.font.Font('freesansbold.ttf', 12)
self.text = self.font.render(self.display_text.lower(), True, BLACK)
self.textRect = self.text.get_rect()
self.textRect.center = (self.x,self.y)
self.display_definition = wordnet.synsets(self.display_text)
def on_hovered(self):
mouse_pos = pygame.mouse.get_pos()
if self.textRect.collidepoint(mouse_pos):
try:
self.definition = self.font.render(self.display_definition[0].definition(), True, WHITE,BLACK)
self.textRect_def = self.definition.get_rect()
self.textRect_def.center = (self.x,self.y + 12)
screen.blit(self.definition,self.textRect_def)
except:
pass
def update(self):
screen.blit(self.text,self.textRect)
class Show_Letters(object):
def __init__(self,x,y):
self.x,self.y = x,y
self.letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
self.display_text = random.choice(self.letters)
self.ChangeLetter()
self.color = WHITE
self.font = pygame.font.Font('freesansbold.ttf', 15)
self.text = self.font.render(self.display_text.upper(), True, BLACK,self.color)
self.textRect = self.text.get_rect()
self.textRect.center = (self.x,self.y)
def ChangeLetter(self): # change this to work
global pick_word,change_to_letter
if len(AddTo_BordList) - 1 > 0:
if len(change_to_letter) > 0:
self.display_text = change_to_letter[0]
change_to_letter.remove(change_to_letter[0])
else:
AddTo_BordList.remove(pick_word)
pick_word = AddTo_BordList[0]
def on_hovered(self):
mouse_pos = pygame.mouse.get_pos()
if self.textRect.collidepoint(mouse_pos):
pass
def update(self):
global AddTo_BordList
pygame.draw.line(screen, BLACK, (450, 0), (450, 600))
screen.blit(self.text,self.textRect)
word_list = []
for y in range(12):
word = Show_Words(500,12 + (24 * y))
word_list.append(word)
pick_word = AddTo_BordList[0]
change_to_letter = list(pick_word[0])
letter_list = []
for x in range(21):
for y in range(29):
letter = Show_Letters(15 + (20 * x),15 + (20 * y))
letter_list.append(letter)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(WHITE)
for letter in letter_list:
letter.update()
letter.on_hovered()
for word in word_list:
word.update()
word.on_hovered()
clock.tick(60)
pygame.display.flip()
pygame.display.update()
with image 1 I have used the pygame width function for the text to move the rectangle however the image moves to much to the left of the screen or the definition tooltip is to big for the screen. with image 2 I could not even start to think of an idea to solve the issue. would I need to add the randomized text to a global list, then read through the list and make another list for each letter of the word? Do I need to index each letter and then print the letters? How would I randomize the position of the word?