forked from MykleR/Pygame-DoodleJump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.py
71 lines (58 loc) · 2.56 KB
/
camera.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
"""
CopyLeft 2021 Michael Rouves
This file is part of Pygame-DoodleJump.
Pygame-DoodleJump is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Pygame-DoodleJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Pygame-DoodleJump. If not, see <https://www.gnu.org/licenses/>.
"""
from pygame import Rect
from pygame.sprite import Sprite
import settings as config
from singleton import Singleton
class Camera(Singleton):
"""
A class to represent the camera.
Manages level position scrolling.
Can be access via Singleton: Camera.instance.
(Check Singleton design pattern for more info)
"""
# constructor called on new instance: Camera()
def __init__(self, lerp=5, width=config.XWIN, height=config.YWIN):
self.lastheight = 0
self.state = Rect(0, 0, width, height)
self.lerp = lerp
self.center = height // 2
self.maxheight = self.center
def reset(self) -> None:
" Called only when game restarts (after player death)."
self.state.y = 0
self.maxheight = self.center
def apply_rect(self, rect: Rect) -> Rect:
""" Transforms given rect relative to camera position.
:param rect: the rect to transform
"""
return rect.move((0, -self.state.topleft[1]))
def apply(self, target: Sprite) -> Rect:
""" Returns new target render position based on current camera position.
:param target: a sprite that wants to get its render position.
"""
return self.apply_rect(target.rect)
def update(self, target: Rect) -> None:
""" Scrolls up to maxheight reached by player.
Should be called each frame.
:param target: the target position to follow.
"""
# updating maxheight
if target.y < self.maxheight:
self.lastheight = self.maxheight
self.maxheight = target.y
# calculate scrolling speed required
speed = ((self.state.y + self.center) - self.maxheight) / self.lerp
self.state.y -= speed