import pygame, display, math, random class Animate: """ Base class for Animate objects """ def __init__(self, location, imagefile,name): self.image = pygame.image.load(imagefile).convert() self.location = location self.name = name self.health = 100 self.alive = True self.awake = False def draw(self,screen,screen_rect): """ Draw the item to the screen. screen_rect is the current viewing window in abs coordinates """ if self.alive and display.is_on_screen(self.location,screen_rect): screen.blit(self.image, display.abs_to_screen(self.location,screen_rect)) return def colliderect(self,rect): """ Returns if the inanimate oject is colliding with something(using rect collision) """ colliding = self.alive and self.location.colliderect(other) return colliding def nearby(self, rect, threshold = 100): distance = math.sqrt(math.pow(self.location.centerx - rect.centerx, 2) + math.pow(self.location.centery - rect.centery, 2)) near = distance < threshold if near: self.awake = True return near def update(self, level, knight): if self.nearby( knight.location, 50 ): if random.random() > 0.5: knight.health -= 2 if knight.attacking: self.health -= random.randrange(2,10) print self.name + " health is " + str(self.health) print "knight health is " + str(knight.health) if self.nearby( knight.location ) or self.awake: self.location.move_ip((random.randrange(-2,3),random.randrange(-2,3))) for inanimate in level.inanimates: if inanimate.colliderect(self.location): if abs(self.location.bottom - inanimate.location.top) <= 16: inanimate.touched_from_top(self) if inanimate.is_solid(): self.location.bottom = inanimate.location.top #touchedting the bottom of the inanimate going up elif abs(self.location.top - inanimate.location.bottom) <= 16: inanimate.touched_from_bottom(self) if inanimate.is_solid(): self.location.top = inanimate.location.bottom #touchedting the sides elif abs(self.location.right - inanimate.location.left) <= 16: inanimate.touched_from_left(self) if inanimate.is_solid(): self.location.right = inanimate.location.left else: inanimate.touched_from_right(self) if inanimate.is_solid(): self.location.left = inanimate.location.right def touched_from_top(self,knight): """ Called when the knight collides with the inanimate object from above """ return def touched_from_bottom(self,knight): """ Called when the knight collides with the inanimate object from below """ return def touched_from_left(self,knight): """ Called when the knight collides with the inanimate object from the left """ return def touched_from_right(self,knight): """ Called when the knight collides with the inanimate object from the right """ return def is_solid(self): """ Returns if the inanimate object is solid(meaning animate objects cannot walk through it) """ return False