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 """ Encapsulate the attributes required for printing styled text """
15 def __init__(self, font, height, colour, debug=False):
16 self.font = pygame.font.Font(font, height)
20 if self.debug: engine.debug_pr("Style %s, height: %d, colour:" % (font, height), self.colour)
23 """ Container for all game state and configuration items """
27 # text printing origin
31 def res_path(self, key):
32 """ Create a complete resource path
33 key: the item in the resource dictionary to make path for
35 if key in self.resources:
36 self.resources[key] = os.path.join(self.resources['path'], self.resources[key])
41 def __init__(self, width=0, height=0, title="", resources={}, debug=False):
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
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))
53 # store the received values
54 self.config.update({'width':width, 'height':height})
55 self.config.update({'title':title})
56 self.resolution = width, height
59 # prefix the resource path to all resource filenames
60 self.resources = resources
61 if not self.resources['path']:
62 self.resources['path'] = ''
64 self.res_path('soundtrack')
66 self.res_path('background')
67 self.res_path('splash')
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)
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:
84 # don't do a blit operation unless there is some text to render
86 self.splash_screen.blit(self.styles[style].font.render(text, True, self.styles[style].colour), (self.print_x, self.print_y))
88 self.print_y += self.styles[style].height # update the text origin
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')
113 pygame.display.update()
116 """ Toggle the game's paused state """
117 self.paused = True if self.paused == False else False
119 if self.config['sound']:
120 self.soundtrack.pause()
121 self.sound_effects.pause()
123 if self.config['sound']:
124 self.soundtrack.unpause()
125 self.sound_effects.unpause()