#๐Ÿ”’ Is there a better way to display keyboard in pygame?

5 messages ยท Page 1 of 1 (latest)

polar crag
#

I'm recreating wordle and I want the keyboard to be on the screen. I know about the keyboardlayouts library but I couldn't make it do what I needed. Instead, I created a list of dicts( self.keys = [ {"value": "Q", "rect" : (330, 450, 46, 60), "parameters" : (353, 480)}...]) and a function that iterates over them and prints them all:

def get_keyboard(self, color=(211,214,218), text_color="Black"):
    for key in self.keys:
       pygame.draw.rect(self.screen, color, pygame.Rect(key["rect"]),  border_radius=4)
       text = self.font.render(f'{key["value"]}', True, text_color)
       text_rect = text.get_rect(center=key["parameters"])
       self.screen.blit(text, text_rect)```
however, the way the whole thing looks makes me think there might be a better way to do that, but this is the best I could come up with considering the rows don't have any pattern as to where they start. Thoughts? Does the list look over-repetetive and possibly unnecessary?
limber cairnBOT
#

@polar crag

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

grave sail
#

i'd use namedtuple instead of dict, though ultimately you can't prevent data entries like this from looking some form of repetitive

limber cairnBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.