sep-pm-platformer/project/sprite/DynamicSprite.py

59 lines
2.0 KiB
Python

import pygame
import sys
from sprite.Sprite import Sprite
from sprite.Spritesheet import Spritesheet
from sprite.StaticSprite import StaticSprite
sys.path.append('./sprite')
class DynamicSprite(StaticSprite):
def __init__(self, spritesheet: Spritesheet):
super().__init__(spritesheet)
self.motion = (0, 0)
self.deceleration_horizontal_air = 0.02
self.deceleration_horizontal_ground = 0.3
self.gravity = 9.81 / 10
# up, right, down, left
self.touches_bounding = (False, False, False, False)
def set_touches_bottom(self, value: bool):
self.touches_bounding = (self.touches_bounding[0], self.touches_bounding[1], value, self.touches_bounding[3])
def set_touches_right(self, value: bool):
self.touches_bounding = (self.touches_bounding[0], value, self.touches_bounding[2], self.touches_bounding[3])
def set_touches_left(self, value: bool):
self.touches_bounding = (self.touches_bounding[0], self.touches_bounding[1], self.touches_bounding[2], value)
def set_touches_top(self, value: bool):
self.touches_bounding = (value, self.touches_bounding[1], self.touches_bounding[2], self.touches_bounding[3])
def reset_touches(self):
self.touches_bounding = (False, False, False, False)
def tick(self, dt: float):
super().tick(dt)
# self.motion = (
# self.motion[0] - self.deceleration_horizontal * dt,
# self.motion[1] + self.gravity * dt
# )
# wrong implementation. Make deceleration_horizontal only until 0 is reached. multiply value with motion
deceleration_horizontal = 0
if abs(self.motion[0]) > 0:
if self.touches_bounding[2]:
deceleration_horizontal = self.deceleration_horizontal_ground
else:
deceleration_horizontal = self.deceleration_horizontal_air
self.motion = (
self.motion[0] - deceleration_horizontal * self.motion[0] * dt,
self.motion[1] + self.gravity * dt
)