MCPcopy Index your code
hub / github.com/PySimpleGUI/PySimpleGUI / main

Function main

DemoPrograms/Demo_Game_Wordle.py:36–97  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

34 return sg.Input(value, key=key, font='Courier 22', size=(1,1), disabled_readonly_background_color='gray', border_width=1, p=1, enable_events=True, disabled=True)
35
36def main():
37 layout = [[sg.Text('Wordle', font='_ 20')],
38 [[TextChar('', (row, col)) for col in range(5)]for row in range(6)],
39 [sg.B('Enter', bind_return_key=True)],
40 [sg.Text('Or press enter', font='_ 10')]]
41
42 window = sg.Window("Wordle", layout, finalize=True, element_justification='c')
43
44 cur_row, correct = 0, False
45 [window[(cur_row, col)].update(disabled=False) for col in range(5)]
46 window.bind('<BackSpace>', '-BACKSPACE-')
47 while True:
48 event, values = window.read()
49 if event == sg.WIN_CLOSED:
50 break
51 if isinstance(event, tuple):
52 if len(values[event]):
53 row, col = event
54 char_input = values[event][-1]
55 if not char_input.isalpha(): # if not a character input, remove the input
56 window[event].update('')
57 else:
58 window[event].update(char_input.upper()[0]) # convert to uppercase
59 if col < 4:
60 window[(row, col+1)].set_focus() # Move to next position
61 elif event == 'Enter' and cur_row < 5:
62 guess = ''.join([values[(cur_row, j)] for j in range(5)])
63 answer2 = copy.copy(answer)
64 for i, letter in enumerate(guess):
65 if letter == answer2[i]:
66 window[(cur_row, i)].update(background_color='green', text_color='white')
67 answer2 = answer2.replace(letter, '*')
68 elif letter in answer2:
69 window[(cur_row, i)].update(background_color='#C9B359', text_color='white')
70 answer2 = answer2.replace(letter, '*')
71 else:
72 window[(cur_row, i)].update(background_color='gray', text_color='white')
73 if guess == answer:
74 correct = True
75 break
76 cur_row += 1 # Move to the next row
77 [window[(cur_row, col)].update(disabled=False) for col in range(5)] # Enable inputs on next row
78 window[(cur_row, 0)].set_focus() # Move to first position on row
79 elif event == 'Enter' and cur_row == 5:
80 correct = False
81 break
82 elif event == '-BACKSPACE-':
83 current_focus = window.find_element_with_focus()
84 current_key = current_focus.Key
85 if isinstance(current_key, tuple):
86 window[current_key].update('')
87 if current_key[1] > 0:
88 window[(current_key[0], current_key[1]-1)].set_focus()
89 window[(current_key[0], current_key[1]-1)].update('')
90
91
92 if correct:
93 sg.popup('You win!')

Callers 1

Calls 7

bindMethod · 0.95
readMethod · 0.95
closeMethod · 0.95
TextCharFunction · 0.85
set_focusMethod · 0.80
updateMethod · 0.45

Tested by

no test coverage detected