2 # (c) Copyright 2013 TJ <hacker@iam.tj>
3 # Licensed on the terms of the GNU General Public License version 3 (see COPYING)
5 # Game engine: game state and configuration
7 import os.path, pygame, engine
10 """ Container for all game state and configuration items """
16 def res_path(self, key):
17 """ Create a complete resource path
18 key: the item in the resource dictionary to make path for
20 if key in self.resources:
21 self.resources[key] = os.path.join(self.resources['path'], self.resources[key])
26 def __init__(self, width=0, height=0, title="", resources={}, debug=False):
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
34 # define the colour styles we need
35 self.colours['h1'] = (255, 255, 255)
36 self.colours['body'] = (0, 255, 255)
38 # store the received values
39 self.config.update({'width':width, 'height':height})
40 self.config.update({'title':title})
41 self.resolution = width, height
44 # prefix the resource path to all resource filenames
45 self.resources = resources
46 if not self.resources['path']:
47 self.resources['path'] = ''
49 self.res_path('soundtrack')
51 self.res_path('background')
52 self.res_path('splash')
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)
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)
80 self.splash_screen.blit(self.fonts['h1'].render(self.resources['title'], True, self.colours['h1']), (x, y))
82 self.splash_screen.blit(self.fonts['body'].render(self.resources['copyright'], True, self.colours['body']), (x, y))
83 pygame.display.update()
86 """ Toggle the game's paused state """
87 self.paused = True if self.paused == False else False
89 if self.config['sound']:
90 self.soundtrack.pause()
91 self.sound_effects.pause()
93 if self.config['sound']:
94 self.soundtrack.unpause()
95 self.sound_effects.unpause()