forked from mattboan/Galtron
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathbackground.py
77 lines (62 loc) · 2.33 KB
/
background.py
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
68
69
70
71
72
73
74
75
76
77
import pygame as pg
class BackgroundManager():
def __init__(self, screen):
self.backgrounds = {}
self.screen = screen
self.screenRect = screen.get_rect()
self.active = ''
self.fillColor = None
def addBackground(self, name, imagePath, moveSpeedX, moveSpeedY):
if not name:
return
image = pg.image.load(imagePath)
if name not in self.backgrounds:
self.backgrounds[name] = []
bg = Background(self.screen, image, moveSpeedX, moveSpeedY)
self.backgrounds[name].append(bg)
def removeBackground(self, name):
if name in self.backgrounds:
del self.backgrounds[name]
self.active = ''
def getBackground(self, name):
if name in self.backgrounds:
return self.backgrounds[name]
def getSelectedBackground(self):
if self.active:
return self.backgrounds[self.active]
def setFillColor(self, fillColor):
self.fillColor = fillColor
def selectBackground(self, name):
if name in self.backgrounds:
self.active = name
def update(self):
if self.active:
for bg in self.backgrounds[self.active]:
bg.update()
def draw(self):
if self.active:
if self.fillColor:
self.screen.fill(self.fillColor)
for bg in self.backgrounds[self.active]:
bg.draw()
class Background():
def __init__(self, screen, image, moveSpeedX, moveSpeedY):
self.screen = screen
self.screenRect = screen.get_rect()
self.image = image
self.imageRect = image.get_rect()
self.x = 0
self.y = 0
self.moveSpeedX = moveSpeedX
self.moveSpeedY = moveSpeedY
def update(self):
self.x = (self.x + self.moveSpeedX) % self.imageRect.width
self.y = (self.y + self.moveSpeedY) % self.imageRect.height
def draw(self):
drawX = (-self.imageRect.width + self.x) if (0 < self.x) else self.x
drawY = (-self.imageRect.height + self.y) if (0 < self.y) else self.y
while (drawX < self.screenRect.width):
while (drawY < self.screenRect.height):
self.screen.blit(self.image, (drawX, drawY))
drawY += self.imageRect.height
drawX += self.imageRect.width