Add Splash screen, begin music earlier, and control scroll speed with F1-F12
[base2-runner.git] / engine / Game.py
index 38174de..203be80 100755 (executable)
@@ -9,6 +9,8 @@ import os.path, pygame, engine
 class Game:
  """ Container for all game state and configuration items """
  config = dict()
+ fonts = dict()
+ colours = dict()
  resources = None
 
  def res_path(self, key):
@@ -18,8 +20,8 @@ class Game:
   if key in self.resources:
    self.resources[key] = os.path.join(self.resources['path'], self.resources[key])
    return True
-
-  return False
+  else:
+   return False
 
  def __init__(self, width=0, height=0, title="", resources={},  debug=False):
   """ Constructor.
@@ -29,11 +31,17 @@ class Game:
    resources:  Dictionary containing resources
    debug:      enable debug messages written to console
   """
+  # define the colour styles we need
+  self.colours['h1']   = (255, 255, 255)
+  self.colours['body'] = (0, 255, 255)
+
+  # store the received values
   self.config.update({'width':width, 'height':height})
   self.config.update({'title':title})
   self.resolution = width, height
   self.debug = debug
 
+  # prefix the resource path to all resource filenames
   self.resources = resources
   if not self.resources['path']:
    self.resources['path'] = ''
@@ -41,14 +49,39 @@ class Game:
   self.res_path('soundtrack')
   self.res_path('icon')
   self.res_path('background')
+  self.res_path('splash')
 
   # default values
+  self.resources['title'] = title
   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)
 
+  # splash screen
+  self.splash()
+
+ def splash(self):
+  # draw the splash screen 
+  if self.resources['splash']:
+   # find out the image dimensions
+   self.splash_screen = pygame.display.set_mode((0,0), pygame.NOFRAME)
+   self.splash_image = pygame.image.load(self.resources['splash']).convert()
+   resolution = self.splash_image.get_size()
+   # draw a splash screen until the game is ready
+   if self.debug: engine.debug_pr("Splash screen=%s" % self.resources['splash'], resolution)
+   self.splash_screen = pygame.display.set_mode(resolution, pygame.NOFRAME)
+   self.splash_image = pygame.image.load(self.resources['splash']).convert()
+   self.splash_screen.blit(self.splash_image, (0,0))
+   self.fonts['h1'] = pygame.font.Font(None, 80)
+   self.fonts['body'] = pygame.font.Font(None, 30)
+   x, y = 40, 10
+   self.splash_screen.blit(self.fonts['h1'].render(self.resources['title'], True, self.colours['h1']), (x, y))
+   y += 80
+   self.splash_screen.blit(self.fonts['body'].render(self.resources['copyright'], True, self.colours['body']), (x, y))
+   pygame.display.update()
+
   def pause(self):
    """ Toggle the game's paused state """
    self.paused = True if self.paused == False else False