Refactor experimental demo into final state for developing the game
[base2-runner.git] / engine / Game.py
diff --git a/engine/Game.py b/engine/Game.py
new file mode 100755 (executable)
index 0000000..38174de
--- /dev/null
@@ -0,0 +1,62 @@
+#!/usr/bin/python3
+# (c) Copyright 2013 TJ <hacker@iam.tj>
+# Licensed on the terms of the GNU General Public License version 3 (see COPYING)
+#
+# Game engine: game state and configuration
+
+import os.path, pygame, engine
+
+class Game:
+ """ Container for all game state and configuration items """
+ config = dict()
+ resources = None
+
+ def res_path(self, key):
+  """ Create a complete resource path
+   key:        the item in the resource dictionary to make path for
+  """
+  if key in self.resources:
+   self.resources[key] = os.path.join(self.resources['path'], self.resources[key])
+   return True
+
+  return False
+
+ def __init__(self, width=0, height=0, title="", resources={},  debug=False):
+  """ Constructor.
+   width:      desired width of the game window
+   height:     desired height of the game window
+   title:      Window caption (title-bar)
+   resources:  Dictionary containing resources
+   debug:      enable debug messages written to console
+  """
+  self.config.update({'width':width, 'height':height})
+  self.config.update({'title':title})
+  self.resolution = width, height
+  self.debug = debug
+
+  self.resources = resources
+  if not self.resources['path']:
+   self.resources['path'] = ''
+
+  self.res_path('soundtrack')
+  self.res_path('icon')
+  self.res_path('background')
+
+  # default values
+  self.config.update({'sound':1}) # default to playing sound
+  self.play = False               # Flag that controls exit from main loop
+  self.paused = False             # Flag controlling pause of game play
+  self.soundtrack = engine.Channel(channel_id=-1, debug=self.debug)
+  self.sound_effects = engine.Channel(channel_id=0, debug=self.debug)
+
+  def pause(self):
+   """ Toggle the game's paused state """
+   self.paused = True if self.paused == False else False
+   if self.paused:
+    if self.config['sound']:
+     self.soundtrack.pause()
+     self.sound_effects.pause()
+   else:
+    if self.config['sound']:
+     self.soundtrack.unpause()
+     self.sound_effects.unpause()