MCPcopy Create free account
hub / github.com/ActiveState/code / get_command

Function get_command

recipes/Python/580680_Tetris/recipe-580680.py:125–147  ·  view source on GitHub ↗

if a command is entered, return it; otherwise raise the exception Timeout

(next_fall_time)

Source from the content-addressed store, hash-verified

123 return result
124
125def get_command(next_fall_time):
126 "if a command is entered, return it; otherwise raise the exception Timeout"
127 while True: # until a timeout occurs or a command is found:
128 timeout = next_fall_time - time.time()
129 if timeout > 0.0:
130 (r, w, e) = select.select([ sys.stdin ], [], [], timeout)
131 else:
132 raise Timeout()
133 if sys.stdin not in r: # not input on stdin?
134 raise Timeout()
135 key = os.read(sys.stdin.fileno(), 1)
136 if key == 'j':
137 return left
138 elif key == 'l':
139 return right
140 elif key == 'k':
141 return turn
142 elif key == ' ':
143 return down
144 elif key == 'q':
145 return quit
146 else: # any other key: ignore
147 pass
148
149def place_block(block, coordinates, color):
150 "if the given block can be placed in the shaft at the given coordinates"\

Callers 1

play_tetrisFunction · 0.85

Calls 5

TimeoutClass · 0.70
timeMethod · 0.45
selectMethod · 0.45
readMethod · 0.45
filenoMethod · 0.45

Tested by

no test coverage detected