203be80a0a5999e156770223d929e05d5833b545
[base2-runner.git] / engine / Game.py
1 #!/usr/bin/python3
2 # (c) Copyright 2013 TJ <hacker@iam.tj>
3 # Licensed on the terms of the GNU General Public License version 3 (see COPYING)
4 #
5 # Game engine: game state and configuration
6
7 import os.path, pygame, engine
8
9 class Game:
10  """ Container for all game state and configuration items """
11  config = dict()
12  fonts = dict()
13  colours = dict()
14  resources = None
15
16  def res_path(self, key):
17   """ Create a complete resource path
18    key:        the item in the resource dictionary to make path for
19   """
20   if key in self.resources:
21    self.resources[key] = os.path.join(self.resources['path'], self.resources[key])
22    return True
23   else:
24    return False
25
26  def __init__(self, width=0, height=0, title="", resources={},  debug=False):
27   """ Constructor.
28    width:      desired width of the game window
29    height:     desired height of the game window
30    title:      Window caption (title-bar)
31    resources:  Dictionary containing resources
32    debug:      enable debug messages written to console
33   """
34   # define the colour styles we need
35   self.colours['h1']   = (255, 255, 255)
36   self.colours['body'] = (0, 255, 255)
37
38   # store the received values
39   self.config.update({'width':width, 'height':height})
40   self.config.update({'title':title})
41   self.resolution = width, height
42   self.debug = debug
43
44   # prefix the resource path to all resource filenames
45   self.resources = resources
46   if not self.resources['path']:
47    self.resources['path'] = ''
48
49   self.res_path('soundtrack')
50   self.res_path('icon')
51   self.res_path('background')
52   self.res_path('splash')
53
54   # default values
55   self.resources['title'] = title
56   self.config.update({'sound':1}) # default to playing sound
57   self.play = False               # Flag that controls exit from main loop
58   self.paused = False             # Flag controlling pause of game play
59   self.soundtrack = engine.Channel(channel_id=-1, debug=self.debug)
60   self.sound_effects = engine.Channel(channel_id=0, debug=self.debug)
61
62   # splash screen
63   self.splash()
64
65  def splash(self):
66   # draw the splash screen 
67   if self.resources['splash']:
68    # find out the image dimensions
69    self.splash_screen = pygame.display.set_mode((0,0), pygame.NOFRAME)
70    self.splash_image = pygame.image.load(self.resources['splash']).convert()
71    resolution = self.splash_image.get_size()
72    # draw a splash screen until the game is ready
73    if self.debug: engine.debug_pr("Splash screen=%s" % self.resources['splash'], resolution)
74    self.splash_screen = pygame.display.set_mode(resolution, pygame.NOFRAME)
75    self.splash_image = pygame.image.load(self.resources['splash']).convert()
76    self.splash_screen.blit(self.splash_image, (0,0))
77    self.fonts['h1'] = pygame.font.Font(None, 80)
78    self.fonts['body'] = pygame.font.Font(None, 30)
79    x, y = 40, 10
80    self.splash_screen.blit(self.fonts['h1'].render(self.resources['title'], True, self.colours['h1']), (x, y))
81    y += 80
82    self.splash_screen.blit(self.fonts['body'].render(self.resources['copyright'], True, self.colours['body']), (x, y))
83    pygame.display.update()
84
85   def pause(self):
86    """ Toggle the game's paused state """
87    self.paused = True if self.paused == False else False
88    if self.paused:
89     if self.config['sound']:
90      self.soundtrack.pause()
91      self.sound_effects.pause()
92    else:
93     if self.config['sound']:
94      self.soundtrack.unpause()
95      self.sound_effects.unpause()