1

The below code makes the window not respond when started.. I wanted to make hangman game and I got the logic done and I was just trying to make the window pop up and its not responding. Also when I run the program, when I type in the letter and type another one then it erases the previous letter a writes the new letter with the underscores. How can I make it so that it keeps the previous letter and prints it with the new letter?

import pygame
pygame.init()

running  = True

window_width = 600
window_height = 600
window = pygame.display.set_mode((window_width, window_height))

clock = pygame.time.Clock()

word = "something"
while running:
    dt = clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        


    answer = ""
    guessed = []

    guessed.append(input("write your letter here -> "))

    for i in word:
        
        if i in guessed:
            answer += i + " "
        else:
            answer += "_ "

    print(answer)
    answer = ""
pygame.quit()
BryanKim
  • 41
  • 2
  • 5

3 Answers3

1

Your issue is here:

while running:
    dt = clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        


    answer = ""
    guessed = [] # <-----ISSUE

Everytime the while loop starts over, you're assigning the guessed list to an empty list. What you need to do is place the guessed = [] before the while like this:

import pygame
pygame.init()

running  = True

window_width = 600
window_height = 600
window = pygame.display.set_mode((window_width, window_height))

clock = pygame.time.Clock()

word = "something"
guessed = []
while running:
    pygame.display.update() # <---- Needs to be called every loop passing to update the window
    dt = clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        
    answer = ""

    guessed.append(input("write your letter here -> "))

    for i in word:
        
        if i in guessed:
            answer += i + " "
        else:
            answer += "_ "

    print(answer)
    answer = ""
pygame.quit()

You also need to update your window every time the while loop goes by. I added it in the code I provided above.

BWallDev
  • 345
  • 3
  • 13
1

Your game is not responding, because you ask for an input inside the application loop. input stops the application loop and waits for input to be confirmed. If you stop the application loop, the window stops responding. Use the KEYDOWN event to get an input in PyGame (see pygame.event):

for event in pygame.event.get():
    # [...]

    if event.type == pygame.KEYDOWN:
        guessed.append(event.unicode)

guessed has to be initialized before the application loop. Don't reset it in the loop:

import pygame
pygame.init()

running  = True

window_width = 600
window_height = 600
window = pygame.display.set_mode((window_width, window_height))

clock = pygame.time.Clock()

word = "something"
guessed = []

answer = ""
for c in word:
    answer += c + " " if c in guessed else "_ "
print(answer)

while running:
    dt = clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            guessed.append(event.unicode)
            answer = ""
            for c in word:
                answer += c + " " if c in guessed else "_ "
            print(answer)
   
pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • does that mean the program can't take inputs when there is an application window? – BryanKim Nov 15 '20 at 00:08
  • @BryanKim It can, but the window doesn't respond. You can get the input from the console in a different thread. Anyway, nobody takes input from the console in a graphical game. At the moment you don't need the window at all. However see [How to create a text input box with pygame?](https://stackoverflow.com/questions/46390231/how-to-create-a-text-input-box-with-pygame/64613666#64613666) – Rabbid76 Nov 15 '20 at 07:56
0

you should create another string variable that has the same amount of letters as the word but instead of letters, it has dashes. you can do that using this:

    guessedWord = []
    for letter in word:
        guessedWord.append("-")

then when the user guesses something you can substitute the guessedWord variable instead of the word. so basically you compare the guessed letter to the corresponding letter in the word and then if they were the same, you substitute the _ with the guessed letter. btw you don't have to put the guessed letters in a list unless you want to use them later.

    i = -1
    for letter in word:
        i += 1
        if letter == guessedLetter:
            guessedWord[i] = guessedLetter
    print(guessedLetter)
NinjaFaraz
  • 31
  • 3