MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / keyloop

Function keyloop

tools/python-3.11.9-amd64/Tools/demo/life.py:166–255  ·  view source on GitHub ↗
(stdscr)

Source from the content-addressed store, hash-verified

164
165
166def keyloop(stdscr):
167 # Clear the screen and display the menu of keys
168 stdscr.clear()
169 stdscr_y, stdscr_x = stdscr.getmaxyx()
170 menu_y = (stdscr_y - 3) - 1
171 display_menu(stdscr, menu_y)
172
173 # If color, then initialize the color pairs
174 if curses.has_colors():
175 curses.init_pair(1, curses.COLOR_BLUE, 0)
176 curses.init_pair(2, curses.COLOR_CYAN, 0)
177 curses.init_pair(3, curses.COLOR_GREEN, 0)
178 curses.init_pair(4, curses.COLOR_MAGENTA, 0)
179 curses.init_pair(5, curses.COLOR_RED, 0)
180 curses.init_pair(6, curses.COLOR_YELLOW, 0)
181 curses.init_pair(7, curses.COLOR_WHITE, 0)
182
183 # Set up the mask to listen for mouse events
184 curses.mousemask(curses.BUTTON1_CLICKED)
185
186 # Allocate a subwindow for the Life board and create the board object
187 subwin = stdscr.subwin(stdscr_y - 3, stdscr_x, 0, 0)
188 board = LifeBoard(subwin, char=ord('*'))
189 board.display(update_board=False)
190
191 # xpos, ypos are the cursor's position
192 xpos, ypos = board.X // 2, board.Y // 2
193
194 # Main loop:
195 while True:
196 stdscr.move(1 + ypos, 1 + xpos) # Move the cursor
197 c = stdscr.getch() # Get a keystroke
198 if 0 < c < 256:
199 c = chr(c)
200 if c in ' \n':
201 board.toggle(ypos, xpos)
202 elif c in 'Cc':
203 erase_menu(stdscr, menu_y)
204 stdscr.addstr(menu_y, 6, ' Hit any key to stop continuously '
205 'updating the screen.')
206 stdscr.refresh()
207 # Activate nodelay mode; getch() will return -1
208 # if no keystroke is available, instead of waiting.
209 stdscr.nodelay(1)
210 while True:
211 c = stdscr.getch()
212 if c != -1:
213 break
214 stdscr.addstr(0, 0, '/')
215 stdscr.refresh()
216 board.display()
217 stdscr.addstr(0, 0, '+')
218 stdscr.refresh()
219
220 stdscr.nodelay(0) # Disable nodelay mode
221 display_menu(stdscr, menu_y)
222
223 elif c in 'Ee':

Callers 1

mainFunction · 0.85

Calls 11

displayMethod · 0.95
toggleMethod · 0.95
eraseMethod · 0.95
make_randomMethod · 0.95
display_menuFunction · 0.85
LifeBoardClass · 0.85
erase_menuFunction · 0.85
clearMethod · 0.45
moveMethod · 0.45
refreshMethod · 0.45
flashMethod · 0.45

Tested by

no test coverage detected