diff options
Diffstat (limited to 'robots/utils.py')
-rw-r--r-- | robots/utils.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/robots/utils.py b/robots/utils.py new file mode 100644 index 0000000..4473a6b --- /dev/null +++ b/robots/utils.py @@ -0,0 +1,39 @@ +from random import sample +import time + +def empty_map(width, height, n_spawns): + board = [['.'] * width for y in range(height)] + + all_positions = [ + (x, y) + for x in range(width) + for y in range(height) + ] + spawns = sample(all_positions, n_spawns) + + for i, (x, y) in enumerate(spawns): + board[y][x] = str(i) + return board + +def rate_limit(fps): + delay_ms = 1. / fps + + while True: + # time one iteration of the loop + start = time.time() + yield + end = time.time() + yield_time = end - start + + # delay to fill up the rest of the time + wait_time = delay_ms - yield_time + if wait_time < 0: + # TODO: log failure +# log.debug('not meeting rate (%.2f ms)' % (wait_time * 1000)) + continue + time.sleep(wait_time) + +def iter_board(board): + for y, row in enumerate(board): + for x, cell in enumerate(row): + yield x, y, cell |