2 # (c) Copyright 2013 TJ <hacker@iam.tj>
3 # Licensed on the terms of the GNU General Public License version 3 (see COPYING)
5 # 2D platform scrolling game that teaches binary logic
7 import sys, os, time, random, pygame
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)
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
20 flags = pygame.OPENGL & pygame.DOUBLEBUF
22 screen = None # the pygame Screen for the game window
24 def __init__(self, width, height, title, resources={}, debug=False):
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)
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()
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()
46 blip = pygame.Surface((abs(self.stepping), abs(self.stepping)), pygame.HWSURFACE)
47 blip_colour = (128, 128, 128)
48 blip.fill(blip_colour)
53 if self.myGame.config['sound'] == 1:
54 self.myGame.soundtrack.playlist(self.myGame.resources['soundtrack'])
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
60 self.myGame.play = True # start the game
61 while (self.myGame.play):
63 if not self.myGame.paused:
65 if x_pos + movement >= 0:
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)
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) ))
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()
91 # manage the frame-rate
92 time.sleep(self.delay)
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:
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
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()
113 if not self.myGame.config['auto_scroll']:
114 # reset if not auto-scrolling
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]:
122 self.myGame.soundtrack.volume_down()
123 elif keys_pressed[pygame.K_8]:
125 self.myGame.soundtrack.volume_up()
126 elif keys_pressed[pygame.K_d]:
128 movement = self.stepping
129 elif keys_pressed[pygame.K_a]:
131 movement = -self.stepping
134 self.myGame.soundtrack.fadeout(5000)
136 pygame.display.quit()
139 def main(width, height, title, resources={}, debug=False):
140 game = Base2Runner(width, height, title, resources, debug)
144 if __name__ == '__main__':
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)