-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.py
More file actions
64 lines (53 loc) · 2.16 KB
/
Copy pathbackground.py
File metadata and controls
64 lines (53 loc) · 2.16 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
import pygame
from asset_paths import get_background_path
from image_loader import load_image
# Height of the ground line as a ratio of the scene height
# Used to compute self.ground_y, which is used by the other classes to determine
# where the ground is.
GROUND_LINE_RATIO = 0.90
class Background(pygame.sprite.Sprite):
def __init__(self, screen_width, scene_height, top):
super().__init__(self.containers)
self.screen_width = screen_width
self.scene_height = scene_height
self.top = top # The y coordinate where background starts (i.e. the height of the HUD)
self.x = 0.0
self.speed = 150
self.current_name = ""
self.did_wrap = True
self.did_mid_cycle = False
@property
def ground_y(self):
return self.top + round(self.scene_height * GROUND_LINE_RATIO)
def _load_scaled_background(self, background_name):
image = load_image(get_background_path(background_name))
half_width = image.get_width() // 2
tile = image.subsurface((0, 0, half_width, image.get_height())).copy()
return pygame.transform.smoothscale(tile, (self.screen_width, self.scene_height)).convert()
def set_background(self, background_name):
if background_name == self.current_name:
return
self.current_name = background_name
self.tile = self._load_scaled_background(background_name)
self.tile_width = self.tile.get_width()
self.x = 0.0
self.did_wrap = True
self.did_mid_cycle = False
def set_level(self, level):
self.set_background(f"lv{level}")
def set_speed(self, speed):
self.speed = speed
def update(self, dt):
previous_x = self.x
self.did_wrap = False
self.did_mid_cycle = False
self.x -= self.speed * dt
if previous_x > -self.tile_width / 2 >= self.x:
self.did_mid_cycle = True
if self.x <= -self.tile_width:
self.x += self.tile_width
self.did_wrap = True
def draw(self, screen):
x = round(self.x)
screen.blit(self.tile, (x, self.top))
screen.blit(self.tile, (x + self.tile_width, self.top))