Hello,
I have this python code working in Thonny but want to add "WINNER!" TEXT and make a gpio pin go high for 1 second when the game is won.
The GAME_OVER section starts on line 137 not sure how to show line numbers here. Last line is 155.
I have this python code working in Thonny but want to add "WINNER!" TEXT and make a gpio pin go high for 1 second when the game is won.
The GAME_OVER section starts on line 137 not sure how to show line numbers here. Last line is 155.
Code:
import randomimport pygamedef load_dict(file_name): file = open(file_name) words = file.readlines() file.close() return [word[:5].upper() for word in words]DICT_GUESSING = load_dict("dictionary_english.txt")DICT_ANSWERS = load_dict("dictionary_wordle.txt")ANSWER = random.choice(DICT_ANSWERS)WIDTH = 600HEIGHT = 700MARGIN = 10T_MARGIN = 100B_MARGIN = 100LR_MARGIN = 100GREY = (70,70,80)GREEN = (6,214,160)YELLOW = (255,209,102)INPUT = ""GUESSES = []ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"UNGUESSED = ALPHABETGAME_OVER = Falsepygame.init()pygame.font.init()pygame.display.set_caption("Wordle")SQ_SIZE = (WIDTH-4*MARGIN-2*LR_MARGIN) // 5FONT = pygame.font.SysFont("free sans bold", SQ_SIZE)FONT_SMALL = pygame.font.SysFont("free sans bold", SQ_SIZE//2)def determine_unguessed_letters(guesses): guessed_letters = "".join(guesses) unguessed_letters = "" for letter in ALPHABET: if letter not in guessed_letters: unguessed_letters = unguessed_letters+letter return unguessed_lettersdef determine_color(guess, j): letter = guess[j] if letter == ANSWER[j]: return GREEN elif letter in ANSWER: n_target = ANSWER.count(letter) n_correct = 0 n_occurrence = 0 for i in range(5): if guess[i] == letter: if i <= j: n_occurrence += 1 if letter == ANSWER[j]: n_correct += 1 if n_target - n_correct - n_occurrence >=0: return YELLOW return GREY # create screenscreen = pygame.display.set_mode((WIDTH,HEIGHT))# animation loopanimating = Truewhile animating: # background screen.fill("white") # draw unguessed letters letters = FONT_SMALL.render(UNGUESSED, False, GREY) surface = letters.get_rect(center = (WIDTH//2, T_MARGIN//2)) screen.blit(letters, surface) # draw guesses y = T_MARGIN for i in range(6): x = LR_MARGIN for j in range(5): # square square = pygame.Rect(x, y, SQ_SIZE, SQ_SIZE) pygame.draw.rect(screen, GREY, square, width = 2, border_radius = 20) # letters/words that have already been guessed if i <len(GUESSES): color = determine_color(GUESSES[i], j) pygame.draw.rect(screen, color, square, border_radius = 20) letter = FONT.render(GUESSES[i][j], False, (255,255,255)) surface = letter.get_rect(center = (x+SQ_SIZE//2, y+SQ_SIZE//2)) screen.blit(letter, surface) # user text input (next guess) if i == len(GUESSES) and j < len(INPUT): letter = FONT.render(INPUT[j], False, GREY) surface = letter.get_rect(center = (x+SQ_SIZE//2, y+SQ_SIZE//2)) screen.blit(letter, surface) x += SQ_SIZE + MARGIN y += SQ_SIZE + MARGIN #show correct answer after game over if len(GUESSES) == 6 and GUESSES[5] != ANSWER: GAME_OVER = True letters = FONT.render(ANSWER, False, GREY) surface = letters.get_rect(center = (WIDTH//2, HEIGHT-B_MARGIN//2-MARGIN)) screen.blit(letters, surface) # update screen pygame.display.flip() #TRACK USER INTERACTION for event in pygame.event.get(): # closing the window stops the animation if event.type == pygame.QUIT: animating = False # user presses key elif event.type == pygame.KEYDOWN: # escape key to quit the animation if event.key == pygame.K_ESCAPE: animating = False # backspace to correct user input if event.key == pygame.K_BACKSPACE: if len(INPUT) > 0: INPUT = INPUT[:len(INPUT)-1] # RETURN KEY TO SUBMIT A GUESS elif event.key == pygame.K_RETURN: if len(INPUT) == 5 and INPUT in DICT_GUESSING: GUESSES.append(INPUT) UNGUESSED = determine_unguessed_letters(GUESSES) GAME_OVER = True if INPUT == ANSWER else False INPUT = "" # space bar to restart elif event.key == pygame.K_SPACE: GAME_OVER = False ANSWER = random.choice(DICT_ANSWERS) GUESSES = [] UNGUESSED = ALPHABET INPUT ="" # regular text input elif len(INPUT) < 5 and not GAME_OVER: INPUT = INPUT + event.unicode.upper()
Statistics: Posted by Smitty505000 — Sat Feb 03, 2024 3:27 pm