How can I adjust my program to toggle response display with key presses?

0
0
Asked By CodeNinja42 On

I'm working on a Python program using Pygame, and I'm trying to refine the logic for displaying answers. Currently, whenever I press a matching key, it immediately shows the response. What I want instead is for the key press to register first, and then only show the response when I press it a second time. I have the following code structure, but I'm running into issues with it. Any advice on how I can achieve this?

2 Answers

Answered By TechGuru99 On

The problem seems to be that you’re re-initializing the `guessed_letter` list each time `displayAnswers()` is called. You should make `guessed_letter` a global variable or pass it as a parameter so that it retains its values between function calls. This way, you can keep track of letters that have already been pressed. Have a look at your structure and see if maintaining its state helps!

GameDevGeek -

I understand your confusion! Originally, I placed `guessed_letter` outside the function too, thinking it would work better. But it’s tricky. Have you tried adjusting its scope and seeing if that remedies the issue?

LearningToCode -

I attempted some changes based on this advice, but I'm still hitting a wall. Maybe there’s another issue?

Answered By DevWizard2023 On

From what I gather, you could use a simple list to track inputs. You can display the response only if the key pressed is the last one in the list. For example, if your code looks like this: `list=[]` and when the key input `x` is pressed, you check if it's the last element in that list. If it is, then show the response. If not, indicate that the key was pressed and wait for the next click to show the response. Check this logic and see if it fits your needs!

CodeNinja42 -

That makes sense! I'm keeping track of which correct letters have been clicked using a list. If the player clicks again, I need to show the response based on that.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.