-2

I'm trying to build a Wordle clone, a word game where one gets 6 chances to guess a 5 letter word. The game itself works but when I try to show the definition of the word at the end of the game, my whole game gets stuck.

My code is this:

import pygame
import pygame.display
import sys
import random
from words import *
from PyDictionary import PyDictionary

pygame.init()


# a py file with list of words
CORRECT_WORD = WORDS
CORRECT_WORD_LIST = list(CORRECT_WORD)
word = random.choice(CORRECT_WORD_LIST)



# number of attempts, 6 in total
attempts = 0
# a list to store the previous guesses
guesses = [[]] * 6
cur_guess = []
cur_guess_str = ""
current_letter_bg_x = 110



game_result = ""

# get the meaning of the word
def get_meaning(word):
    dicto = PyDictionary()
    meaning = dicto.meaning(word)
    if meaning == "":
        meaning = "No definition found"
    return meaning

meaning = get_meaning(word)


def blit_text(surface, text, pos, font):
    """Multi-line text blit from https://stackoverflow.com/a/42015712/2280890"""
   

def check_guess(guess):
    # Check if letter is Green, Yellow or grey if not in word
    # trimmed, for not being part of the problem
           

    attempts += 1
    cur_guess = []
    cur_guess_str = ""
    current_letter_bg_x = 110

def play_again():    
    SCREEN.blit(BACKGROUND, BG_RECT)
    
    play_again_text = play_again_font.render("ENTER to Play Again? or Q to Quit!", True, "#FCFCFC")
    
    pygame.display.update()

  

    word_text = play_again_font.render(f"{word.upper()}", True, "#FFB90F")
    

    SCREEN.blit(play_again_text, play_again_rect)
    SCREEN.blit(word_text, word_rect)
    pygame.display.flip()


def reset():
    # Reset all global variables
   

I've trimmed/removed a bunch of lines relating to creating/drawing letters and backspace etc.

Up until this point everything works as expected. The letters, if in correct position get colored as expected.

My problem starts here. Instead of only showing the definition after 6 attempts it shows the meaning as soon as the game starts. This is the block of code that breaks the game:

    for i in enumerate(guesses[attempts]):
        if i == 6 and game_result is not "W":
            word = random.choice(CORRECT_WORD_LIST)
            meaning = get_meaning(word)

    txt = f"{meaning}"
    txt_pos = (40, 70)
    window.blit(BACKGROUND, [-317, -300])
    f"{blit_text(window, txt, txt_pos, sys_font)}", True, "#FFB90F"
    pygame.display.update

If I omit that block the game runs as expected and if I leave as below, it works as expected.

Game

    while running:
        if game_result != "":
            play_again()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    if game_result != "":
                        reset()

                    else:
                        if len(cur_guess_str) == 5 and cur_guess_str.lower() in WORDS:
                            check_guess(cur_guess)
                elif event.key == pygame.K_BACKSPACE:
                    if len(cur_guess_str) > 0:
                        delete_letter()

                else:
                    key_pressed = event.unicode.upper()
                    if key_pressed in "QWERTYUIOPASDFGHJKLZXCVBNM" and key_pressed != "":
                        if len(cur_guess_str) < 5:
                            new_letter()
       

Can someone help me understand why my logic/reasoning is flawed? The block of code that is causing issues I insert it right after ### game, in between while running: and right before

if game_result != "":
    play_again()
kip
  • 145
  • 7
  • 1
    `pygame.display.update` probably needs to be `pygame.display.update()`. It may not solve your problem, but it's one thing that stands out as being likely wrong. And the line above that is also weird: you create a tuple of string, bool and string, but never assign nor use it. – 9769953 May 01 '22 at 11:04
  • 2
    " edit, why am I getting a downvote for a sincere help request? ": possibly because you put in dozens of line of code, without a clear attempt at minimizing the code to the actual problem. Create a [mcve] for your problem. Please read through the help of StackOverflow to learn how to ask a good, answerable question. (Also, that remark is not part of your problem, so don't put it in the question. Put it in a comment.) – 9769953 May 01 '22 at 11:07
  • but my minimal reproducible example depends on the whole code, in order to explain the problem. If I just put in the problematic part it wouldn't show what the problem is. – kip May 01 '22 at 11:09
  • 1
    From a quick glance, I can probably remove dozens of lines of code already. Some lines don't do anything (see my first comment), and setting the font style etc is very unlikely to be related to your problem (using the defaults, without setting anything, will cause the problem as well). You probably also don't need a user to ask for guesses, you can just set six (incorrect) guesses instead. – 9769953 May 01 '22 at 11:11
  • In addition to trimming down your code, it would also help to show precisely where you're inserting the troublesome added section. You're much more familiar with your code than anyone else here; anything you can do ahead of time to limit the amount of reading, digesting, and editing required of potential answerers will make their job easier and increase your chances of getting a helpful answer. – CrazyChucky May 01 '22 at 13:47
  • Thank you CrazyChucky. I'm trying to trim as much as possible to improve readability while maintaining the bare minimum for it to be reproducible. – kip May 01 '22 at 14:15

1 Answers1

1
    while True:
    meaning = ""
        for i in range(len(guesses[attempts])):
            if i == 6 and game_result is not "W":
                word = random.choice(CORRECT_WORD_LIST)
                meaning = get_meaning(word)
    
        txt = f"{meaning}"
        txt_pos = (40, 70)
        window.blit(BACKGROUND, [-317, -300])
        f"{blit_text(window, txt, txt_pos, sys_font)}", True, "#FFB90F"
        pygame.display.update()

for the code above you have put while running but you never change running so wouldn`t it be better to do while True. Also if you put while True a better way of ending the game is to just do break instead of pygame.quit() and sys.exit(). Also can I see the output that prints when the game breaks please?

Don't Work (I have edited the code and it prints because as soon as the game starts to run you have put txt=f"{meaning}" by moving it across that only happens when i == 6 and game_result is not "W". I don't know if this will work but its worth a try.)

If that doesn`t work from what I can see you don't use enumerate so I would replace it with range(len(guesses[attempts]))

abc bcd
  • 131
  • 1
  • 10
  • Thanks for taking the time to answer. The game "breaks" in the sense that it displays the meaning as soon as the first key (of the first word is pressed), so instead of going through 6 attempts, and if the word is not guessed, then showing the meaning. I have also tried while True, with the same results. – kip May 01 '22 at 11:06
  • @kip try again I have update the code and my answer – abc bcd May 01 '22 at 11:18
  • Thanks again abc, had indeed tried moving the position without success. Now it doesn't display the meaning at all, and hitting ENTER does not start the game again, only Q works. I positioned it there because I thought it should belong to the for loop. – kip May 01 '22 at 11:18
  • I super appreciate you taking the time. I've edited the code to reflect your suggestions, but it just freezes. No error is thrown, I have to Q out, the RETURN button doesn't work and I only get the Ctrl+C msg; KeyboardInterrupt Process finished with exit code -1073741510 (0xC000013A: interrupted by Ctrl+C) – kip May 01 '22 at 11:29
  • the above answer didn't resolve my issue but I don't see the need for a downvote. – kip May 01 '22 at 12:46