git ignore vim editor temporary files
[base2-runner.git] / base2-runner.py
1 #!/usr/bin/python3
2 # (c) Copyright 2013 TJ <hacker@iam.tj>
3 # Licensed on the terms of the GNU General Public License version 3 (see COPYING)
4 #
5 # 2D platform scrolling game that teaches binary logic
6
7 import sys, os, time, random, pygame
8 import engine
9
10 # Use conditionals in the code to print useful information to the console using the form:
11 # if self.debug: engine.debug_pr("some_var=%d, another_var=%s" % (some_var, another_var), somevalue, another_value)
12
13 class Base2Runner:
14  background_image = None
15  stepping = 8 # pixels scrolled each flip
16  delay = 0.01  # main loop sleep (100 milliseconds)
17  background_colour = (0, 0, 0)
18  background_image = None
19  icon_image = None
20  flags = pygame.OPENGL & pygame.DOUBLEBUF
21  depth = 0
22  screen = None # the pygame Screen for the game window
23
24  def __init__(self, width, height, title, resources={}, debug=False):
25   pygame.init()
26   self.debug = debug
27   self.myGame = engine.Game(width, height, title, resources, debug=self.debug)
28   self.background_colour = (20, 20, 64)
29   if 'icon' in self.myGame.resources:
30    if self.debug: engine.debug_pr("pygame.image.get_extended()=%r" % pygame.image.get_extended())
31    self.icon_image = pygame.image.load(self.myGame.resources['icon'])
32    pygame.display.set_icon(self.icon_image)
33
34  def play(self):
35   self.screen = pygame.display.set_mode(self.myGame.resolution, self.flags, self.depth)
36   self.screen.fill(self.background_colour)
37   pygame.display.set_caption(self.myGame.config['title'])
38   resolution = self.screen.get_size()
39
40   # background image
41   self.background_image = engine.SeamlessBackground(self.myGame.resources['background'], debug=self.debug)
42   self.background_image.draw(self.screen, -1, 0) # -1 flags initial background drawing
43   pygame.display.update()
44
45   # scrolling object
46   blip = pygame.Surface((abs(self.stepping), abs(self.stepping)), pygame.HWSURFACE)
47   blip_colour = (128, 128, 128)
48   blip.fill(blip_colour)
49
50   # starting position
51   y = resolution[1] / 2
52
53   if self.myGame.config['sound'] == 1:
54    self.myGame.soundtrack.playlist(self.myGame.resources['soundtrack'])
55
56   self.myGame.config['auto_scroll'] = False
57   movement = 0 # represents number of pixels, and direction, of next scroll
58   x_pos = 0 # coordinate of left side of viewport
59
60   self.myGame.play = True # start the game
61   while (self.myGame.play):
62
63    if not self.myGame.paused:
64     # update game state
65     if x_pos + movement >= 0:
66      x_pos += movement
67     else:
68      movement = 0
69
70     # change colour randomly
71     d_red = random.randrange(-1,2) *8
72     d_green = random.randrange(-1,2) *8
73     d_blue = random.randrange(-1,2) *8
74     # build a modified colour tuple, ensuring all values are valid
75     blip_colour = tuple( b+d if b+d < 256 and b+d >=0 else b for b, d in zip(blip_colour, (d_red, d_green, d_blue)) )
76     blip.fill(blip_colour)
77
78     # change blip position up or down randomly by one step
79     dy = random.randrange(-1,2) * abs(self.stepping)
80     if((y + dy) < resolution[1] and (y + dy) >= 0):
81      # TODO: no longer need to redraw background colour if using a background image
82      # screen.fill(background_colour, pygame.Rect(resolution[0] - abs(stepping), y, abs(stepping), abs(stepping) ))
83      y += dy
84
85     # redraw the display
86     self.screen.scroll(-movement, 0)
87     self.background_image.draw(self.screen, x_pos, movement)
88     self.screen.blit(blip, (resolution[0] - abs(self.stepping), y))
89     pygame.display.update()
90    
91    # manage the frame-rate
92    time.sleep(self.delay)
93
94    # process events
95    for event in pygame.event.get():
96     if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_q):
97      self.myGame.play = False
98     if event.type ==  pygame.ACTIVEEVENT:
99      if self.debug: engine.debug_pr("active=%d, pygame.ACTIVEEVENT:" % pygame.display.get_active(), event)
100     if event.type == pygame.KEYUP:
101      k = event.key
102      if k == pygame.K_p:
103       time.sleep(10)
104      elif k == pygame.K_7:
105       self.myGame.soundtrack.pause()
106      elif k == pygame.K_l:
107       self.myGame.config['auto_scroll'] = True if self.myGame.config['auto_scroll'] == False else False
108
109     if event.type == pygame.USEREVENT:
110      if self.debug: engine.debug_pr("pygame.USEREVENT received; calling playlist_next()")
111      self.myGame.soundtrack.playlist_next()
112
113    if not self.myGame.config['auto_scroll']:
114     # reset if not auto-scrolling
115     movement = 0
116
117    # detect which keys are held down
118    keys_pressed = pygame.key.get_pressed()
119    if sum(keys_pressed):
120     if keys_pressed[pygame.K_6]:
121      # volume down
122      self.myGame.soundtrack.volume_down()
123     elif keys_pressed[pygame.K_8]:
124      # volume up
125      self.myGame.soundtrack.volume_up()
126     elif keys_pressed[pygame.K_d]:
127      # move right
128      movement = self.stepping
129     elif keys_pressed[pygame.K_a]:
130      # move left
131      movement = -self.stepping
132
133   # end of game
134   self.myGame.soundtrack.fadeout(5000)
135   time.sleep(5)
136   pygame.display.quit()
137
138
139 def main(width, height, title, resources={}, debug=False):
140  game = Base2Runner(width, height, title, resources, debug)
141  game.play()
142  exit(0)
143
144 if __name__ == '__main__':
145  resources = {}
146  resources['path'] = 'resources'
147  resources['icon'] = 'base2runner-icon.png'
148  resources['background'] = 'binary-1024x1024.jpg'
149  resources['splash'] = 'base2runner.jpg'
150  resources['soundtrack'] = 'Music'
151  main(900, 600, "BaseĀ² Runner", resources, debug=True)