BryanKim
BryanKim

Reputation: 41

Pygame Window not Responding after few seconds

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()

Upvotes: 1

Views: 463

Answers (3)

Rabbid76
Rabbid76

Reputation: 210880

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()

Upvotes: 1

NinjaFaraz
NinjaFaraz

Reputation: 29

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)

Upvotes: 0

BWallDev
BWallDev

Reputation: 375

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.

Upvotes: 1

Related Questions