summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Ward <peteraward@gmail.com>2009-10-02 13:51:15 +1000
committerPeter Ward <peteraward@gmail.com>2009-10-02 13:51:15 +1000
commit4aef934741f595589ad2b2fdae65341348960ec3 (patch)
treec20380e1344488105e0f576a003cb431ad4f00f3
parent44a032e636d93e44a931a31e4e0f0ab7458fb465 (diff)
Added results.
-rwxr-xr-xpyglet_snake.py4
-rw-r--r--snake.py22
2 files changed, 23 insertions, 3 deletions
diff --git a/pyglet_snake.py b/pyglet_snake.py
index 4ad174e..10e4a3d 100755
--- a/pyglet_snake.py
+++ b/pyglet_snake.py
@@ -30,8 +30,8 @@ class PygletSnakeEngine(SnakeEngine, pyglet.window.Window):
EDGE_COLOR = (255, 255, 255, 255)
EDGE_WIDTH = 2
- def __init__(self, rows, columns, n_apples):
- super(PygletSnakeEngine, self).__init__(rows, columns, n_apples)
+ def __init__(self, rows, columns, n_apples, *args, **kwargs):
+ super(PygletSnakeEngine, self).__init__(rows, columns, n_apples, *args, **kwargs)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
diff --git a/snake.py b/snake.py
index 48048d8..1edcc49 100644
--- a/snake.py
+++ b/snake.py
@@ -2,6 +2,7 @@
from __future__ import division
+import time
import string
from random import randint
from collections import deque
@@ -11,13 +12,16 @@ import traceback
from common import *
class SnakeEngine(object):
- def __init__(self, rows, columns, n_apples):
+ def __init__(self, rows, columns, n_apples, results=False):
super(SnakeEngine, self).__init__()
self.letters = list(string.lowercase)
self.letters.reverse()
self.bots = {}
+ self.results = None
+ if results:
+ self.results = open('results.csv', 'a+')
self.new_game(rows, columns, n_apples)
@@ -34,6 +38,8 @@ class SnakeEngine(object):
return x, y
def new_game(self, rows, columns, n_apples):
+ self.start_time = time.time()
+
self.rows = rows
self.columns = columns
@@ -66,13 +72,27 @@ class SnakeEngine(object):
def remove_bot(self, letter):
letter = letter.lower()
+ time_score = time.time() - self.start_time
+
for row in self.board:
for x, cell in enumerate(row):
if cell.lower() == letter:
row[x] = Squares.EMPTY
+ bot = self.bots[letter]
del self.bots[letter]
+ if not self.results:
+ return
+
+ try:
+ name = bot[0].__name__
+ except AttributeError:
+ pass
+ else:
+ apple_score = len(bot[2])
+ self.results.write('%s,%s,%s\n' % (name, apple_score, time_score))
+
def update_snakes(self, directions_id=id(directions)):
assert id(directions) == directions_id, \
"The common.directions dictionary has been modified since startup..."