سؤال

I'm using Python and developing a trivia game with livewires. I had a theory to have a default question card as an image display. And then have the console print text into accurately positioned locations. The questions, answers, and explanations would be loaded from a text file, and the program would randomize which ones were loaded. But if this theory would work, the program could virtually have as many questions, or different topics as one wanted, and would be easily edited from the notepad file. I guess my question is, is there a way to accurately position printed console text, and allow it to be placed on top of an image inside a pygame window?

هل كانت مفيدة؟

المحلول

Loading the questions from text file sounds good. Using pygame you can position text anywhere on the screen, it's a two step process:

question = "Is this a question?"

# Render the text:
font = pygame.font.SysFont("Arial", 20)
text_surface = font.render(question, true, (0,0,0))

# Blit the text to the screen
text_rect = text_surface.get_rect()
text_rect.center = (100,100)
screen.blit(text_surface, text_rect.topleft)

This draws the question to the desired position.

If you want to handle multiline questions you could use a special character in your file to indicate the line break (example a pipe |) or you can make an algorithm to find out automatically where to place the line breaks. Next you render and blit line by line adding some pixels to the y coordinate.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top