-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGround.py
75 lines (60 loc) · 2.59 KB
/
Ground.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
from Global import find_abs_path
from typing import *
import pygame
import Time
class Ground(pygame.sprite.Sprite):
def __init__(self, resolution: Tuple[int, int]):
super().__init__()
self.tile_image = pygame.image.load(find_abs_path("./Images/Sprites/ground.png")).convert_alpha()
self.tile_width, self.tile_height = self.tile_image.get_size()
self.tile_count = 1
while self.tile_width * self.tile_count < resolution[0] + self.tile_width:
self.tile_count += 1
self.tile_count += 1
self.width = self.tile_width * self.tile_count
self.height = self.tile_height
self.image = pygame.Surface((self.width, self.height))
for x in range(self.tile_count):
self.image.blit(self.tile_image, (self.tile_width * x, 0))
self.x = 0
self.y = resolution[1] - self.height
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
def get_tile_size(self) -> Tuple[int, int]:
return self.tile_width, self.tile_height
def get_pos(self) -> Tuple[int, int]:
return self.x, self.y
def get_rect(self) -> pygame.Rect:
return self.rect
def update(self, movement: float) -> None:
self.x += movement
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
def set_x(self, x: float) -> None:
self.x = x
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
class GroundGroup(pygame.sprite.Group):
def __init__(self, resolution: Tuple[int, int]):
super().__init__()
self.resolution = resolution
self.delta_x = -100 # Production movement: -100, Debug movement: -40
self.ground_object = Ground(self.resolution)
self.add(self.ground_object)
self.frame_timer = Time.Time()
self.frame_timer.reset_timer()
def get_size(self) -> Tuple[int, int]:
return self.ground_object.get_tile_size()
def get_pos(self) -> Tuple[int, int]:
return self.ground_object.get_pos()
def move(self) -> float:
movement = (self.delta_x * self.frame_timer.get_time())
self.update(movement)
self.frame_timer.reset_timer()
return movement
def reset_pos(self) -> None:
# if x < 0: x = -(|x| % width)
if self.ground_object.get_pos()[0] < 0:
self.ground_object.set_x(-(abs(self.ground_object.get_pos()[0]) % self.ground_object.get_tile_size()[0]))
def pause(self) -> None:
self.frame_timer.pause()
def unpause(self) -> None:
if self.frame_timer.is_paused():
self.frame_timer.unpause()