class Enemy(pygame.sprite.Sprite): def __init__(self, x, y, speed): pygame.sprite.Sprite.__init__(self) # (x, y) represents bottom center self.x = x self.y = y self.time = 0 self.speed = speed self.interval = z_walk.animSpeed / abs(self.speed) self.dead = False self.deadShot = False self.size = 25 self.animSprite = AnimatedSprite(ZOMBIE_SPRITE_SHEET, [z_walk, z_hit, z_die], 'walk') self.animSprite.loadImages() self.rect = pygame.rect.Rect( (self.x - self.size / 2, self.y - self.size, self.size, self.size)) self.image = pygame.Surface((self.size, self.size)) self.hp = 100 def updateHit(self): iw = self.image.get_width() ih = self.image.get_height() self.rect = pygame.rect.Rect((self.x - iw / 2, self.y - ih, iw, ih)) def update(self): self.updateHit() self.image = self.animSprite.getImage( True if self.speed < 0 else False) if not self.dead and self.time % self.interval == 0: self.x += self.speed * self.interval self.time += 1 * abs(self.speed) def hItYoUrSeLf(self): if not self.dead: self.animSprite.clearStack() self.animSprite.addAnimation('hit') self.hp -= 25 if self.hp <= 0: self.die() def die(self): self.animSprite.addAnimation('die') self.dead = True def draw(self, surf): surf.blit(self.image, (self.x - self.image.get_width() / 2, self.y - self.image.get_height())) def collidePoint(self, x, y): return self.rect.collidepoint(x, y)
keys = [False for i in range(1000)] z_walk = SpriteConfig( pygame.Rect(6, 90, K_WIDTH - 1, 68), # capture rect of all 12, # number 'walk', # name 40, # max width per tile 3, # border 0.0, # look buffer 8, # frames / animation_frame 0, # skipping buffer - how many pixels to skip after green line ) z_die = SpriteConfig(pygame.Rect(9, 395, 420, 70), 8, 'die', 48, 3, 0.0, 4, 0) z_hit = SpriteConfig(pygame.Rect(9, 395, 420, 70), 2, 'hit', 48, 3, 0.0, 10, 0) ex_zomb = AnimatedSprite(ZOMBIE_SPRITE_SHEET, [z_walk, z_hit, z_die], 'walk') ex_zomb.loadImages() kirb = AnimatedSprite( KIRBY_SPRITE_SHEET, [idle, walk, attack, flip, spin, land, ascend, spin_attack, shoot], "idle") SCREEN.blit(KIRBY_SPRITE_SHEET, (0, 0)) kirb.loadImages(SCREEN) enemies = [] bullets = [] def generateZombie(): x = 0 y = random.randint(0, HEIGHT - UI_SIZE * 2) + UI_SIZE dir = 1 + DIFFICULTY if bool(random.randint(0, 1)): x = WIDTH + 50