summaryrefslogtreecommitdiffstats
path: root/Geometry-Dash/platform.py
blob: 85db5905a77224ad588ac83321014237f75f29f6 (plain)
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
from setup import *
from colors import COLOR_BLACK
from player import Player


class Platform:
    width, height = 25, 25
    color = COLOR_BLACK

    def __init__(self, x, y):
        self.position = pygame.Vector2(x, y)

    def draw(self, screen, camera_x, camera_y):
        r = pygame.Rect(self.position.x + camera_x, self.position.y + camera_y, self.width, self.height)
        pygame.draw.rect(screen, self.color, r)


class PlatformManager:
    platform_spread = 50

    def __init__(self):
        self.platforms = []

        self.next_slot = 0

    def spawn_platform_at_slot(self, slot):
        platform_position_y = 700 - slot

        platform_position_x = self.platform_spread * slot

        new_platform = Platform(platform_position_x, platform_position_y)
        self.platforms.append(new_platform)

    def spawn_new_platforms(self, camera_x):
        while camera_x <= SCREEN_WIDTH - (self.platform_spread * self.next_slot):
            self.spawn_platform_at_slot(self.next_slot)
            self.next_slot += 1

    def delete_old_platforms(self, camera_x):
        if self.platforms:

            platform = self.platforms[0]

            offset = Platform.width + Player.width + camera_x

            if platform.position.x < -offset:
                self.platforms.remove(self.platforms[0])

    def update(self, camera_x):
        self.delete_old_platforms(camera_x)
        self.spawn_new_platforms(camera_x)

    def draw(self, screen, camera_x, camera_y):
        for platform in self.platforms:
            platform.draw(screen, camera_x, camera_y)