From 94ce712d10876996b4fd8f0742ce7a4027957950 Mon Sep 17 00:00:00 2001 From: TJ Date: Sun, 1 Dec 2013 15:25:04 +0000 Subject: [PATCH] convert to 'new style' classes and refactor to a Java-esque object-oriented pattern for main() --- base2-runner.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/base2-runner.py b/base2-runner.py index b584e20..6f274ae 100755 --- a/base2-runner.py +++ b/base2-runner.py @@ -10,7 +10,7 @@ import engine, circuits # Use conditionals in the code to print useful information to the console using the form: # if self.debug: engine.debug_pr("some_var=%d, another_var=%s" % (some_var, another_var), somevalue, another_value) -class Base2Runner: +class Base2Runner(object): background_image = None stepping = 8 # pixels scrolled each flip delay = 0.01 # main loop sleep (100 milliseconds) @@ -36,6 +36,7 @@ class Base2Runner: pygame.display.set_icon(self.icon_image) def play(self): + error_code = 0 self.screen = pygame.display.set_mode(self.myGame.resolution, self.flags, self.depth) self.screen.fill(self.background_colour) pygame.display.set_caption(self.myGame.config['title']) @@ -140,15 +141,17 @@ class Base2Runner: self.myGame.soundtrack.fadeout(5000) time.sleep(5) pygame.display.quit() + return error_code -def main(width, height, title, resources={}, debug=False): - game = Base2Runner(width, height, title, resources, debug) - time.sleep(10) # keep splash screen up for a little longer - game.play() - exit(0) + def main(self, args, env) + """ entry point for starting the game """ + time.sleep(10) # keep splash screen up for a little longer + return self.play() + if __name__ == '__main__': + # define some basic resources resources = {} resources['path'] = 'resources' resources['icon'] = 'base2runner-icon.png' @@ -159,4 +162,8 @@ if __name__ == '__main__': resources['soundtrack.copyright'] = 'Rolemusic @ The Free Music Archive' resources['soundtrack.license'] = 'Creative Commons Attribution Non-Commercial Share-Alike' resources['soundtrack.url'] = 'http://freemusicarchive.org/music/Rolemusic/' - main(900, 600, "Base² Runner", resources, debug=True) + + # instantiate the program, which will also initialise the display and show a splash screen + app = Base2Runner(iwidth=900, height=600, title="Base² Runner", resources=resources, debug=True) + # start the program with any command-line arguments and environment and exit using any error code it returns + exit( app.main(sys.argv[1:], os.environ) ) -- 2.17.1