3 # An example of a vertical scrolling map with pygame
5 # Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # https://www.cs.ucsb.edu/~pconrad/cs5nm/topics/pygame/drawing/
25 IMAGE_PATH = 'road.png'
30 class VerticalScrollingMap(pygame.Surface):
32 There are some assumptions on the scrolling image for the seamless looping
34 - The image height must be at least twice the height of the
36 - The image must have two identical regions, one at the top and one at
37 the bottom, of the same height as the screen/viewport.
39 def __init__(self, image, *args, **kwargs):
40 super(VerticalScrollingMap, self).__init__(*args, **kwargs)
42 self.viewport_width, self.viewport_height = self.get_size()
44 self.texture = pygame.image.load(image)
45 self.texture_widht, self.texture_height = self.texture.get_size()
51 self.x = (self.viewport_width - self.texture_widht) / 2.0
53 def draw(self, screen):
56 # NOTE, we assume that the texture has at least twice the height of the
58 lastframe_limit = -(self.texture_height - self.viewport_height)
60 # Wrap around to cycle seamlessly
61 self.y %= lastframe_limit
63 screen.blit(self.texture, (self.x, self.y))
65 def set_offset(self, offset):
68 def inc_offset(self, amount=1):
71 def dec_offset(self, amount=1):
78 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT),
79 pygame.HWSURFACE | pygame.DOUBLEBUF)
80 pygame.display.set_caption('Vertical Scrolling Map')
81 pygame.mouse.set_visible(0)
83 screen_size = screen.get_size()
86 background = pygame.Surface(screen_size)
87 background.fill(bg_color)
89 screen.blit(background.convert(), (0, 0))
91 scrolling_map = VerticalScrollingMap(IMAGE_PATH, screen_size)
93 # If there is a joustick connected it will take precedence
94 # to set the scrolling offset over the key presses
96 joystick = pygame.joystick.Joystick(0)
102 clock = pygame.time.Clock()
107 msElapsed = clock.tick(fps)
109 for event in pygame.event.get():
110 if event.type == pygame.QUIT:
115 val = joystick.get_axis(axis)
116 scrolling_map.set_offset(int(val * 50))
118 key = pygame.key.get_pressed()
119 if key[pygame.K_ESCAPE]:
122 scrolling_map.inc_offset()
123 if key[pygame.K_DOWN]:
124 scrolling_map.dec_offset()
129 screen.blit(background.convert(), (0, 0))
130 scrolling_map.draw(screen)
132 pygame.display.update()
136 if __name__ == '__main__':