def test_addition(): v = Vector([1,2,3,4], {1:1, 2:2, 3:3, 4:4}) w = Vector([1,2,3,4,5], {1:1, 2:2, 3:3, 4:4, 5:5}) y = v.vector_add(w) assert y.getitem(1) == 2 #test the new sum assert v.getitem(1) == 1 #preserve original vector assert 5 in y.domain #test the domains are summed
def getSafeMatrix(self, _loc, _level): fn = _level.isSolid trX = Vector(self.getHitbox()[2] // 2, 0) trY = Vector(0, self.getHitbox()[3] // 2) return [[ not fn(_loc.addVec(trY.mult(y)).addVec(trX.mult(x))) for x in range(3) ] for y in range(3)]
def nearestSafePlace(self, _loc, _level): _loc = _loc.floor() mat = self.getSafeMatrix(_loc, _level) # SOL (PLAT) OU OBSTACLE while not (mat[2][0] or mat[2][2]) or not mat[2][1]: _loc = _loc.addVec(Vector(0, -1)) mat = self.getSafeMatrix(_loc, _level) # PLAFOND (PLAT) OU OBSTACLE while not (mat[0][0] or mat[0][2]) or not mat[0][1]: _loc = _loc.addVec(Vector(0, 1)) mat = self.getSafeMatrix(_loc, _level) # MUR GAUCHE (PLAT) OU OBSTACLE while not (mat[0][0] or mat[2][0]) or not mat[1][0]: _loc = _loc.addVec(Vector(1, 0)) mat = self.getSafeMatrix(_loc, _level) # MUR DROIT (PLAT) OU OBSTACLE while not (mat[0][0] or mat[2][0]) or not mat[1][0]: _loc = _loc.addVec(Vector(-1, 0)) mat = self.getSafeMatrix(_loc, _level) # COIN SUP G while not (mat[0][0]): _loc = _loc.addVec(Vector(1, 1)) mat = self.getSafeMatrix(_loc, _level) # COIN SUP D while not (mat[0][2]): _loc = _loc.addVec(Vector(-1, 1)) mat = self.getSafeMatrix(_loc, _level) # COIN INF G while not (mat[2][0]): _loc = _loc.addVec(Vector(1, -1)) mat = self.getSafeMatrix(_loc, _level) # COIN INF D while not (mat[2][2]): _loc = _loc.addVec(Vector(-1, -1)) mat = self.getSafeMatrix(_loc, _level) return _loc
def addOffset(self, _loc): return _loc.addVec(Vector(self.offX, self.offY))
def start(self, _lv): self.setCurrentLevel(Level(_lv)) loc = self.getCurrentLevel().getStarting() loc = loc.addVec( Vector((50 - const.CHAR_WIDTH) / 2, -const.CHAR_HEIGHT)) self.getPlayer().setLocation(loc)
from classes.vector import Vector simple_vec = Vector(1, 2, 3) print(simple_vec) print(simple_vec.norm())
def hasBlockRight(self, _level): tr = Vector(1, 0) mat = self.getSafeMatrix(self.getLocation().addVec(tr), _level) return not (mat[0][2] and mat[1][2] and mat[2][2])
def hasBlockUp(self, _level): tr = Vector(0, -1) mat = self.getSafeMatrix(self.getLocation().addVec(tr), _level) return not (mat[0][0] and mat[0][1] and mat[0][2])
def test_dot_product(): v = Vector([1,2,3,4], {1:1, 2:2, 3:3, 4:4}) w = Vector([1,2,3,4], {1:1, 2:2, 3:3, 4:4}) y = v.dot_product(w) assert y == 30
def test_scalar_mult(): v = Vector([1,2,3,4], {1:1, 2:2, 3:3, 4:4}) y = v.scalar_mult(2) assert y.getitem(1) == 2 assert y.getitem(4) == 8
class Player(Collidable): velocity = Vector(0, 0) speed = const.CHAR_BASE_SPEED health = 100 maxHealth = 100 facing = 1 walkSeq = 0 def __init__(self, _gm): super().__init__(_gm) self.hitbox = (const.CHAR_WIDTH, const.CHAR_HEIGHT) def getHealth(self): return self.health def setHealth(self, _amount): if _amount < 0: _amount = 0 if _amount > self.maxHealth: _amount = self.maxHealth self.health = _amount def getSpeed(self): return self.speed def setSpeed(self, _speed): self.speed = _speed def getVelocity(self): return self.velocity def setVelocity(self, _vel): self.velocity = _vel def getWalkSeq(self): return self.walkSeq def setWalkSeq(self, _s): self.walkSeq = _s def getFacing(self): return self.facing def setFacing(self, _f): self.facing = _f def getTextureLocation(self): offX = (const.CHAR_T_WIDTH - const.CHAR_WIDTH) / 2 offY = (const.CHAR_T_HEIGHT - const.CHAR_HEIGHT) / 2 + 5 return self.getLocation().addVec(-Vector(int(offX), int(offY))) def moveUpdate(self, _keys): level = self.GM.getCurrentLevel() # CALCUL DE LA VITESSE vel = self.getVelocity() if _keys[pygame.K_LEFT]: self.setFacing(-1) if self.hasBlockLeft(level): vel.setX(0) self.walkSeq = 0 else: vel.setX(-self.getSpeed()) elif _keys[pygame.K_RIGHT]: self.setFacing(1) if self.hasBlockRight(level): vel.setX(0) self.walkSeq = 0 else: vel.setX(self.getSpeed()) else: vel.setX(0) self.walkSeq = 0 if self.hasBlockUp(level): if vel.getY() < 0: vel.setY(0) if self.hasBlockDown(level): if _keys[pygame.K_UP]: self.walkSeq = 0 vel.setY(-const.CHAR_JUMP_SPEED) if vel.getY() > 0: vel.setY(0) else: vel.setY(vel.getY() + const.GRAVITY) if abs(vel.getY()) > const.MAX_FALL_SPEED: v = vel.getY() / abs(vel.getY()) vel.setY(10 * v) # CALCUL DES POSITIONS loc = self.getLocation() if not self.isSafePlace(loc.addVec(vel), level): loc = self.nearestSafePlace(loc.addVec(vel), level) else: loc = loc.addVec(vel) self.setLocation(loc)
def getTextureLocation(self): offX = (const.CHAR_T_WIDTH - const.CHAR_WIDTH) / 2 offY = (const.CHAR_T_HEIGHT - const.CHAR_HEIGHT) / 2 + 5 return self.getLocation().addVec(-Vector(int(offX), int(offY)))