Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Aguiar <alanjas@hotmail.com>2014-01-08 00:46:29 (GMT)
committer Alan Aguiar <alanjas@hotmail.com>2014-01-08 00:46:29 (GMT)
commit59cbb9fb6f1a728c80c4343e90a2e598d29a90fc (patch)
treeeb7b36ad2ea18ffe6f9c8dd0c8b038c32b92c1c9
parent0b32e1ab84c9b874f39db8bc096b8d44f108652d (diff)
save stats in journal
-rwxr-xr-xSpaceWar.py43
-rw-r--r--activity.py12
2 files changed, 22 insertions, 33 deletions
diff --git a/SpaceWar.py b/SpaceWar.py
index 1ec8ec5..bb4eae8 100755
--- a/SpaceWar.py
+++ b/SpaceWar.py
@@ -43,40 +43,17 @@ class SpaceWar():
self.old_points = 0
self.running = True
self.screen = None
- self.load_stats()
- def load_stats(self):
- if self.parent is not None:
- try:
- folder = self.parent.get_activity_root()
- path = os.path.join(folder, 'data', 'stats.dat')
- if os.path.exists(path):
- f = open(path, 'r')
- val = f.readline()
- val = val.strip('\n')
- if not(val == ''):
- level = int(val)
- self.level = level
- val = f.readline()
- val = val.strip('\n')
- if not(val == ''):
- score = int(val)
- self.points = score
- f.close()
- except Exception, err:
- print 'Cannot load score', err
+ def get_stats(self):
+ msg = str(self.level) + '\n'
+ msg = msg + str(self.points)
+ return msg
- def save_stats(self):
- if self.parent is not None:
- try:
- folder = self.parent.get_activity_root()
- path = os.path.join(folder, 'data', 'stats.dat')
- f = open(path, 'w')
- f.write(str(self.level) + '\n')
- f.write(str(self.points) + '\n')
- f.close()
- except Exception, err:
- print 'Error saving score', err
+ def set_stats(self, stats):
+ l = stats.split('\n')
+ if len(l) == 2:
+ self.level = int(l[0])
+ self.points = int(l[1])
def load_all(self):
# Dun dun duuuuuuuun
@@ -138,6 +115,7 @@ class SpaceWar():
def run(self):
self.load_all()
+
while self.running:
# Timing phase
delta = self.clock.tick(30)
@@ -147,7 +125,6 @@ class SpaceWar():
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
self.running = False
- self.save_stats()
elif evt.type == pygame.KEYDOWN:
if evt.key == pygame.K_ESCAPE:
self.running = False
diff --git a/activity.py b/activity.py
index 5ee9a06..3b80e3b 100644
--- a/activity.py
+++ b/activity.py
@@ -66,3 +66,15 @@ class SpaceWarActivity(activity.Activity):
toolbox.show_all()
+ def read_file(self, file_path):
+ fd = open(file_path, 'r')
+ text = fd.read()
+ self.game.set_stats(text)
+ fd.close()
+
+ def write_file(self, file_path):
+ fd = open(file_path, 'w')
+ text = self.game.get_stats()
+ fd.write(text)
+ fd.close()
+