class Entity: def __init__(self): self.location = Vector(0, 0) self.size = Vector(0, 0) self.boundingRectangle = pygame.Rect(0, 0, 0, 0) self.disposable = False self.disposed = False def update(self, time, timePassed): pass def render(self, screen, offset, time): screen.blit(self.image, (int(offset[0] + self.location.x), int(offset[1] + self.location.y))) def getLocation(self): return self.location def setLocation(self, newLocation): self.location = newLocation self.updateBoundingRectangle() def getCenterLocation(self): return self.location.add(self.size.multiplyScalar(0.5)) def centerOn(self, location): self.move(location.subtract(self.size.multiplyScalar(0.5))) def move(self, movementVector): self.setLocation(self.location.add(movementVector)) def setSize(self, newSize): self.size = newSize self.updateBoundingRectangle() def updateBoundingRectangle(self): self.boundingRectangle = pygame.Rect(self.location.x, self.location.y, self.size.x, self.size.y) def markDisposable(self): self.disposable = True def dispose(self): self.disposed = True def getDirectionFromVector(self, vector): if vector.y < 0: return Direction.NORTH elif vector.y > 0: return Direction.SOUTH elif vector.x < 0: return Direction.WEST elif vector.x > 0: return Direction.EAST
def renderPlayerHitpoints(self, targetSurface): image = images.get('ui_heart') startLocation = Vector(220, 240 - 11) for _ in range(gamecontroller.getPlayerTank().getHitpoints()): targetSurface.blit(image, startLocation.toIntTuple()) startLocation = startLocation.add(Vector(8, 0))
def renderWeaponPower(self, targetSurface): image = images.get('ui_weaponpower') startLocation = Vector(175, 240 - 11) for _ in range(gamecontroller.getPlayerTank().getWeapon().getLevel()): targetSurface.blit(image, startLocation.toIntTuple()) startLocation = startLocation.add(Vector(7, 0))
def renderLives(self, targetSurface): startLocation = Vector(0, 240 - 12) tankImage = images.get('tank1') for _ in range(gamecontroller.getLives()): targetSurface.blit(tankImage, startLocation.toIntTuple()) startLocation = startLocation.add(Vector(16, 0))
def render(self, screen, offset, time): extraOffset = Vector(0, 0) if not self.lastHitTime == None and time - self.lastHitTime > 50: extraOffset = self.lastHitVector.multiplyScalar(-1).toUnit() drawOffset = Vector(offset[0], offset[1]) self.graphics.render(screen, drawOffset.add(self.location).add(extraOffset), self.direction) self.controller.render(screen)
def checkHorizontalCollisions(self, y): start = Vector(self.entity.boundingRectangle.left, y) end = start.add(Vector(self.entity.size.x - 1, 0)) increment = Vector(playfield.blockSize, 0) return self.checkLineCollisions(start, end, increment)
def checkVerticalCollisions(self, x): start = Vector(x, self.entity.boundingRectangle.top) end = start.add(Vector(0, self.entity.size.y - 1)) increment = Vector(0, playfield.blockSize) return self.checkLineCollisions(start, end, increment)