Add Splash screen, begin music earlier, and control scroll speed with F1-F12
[base2-runner.git] / base2-runner.py
index 7937a84..e00642e 100755 (executable)
@@ -4,7 +4,7 @@
 #
 # 2D platform scrolling game that teaches binary logic
 
-import sys, os, time, random, pygame
+import sys, os, time, random, math, pygame
 import engine
 
 # Use conditionals in the code to print useful information to the console using the form:
@@ -22,9 +22,13 @@ class Base2Runner:
  screen = None # the pygame Screen for the game window
 
  def __init__(self, width, height, title, resources={}, debug=False):
+  os.environ['SDL_VIDEO_CENTERED'] = '1' 
   pygame.init()
   self.debug = debug
   self.myGame = engine.Game(width, height, title, resources, debug=self.debug)
+  if self.myGame.config['sound'] == 1:
+   self.myGame.soundtrack.playlist(self.myGame.resources['soundtrack'])
+
   self.background_colour = (20, 20, 64)
   if 'icon' in self.myGame.resources:
    if self.debug: engine.debug_pr("pygame.image.get_extended()=%r" % pygame.image.get_extended())
@@ -50,9 +54,6 @@ class Base2Runner:
   # starting position
   y = resolution[1] / 2
 
-  if self.myGame.config['sound'] == 1:
-   self.myGame.soundtrack.playlist(self.myGame.resources['soundtrack'])
-
   self.myGame.config['auto_scroll'] = False
   movement = 0 # represents number of pixels, and direction, of next scroll
   x_pos = 0 # coordinate of left side of viewport
@@ -105,6 +106,11 @@ class Base2Runner:
       self.myGame.soundtrack.pause()
      elif k == pygame.K_l:
       self.myGame.config['auto_scroll'] = True if self.myGame.config['auto_scroll'] == False else False
+     elif k >= pygame.K_F1 and k <= pygame.K_F12:
+      self.stepping = k - (pygame.K_F1 - 1)
+      if self.myGame.config['auto_scroll']:
+       movement = int(math.copysign(self.stepping, movement))
+      if self.debug: engine.debug_pr("Keypress=%d, movement=%d" % (k, movement))
 
     if event.type == pygame.USEREVENT:
      if self.debug: engine.debug_pr("pygame.USEREVENT received; calling playlist_next()")
@@ -138,6 +144,7 @@ class Base2Runner:
 
 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)
 
@@ -148,4 +155,5 @@ if __name__ == '__main__':
  resources['background'] = 'binary-1024x1024.jpg'
  resources['splash'] = 'base2runner.jpg'
  resources['soundtrack'] = 'Music'
+ resources['copyright'] = '© Copyright 2013 TJ <hacker@iam.tj>'
  main(900, 600, "Base² Runner", resources, debug=True)