d20ab21137807f94756e0c6d0df4eba35c3fe1b6
[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 Style:
10  """ Encapsulate the attributes required for printing styled text """
11  font = None
12  colour = None
13  height = 0
14  
15  def __init__(self, font, height, colour, debug=False):
16   self.font = pygame.font.Font(font, height)
17   self.height = height
18   self.colour = colour
19   self.debug = debug
20   if self.debug: engine.debug_pr("Style %s, height: %d, colour:" % (font, height), self.colour)
21
22 class Game:
23  """ Container for all game state and configuration items """
24  config = dict()
25  styles = dict()
26  resources = None
27  # text printing origin
28  print_x = 0
29  print_y = 0
30
31  def res_path(self, key):
32   """ Create a complete resource path
33    key:        the item in the resource dictionary to make path for
34   """
35   if key in self.resources:
36    self.resources[key] = os.path.join(self.resources['path'], self.resources[key])
37    return True
38   else:
39    return False
40
41  def __init__(self, width=0, height=0, title="", resources={},  debug=False):
42   """ Constructor.
43    width:      desired width of the game window
44    height:     desired height of the game window
45    title:      Window caption (title-bar)
46    resources:  Dictionary containing resources
47    debug:      enable debug messages written to console
48   """
49   # define the styles for printed text
50   self.styles['h1']   = engine.Style(None,  80, (255, 255, 255))
51   self.styles['body'] = engine.Style(None,  30, (255, 255, 0))
52
53   # store the received values
54   self.config.update({'width':width, 'height':height})
55   self.config.update({'title':title})
56   self.resolution = width, height
57   self.debug = debug
58
59   # prefix the resource path to all resource filenames
60   self.resources = resources
61   if not self.resources['path']:
62    self.resources['path'] = ''
63
64   self.res_path('soundtrack')
65   self.res_path('icon')
66   self.res_path('background')
67   self.res_path('splash')
68
69   # default values
70   self.resources['title'] = title
71   self.config.update({'sound':1}) # default to playing sound
72   self.play = False               # Flag that controls exit from main loop
73   self.paused = False             # Flag controlling pause of game play
74   self.soundtrack = engine.Channel(channel_id=-1, debug=self.debug)
75   self.sound_effects = engine.Channel(channel_id=0, debug=self.debug)
76
77   # splash screen
78   self.splash()
79
80  def print(self, text, style):
81   """ print a line of text and move the text origin to the next line """
82   if not style in self.styles:
83    style = 'body'
84   # don't do a blit operation unless there is some text to render
85   if len(text) > 0:
86    self.splash_screen.blit(self.styles[style].font.render(text, True, self.styles[style].colour), (self.print_x, self.print_y))
87
88   self.print_y += self.styles[style].height # update the text origin
89
90  def splash(self):
91   # draw the splash screen 
92   if self.resources['splash']:
93    # find out the image dimensions
94    self.splash_screen = pygame.display.set_mode((0,0), pygame.NOFRAME)
95    self.splash_image = pygame.image.load(self.resources['splash']).convert()
96    resolution = self.splash_image.get_size()
97    # draw a splash screen until the game is ready
98    if self.debug: engine.debug_pr("Splash screen=%s" % self.resources['splash'], resolution)
99    self.splash_screen = pygame.display.set_mode(resolution, pygame.NOFRAME)
100    self.splash_image = pygame.image.load(self.resources['splash']).convert()
101    self.splash_screen.blit(self.splash_image, (0,0))
102    # print the Game title and copyright
103    self.print_x, self.print_y = 40, 40 # indent
104    self.print(self.resources['title'], 'h1')
105    self.print(self.resources['copyright'], 'body')
106    # print the Soundtrack copyright information
107    self.print('Soundtrack:', 'body')
108    self.print_x += 20 # indent
109    for key in ('copyright', 'license', 'url'):
110     text = "%s: %s" % (key, self.resources["soundtrack.%s" % key])
111     self.print(text, 'body')
112
113    pygame.display.update()
114
115   def pause(self):
116    """ Toggle the game's paused state """
117    self.paused = True if self.paused == False else False
118    if self.paused:
119     if self.config['sound']:
120      self.soundtrack.pause()
121      self.sound_effects.pause()
122    else:
123     if self.config['sound']:
124      self.soundtrack.unpause()
125      self.sound_effects.unpause()