-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBackground.py
More file actions
67 lines (40 loc) · 1.26 KB
/
Background.py
File metadata and controls
67 lines (40 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
#
#
#
#
import pygame
import re
import os
import collections
class Background():
def __init__(self, filename):
self.image = self._load_image(filename)
self.x, self.y = 0, 0
self.width, self.height = self.image.get_size()
@property
def rect(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
def _load_image(self, filename, colorkey = None):
""" Load a specific image from a file """
try:
image = pygame.image.load(filename).convert_alpha()
except pygame.error as e:
print(f'Unable To Load Image: {filename}')
raise SystemExit(e)
return image
def blitme(self, screen):
screen.blit(self.image, self.rect)
class Backgrounds():
def __init__(self, directory):
pygame.init()
self.directory = directory
self.backgrounds = collections.defaultdict()
self._load_background_sprites()
def _load_background_sprites(self):
for filename in sorted(os.listdir(self.directory), key = lambda filename: int(re.search(r'\d+', filename).group())):
bg = Background(os.path.join(self.directory, filename))
level = int(re.search(r'\d+', filename).group()) - 1
self.backgrounds[level] = bg
if __name__ == "__main__":
background = Backgrounds(pygame.display.set_mode((480, 360)), 's', "BG")