Skip to content

Commit

Permalink
30 fps and try finally to ensure closing on loss or win
Browse files Browse the repository at this point in the history
  • Loading branch information
shoeffner committed Feb 18, 2017
1 parent 1698710 commit a960148
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
24 changes: 13 additions & 11 deletions human_snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ def keypress(self, event):
ret = self.snake.step(self.actions[key])
if ret != 0:
print('You {}!'.format('win' if ret > 0 else 'lose'))
if '--store' in sys.argv:
# writes the score into the output file
try:
fname = 'participant{}.csv'.format(
sys.argv[sys.argv.index('--store') + 1])
except IndexError:
fname = 'participant_unspecified.csv'
with open(fname, 'a') as f:
print(self.snake.highscore, self.snake.steps, sep=',',
file=f)
sys.exit()
try:
if '--store' in sys.argv:
# writes the score into the output file
try:
fname = 'participant{}.csv'.format(
sys.argv[sys.argv.index('--store') + 1])
except IndexError:
fname = 'participant_unspecified.csv'
with open(fname, 'a') as f:
print(self.snake.highscore, self.snake.steps,
sep=',', file=f)
finally:
sys.exit()


if __name__ == '__main__':
Expand Down
5 changes: 4 additions & 1 deletion q_snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ def step():
ret = game.step(player.get_action(game.board))
if ret:
print('You {}'.format('win' if ret > 0 else 'lose'))
return 0
try:
return 0
finally:
sys.exit()

ui = SnakeUI(game)
timer = ui.fig.canvas.new_timer(500, [(step, [], {})])
Expand Down
1 change: 1 addition & 0 deletions snake_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, snake):

self.writer = Writer()
self.writer.setup(self.fig, name, 72)
self.writer.fps = 30

atexit.register(self.finish)

Expand Down
4 changes: 3 additions & 1 deletion systematic_snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ def __call__(self):
with open('systematic.csv', 'a') as f:
print(self.snake.highscore, self.snake.steps, sep=',',
file=f)
try:
return 0
finally:
sys.exit()
return 0

def calculate_step(self):
"""Calculates the next step.
Expand Down

0 comments on commit a960148

Please sign in to comment.