convert to 'new style' classes and refactor
authorTJ <git@iam.tj>
Sun, 1 Dec 2013 15:25:04 +0000 (15:25 +0000)
committerTJ <git@iam.tj>
Sun, 1 Dec 2013 15:25:04 +0000 (15:25 +0000)
  to a Java-esque object-oriented pattern for main()

base2-runner.py

index b584e20..6f274ae 100755 (executable)
@@ -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) )