def __init__(self, type, speed = 9, pos =(400,300), fps=10): images = configs.images[type] AnimatedSprite.__init__(self, images, fps) self.fps = fps self.type = type self.speed = speed self.rect.topleft = pos self.dead = False
def init_players_sprite(self, players): for player in players: right_images_file = "player_{}_sprite_right.png".format(player.pid) right_images = self.load_sliced_sprites(20, 20, right_images_file) left_images_file = "player_{}_sprite_left.png".format(player.pid) left_images = self.load_sliced_sprites(20, 20, left_images_file) player_sprite = AnimatedSprite(left_images, right_images) player_sprite.is_direction_left = True self.players[player.name] = player_sprite
def __init__(self, type, pos, source, dir = 1, dy=10, dx=0, fps = 10): images = configs.images[type] AnimatedSprite.__init__(self, images, fps) self.dy = dy*dir self.dx = dx self.rect.topleft = pos self.type = type self.color = type[0:5] self.source = source
def genM(lane): global spriteM typeM = random.randint(0, 2) if typeM == 0: spriteM[lane] = AnimatedSprite(fireM, mStart[lane], 8, 1, 0, 8, 12) elif typeM == 1: spriteM[lane] = AnimatedSprite(iceM, mStart[lane], 8, 1, 0, 8, 12) elif typeM == 2: spriteM[lane] = AnimatedSprite(dustM, mStart[lane], 8, 1, 0, 8, 12) return typeM
def __init__(self, dpTop): Pony.__init__(self) if not Rarity.images: Rarity.images['idle'] = [load_image('rarityWalking-'+str(i)+'.png', (PONY_SIZE, PONY_SIZE)) for i in xrange(24)] Rarity.images['attacking'] = load_image('rarityFindingGems.png', (PONY_SIZE, PONY_SIZE)) self.images = Rarity.images['idle'] AnimatedSprite.__init__(self, Rarity.images['idle'], 10) self.dpTop = dpTop self.rect = self.image.get_rect() self.timeTilFire = 1000 self.attackTime = 500 self.attacking = False
def switch_pole(self): if self.type == "black": self.type = "white" self.color = "white" oldpos = self.rect.topleft AnimatedSprite.__init__(self, configs.images[self.type]) self.rect.topleft = oldpos else: self.type = "black" self.color = "black" oldpos = self.rect.topleft AnimatedSprite.__init__(self, configs.images[self.type]) self.rect.topleft = oldpos
def onHit(self,b): if b.color == self.color: self.health -= 0.5 else: self.health -= 1 if self.health <= 0: self.dead = True self.killed_by = b.source if not self.dead: oldpos = self.rect.topleft self.isHit = True AnimatedSprite.__init__(self, configs.images[self.type + "_hit"]) self.rect.topleft = oldpos
def update(self, t): if self.isHit: self.hurt_anim_Counter -= 1 if self.hurt_anim_Counter == 0: oldpos = self.rect.topleft self.isHit = False AnimatedSprite.__init__(self, configs.images[self.type]) self.rect.topleft = oldpos self.hurt_anim_Counter = 3 Fighter.update(self,t) dx, dy = self.move.update(self) self.rect.top += dy self.rect.left += dx
def __init__(self, dpTop): Pony.__init__(self) self.dpTop = dpTop if not Derpy.frames: Derpy.frames = [load_image('derpyFlying-'+str(i)+'.png', (PONY_SIZE, PONY_SIZE)).convert_alpha() for i in xrange(7)] AnimatedSprite.__init__(self, Derpy.frames) self.rect = self.image.get_rect() MovingSprite.__init__(self, None, 200) self.range = 500 self.attacking = False self.cooldown = 4000
class NpcRenderer: def __init__(self, surface, tile_width=32, tile_height=32): self.surface = surface # FIXME: Just test images for now self.MAP_TILE_WIDTH = tile_width self.MAP_TILE_HEIGHT = tile_height self.font = pygame.font.Font(None, 17) self.npc_dead_img = pygame.image.load("monk_dead.png") self.npc_alive_img = pygame.image.load("monk.png") self.npcimage = self.npc_alive_img self.work_animation_images = TileUtils.load_sliced_sprites(32, 32, 'monk_working.png') self.work_animation = AnimatedSprite(self.work_animation_images, 30) self.sleep_animation_images = TileUtils.load_sliced_sprites(32, 32, 'monk_sleeping.png') self.sleep_animation = AnimatedSprite(self.sleep_animation_images, 30) self.brewing_animation_images = TileUtils.load_sliced_sprites(32, 32, 'monk_brewing.png') self.brewing_animation = AnimatedSprite(self.brewing_animation_images, 8) self.hunting_animation_images = TileUtils.load_sliced_sprites(32, 32, 'monk_hunting.png') self.hunting_animation = AnimatedSprite(self.hunting_animation_images, 8) self.npc_animation = self.work_animation def draw_npc(self, npc): # default image npcimage = self.npc_alive_img action = npc.schedule.get_current_action() if npc.alive: if (type(action) == ProduceAction) and (type(npc.occupation) == Hunter): self.npc_animation = self.hunting_animation self.surface.blit(self.hunting_animation.image, (npc.x, npc.y)) elif (type(action) == ProduceAction) and (type(npc.occupation) == Brewer): self.npc_animation = self.brewing_animation self.surface.blit(self.brewing_animation.image, (npc.x, npc.y)) elif type(action) == ProduceAction: self.npc_animation = self.work_animation self.surface.blit(self.work_animation.image, (npc.x, npc.y)) elif type(action) == Action and action.name == "Sleep": self.npc_animation = self.sleep_animation self.surface.blit(self.sleep_animation.image, (npc.x,npc.y)) else: self.surface.blit(npcimage, (npc.x, npc.y)) else: # if npc is dead, show dead npc image no matter what self.npcimage = self.npc_dead_img self.surface.blit(npcimage, (npc.x, npc.y)) # draw npc name text = self.font.render(npc.name, True, (255,255, 255)) textRect = text.get_rect() textRect.left = npc.x - (self.MAP_TILE_WIDTH / 2) textRect.top = npc.y + (self.MAP_TILE_HEIGHT) self.surface.blit(text, textRect) def update(self, time): self.work_animation.update(time) self.brewing_animation.update(time) self.sleep_animation.update(time) self.hunting_animation.update(time)
def __init__(self, surface, tile_width=32, tile_height=32): self.surface = surface # FIXME: Just test images for now self.MAP_TILE_WIDTH = tile_width self.MAP_TILE_HEIGHT = tile_height self.font = pygame.font.Font(None, 17) self.npc_dead_img = pygame.image.load("monk_dead.png") self.npc_alive_img = pygame.image.load("monk.png") self.npcimage = self.npc_alive_img self.work_animation_images = TileUtils.load_sliced_sprites(32, 32, 'monk_working.png') self.work_animation = AnimatedSprite(self.work_animation_images, 30) self.sleep_animation_images = TileUtils.load_sliced_sprites(32, 32, 'monk_sleeping.png') self.sleep_animation = AnimatedSprite(self.sleep_animation_images, 30) self.brewing_animation_images = TileUtils.load_sliced_sprites(32, 32, 'monk_brewing.png') self.brewing_animation = AnimatedSprite(self.brewing_animation_images, 8) self.hunting_animation_images = TileUtils.load_sliced_sprites(32, 32, 'monk_hunting.png') self.hunting_animation = AnimatedSprite(self.hunting_animation_images, 8) self.npc_animation = self.work_animation
def update(self, dTime): Pony.update(self) if self.attacking: self.attackTime -= dTime if self.attackTime <= 0: # stand back up self.attackTime = 500 self.attacking = False else: AnimatedSprite.update(self, dTime) self.timeTilFire -= dTime if self.timeTilFire <= 0 and not self.ghost: #find a gem self.image = Rarity.images['attacking'] self.timeTilFire = 3000 self.attacking = True g = Gem(self.dpTop) g.place(self.rect.center) self.dpTop.sprites.add(g) self.dpTop.clickables.add(g)
def update(self, dTime): AnimatedSprite.update(self, dTime) if not self.ghost: MovingSprite.update(self, dTime) if not self.target or not self.target.alive(): print "finding new target" #find a suitable target ens = self.dpTop.enemiesWithinRange(self.rect.center, self.range) ens.sort(key=lambda x: -x.health) if ens: self.target = ens[0] else: self.target = None self.cooldown -= dTime #check whether we can drop an anvil if pygame.sprite.spritecollideany(self, self.dpTop.enemies) and self.cooldown <= 0: a = Anvil(self.dpTop) a.place(self.rect.center) self.dpTop.sprites.add(a) self.cooldown = 4000
class Explosion(object): EXPLOSION = AnimatedSprite(explode=[(64, 64), 16, 4, (0, 0), 'exp_sheet.png']) def __init__(self, scene, to_destroy, c_scale=-1): super(Explosion, self).__init__() self.interval = 33 self.phase = 0 import random from resources import Textures self.angle = random.randint(0, 359) self.scale = random.uniform(1, 5) if c_scale <= 0 else c_scale self.image = pygame.transform.rotozoom(Textures['exp_sheet'][self.phase], self.angle, self.scale) del random self.rect = self.image.get_rect() self.rect.center = pygame.mouse.get_pos() self.next_update = pygame.time.get_ticks() + self.interval to_destroy.explode(self.rect.center, self.rect.width // 5) scene.group_explosions.add(self) def update_phase(self): if pygame.time.get_ticks() >= self.next_update: from resources import Textures if self.phase >= len(Textures['exp_sheet']): return True center = self.rect.center self.image = pygame.transform.rotozoom(Textures['exp_sheet'][self.phase], self.angle, self.scale) self.rect = self.image.get_rect() self.rect.center = center self.phase += 1 self.next_update = pygame.time.get_ticks() + self.interval return False def update(self): if self.update_phase(): # gr.blit(Explosion.explosion_del, self.rect, None, pygame.BLEND_RGBA_MIN) self.kill()
def main(): pygame.init() surf_sz = (640, 480) main_surface = pygame.display.set_mode(surf_sz) global spriteMage, spriteM typeM = [0, 0, 0] for lane in range(0, 3): typeM[lane] = genM(lane) curr = 1 score = 0 black = (0, 0, 0) red = (255, 0, 0) misses = 0 my_clock = pygame.time.Clock() my_font = pygame.font.SysFont("Arial Black", 20) scoreN = my_font.render("Score:", False, black) miss = my_font.render("X", False, red) boxes = my_font.render("[ ] [ ] [ ]", False, black) game_font = pygame.font.SysFont("Macropsia", 40) game_over = game_font.render("Game Over", False, red) pause_game = game_font.render("Game Paused", False, red) title_font = pygame.font.SysFont("Lithograph", 50) title = title_font.render("Wymbert's Adventures", False, (128, 0, 255)) moveM = [.5, .5, .5] accept = True attack = [False, False, False] explode = [False, False, False] pause = False while True: ev = pygame.event.poll() if ev.type == pygame.QUIT: break if accept and ev.type == pygame.KEYDOWN: key = ev.dict["key"] if key == 27: break if key == ord("q"): spriteMage = AnimatedSprite(magePunch, spriteMage.position, 2, 1, 0, 2, 6) accept = False elif key == ord("w"): spriteMage = AnimatedSprite(mageMegaPunch, spriteMage.position, 3, 1, 0, 3, 6) accept = False elif key == ord("e"): spriteMage = AnimatedSprite(magePound, spriteMage.position, 4, 1, 0, 4, 6) accept = False elif key == ord("p"): pause = not pause elif key == 273: #up key if not curr == 0: curr -= 1 spriteMage.setPosition(spriteMage.position[0], mageY[curr]) elif key == 274: #down key if not curr == 2: curr += 1 spriteMage.setPosition(spriteMage.position[0], mageY[curr]) if pause: main_surface.blit(pause_game, (225, 85)) elif misses < 3: scoreT = my_font.render(str(score), False, black) if spriteMage.image == magePunch and spriteMage.updateCount >= spriteMage.updateDivisor * 2: spriteMage = AnimatedSprite(mageWait, (mageX, mageY[curr]), 2, 1, 0, 2, 15) accept = True elif spriteMage.image == mageMegaPunch and spriteMage.updateCount >= spriteMage.updateDivisor * 3: spriteMage = AnimatedSprite(mageWait, (mageX, mageY[curr]), 2, 1, 0, 2, 15) accept = True elif spriteMage.image == magePound and spriteMage.updateCount >= spriteMage.updateDivisor * 4: spriteMage = AnimatedSprite(mageWait, (mageX, mageY[curr]), 2, 1, 0, 2, 15) accept = True if spriteMage.image == magePunch and spriteMage.updateCount >= spriteMage.updateDivisor * 1: attack[curr] = True spriteAttack[curr] = AnimatedSprite( fire, (spriteMage.position[0] + spriteMage.frameWidth, spriteMage.position[1] + (.25 * spriteMage.frameHeight)), 5, 1, 0, 5, 6) elif spriteMage.image == mageMegaPunch and spriteMage.updateCount >= spriteMage.updateDivisor * 2: attack[curr] = True spriteAttack[curr] = AnimatedSprite( water, (spriteMage.position[0] + spriteMage.frameWidth, spriteMage.position[1] + (.25 * spriteMage.frameHeight)), 3, 1, 0, 3, 8) elif spriteMage.image == magePound and spriteMage.updateCount >= spriteMage.updateDivisor * 3: attack[curr] = True spriteAttack[curr] = AnimatedSprite( wind, (spriteMage.position[0] + spriteMage.frameWidth, spriteMage.position[1]), 2, 1, 0, 2, 12) main_surface.fill((255, 255, 255)) main_surface.blit(scoreN, (10, 10)) main_surface.blit(scoreT, (85, 10)) main_surface.blit(boxes, (surf_sz[0] - 120, 10)) main_surface.blit(title, (125, 50)) for lane in range(0, 3): if spriteM[lane].position[0] > 0 and not explode[lane]: spriteM[lane].update() spriteM[lane].setPosition( spriteM[lane].position[0] - moveM[lane], spriteM[lane].position[1]) spriteM[lane].draw(main_surface) else: if not explode[lane]: misses += 1 spriteM[lane].setPosition( surf_sz[0], spriteMage.position[1] + (.1 * spriteMage.frameHeight)) explode[lane] = False typeM[lane] = genM(lane) for lane in range(0, 3): if attack[lane]: if spriteM[lane].containsPoint( (spriteAttack[lane].position[0] + spriteAttack[lane].frameWidth, spriteAttack[lane].position[1] + spriteAttack[lane].frameHeight / 2)): attack[lane] = False if (typeM[lane] == 0 and spriteAttack[lane].image == water) or ( typeM[lane] == 1 and spriteAttack[lane].image == fire) or ( typeM[lane] == 2 and spriteAttack[lane].image == wind): explode[lane] = True score += 1 moveM[lane] += .25 else: spriteAttack[lane].update() spriteAttack[lane].setPosition( spriteAttack[lane].position[0] + 3, spriteAttack[lane].position[1]) spriteAttack[lane].draw(main_surface) for x in range(0, misses): main_surface.blit(miss, (surf_sz[0] - 113 + 37 * x, 12)) spriteMage.update() spriteMage.draw(main_surface) else: main_surface.blit(game_over, (250, 85)) pygame.display.flip() my_clock.tick(60) pygame.quit()
def doDie(self): oldpos = self.rect.topleft AnimatedSprite.__init__(self, configs.images["dead_anim"], self.fps) self.rect.topleft = oldpos self.dead = True
mageMegaPunch = pygame.image.load("sprites/mageMegaPunch.png") magePound = pygame.image.load("sprites/magePound.png") fire = pygame.image.load("sprites/fireballSprite.png") water = pygame.image.load("sprites/waterSprite.png") wind = pygame.image.load("sprites/windSprite.png") fireM = pygame.image.load("sprites/fireMinionSprite.png") iceM = pygame.image.load("sprites/iceMinionSprite.png") dustM = pygame.image.load("sprites/dustMinionSprite.png") surf_sz = (640, 480) mageX = surf_sz[0] / 20 mageY = (.25 * surf_sz[1], .5 * surf_sz[1], .75 * surf_sz[1]) spriteMage = AnimatedSprite(mageWait, (mageX, mageY[1]), 2, 1, 0, 2, 24) spriteM = [spriteMage, spriteMage, spriteMage] spriteAttack = [spriteMage, spriteMage, spriteMage] mStart = [(surf_sz[0], mageY[0] + .1 * spriteMage.frameHeight), (surf_sz[0], mageY[1] + .1 * spriteMage.frameHeight), (surf_sz[0], mageY[2] + .1 * spriteMage.frameHeight)] def genM(lane): global spriteM typeM = random.randint(0, 2) if typeM == 0: spriteM[lane] = AnimatedSprite(fireM, mStart[lane], 8, 1, 0, 8, 12) elif typeM == 1:
def readIndivFrames(fileType, path): switch1 = [[pygame.image.load("%sa1/1%s" % (path, fileType))], [pygame.image.load("%sa1/2%s" % (path, fileType))], [pygame.image.load("%sa1/3%s" % (path, fileType))], [pygame.image.load("%sa1/4%s" % (path, fileType))], [pygame.image.load("%sa1/5%s" % (path, fileType))], [pygame.image.load("%sa1/6%s" % (path, fileType))], [pygame.image.load("%sa1/7%s" % (path, fileType))], [pygame.image.load("%sa1/8%s" % (path, fileType))], [pygame.image.load("%sa1/9%s" % (path, fileType))]] switch2 = [[pygame.image.load("%sa2/1%s" % (path, fileType))], [pygame.image.load("%sa2/2%s" % (path, fileType))], [pygame.image.load("%sa2/3%s" % (path, fileType))], [pygame.image.load("%sa2/4%s" % (path, fileType))], [pygame.image.load("%sa2/5%s" % (path, fileType))], [pygame.image.load("%sa2/6%s" % (path, fileType))], [pygame.image.load("%sa2/7%s" % (path, fileType))], [pygame.image.load("%sa2/8%s" % (path, fileType))], [pygame.image.load("%sa2/9%s" % (path, fileType))]] instances = [] cnt = make while cnt > 0: animatedSprites = [] animatedSprites.append( [AnimatedSprite(switch1, '', 10), [(40 * cnt), 0, 2, 2]]) animatedSprites.append( [AnimatedSprite(switch2, '', 10), [(40 * cnt), 40, 2, 2]]) instances.append(animatedSprites) cnt = cnt - 1 trials = 0 while trials < 5: groups = len(instances) - 1 while groups >= 0: instances[groups][0][1][0] = 40 * groups instances[groups][0][1][1] = 0 instances[groups][1][1][0] = 40 * groups instances[groups][1][1][1] = 40 groups = groups - 1 changes = 0 start = time.time() while changes < 500: groups = len(instances) - 1 while groups >= 0: instances[groups][0][0].nextFrame() instances[groups][1][0].nextFrame() if instances[groups][0][1][0] < 0 or instances[groups][0][1][ 0] > WIDTH - 40: instances[groups][0][1][ 2] = instances[groups][0][1][2] * -1 if instances[groups][0][1][1] < 0 or instances[groups][0][1][ 1] > HEIGHT - 40: instances[groups][0][1][ 3] = instances[groups][0][1][3] * -1 if instances[groups][1][1][0] < 0 or instances[groups][1][1][ 0] > WIDTH - 40: instances[groups][1][1][ 2] = instances[groups][1][1][2] * -1 if instances[groups][1][1][1] < 0 or instances[groups][1][1][ 1] > HEIGHT - 40: instances[groups][1][1][ 3] = instances[groups][1][1][3] * -1 instances[groups][0][1][0] += instances[groups][0][1][2] instances[groups][0][1][1] += instances[groups][0][1][3] instances[groups][1][1][0] += instances[groups][1][1][2] instances[groups][1][1][1] += instances[groups][1][1][3] screen.blit(instances[groups][0][0].image[0], (instances[groups][0][0].image[0].get_rect().move( instances[groups][0][1][0], instances[groups][0][1][1]))) screen.blit(instances[groups][1][0].image[0], (instances[groups][1][0].image[0].get_rect().move( instances[groups][1][1][0], instances[groups][1][1][1]))) groups = groups - 1 pygame.display.flip() screen.fill((BACKGROUNDR, BACKGROUNDG, BACKGROUNDB)) changes = changes + 1 trials = trials + 1 print(trials) print(1 / ((time.time() - start) / 500))
def readIndivSheet(fileType, path): spriteSheet1 = Spritesheet(("%s1%s" % (path, fileType))) spriteSheet2 = Spritesheet(("%s2%s" % (path, fileType))) instances = [] cnt = make while cnt > 0: animatedSprites = [] animatedSprites.append([ AnimatedSprite(spriteSheet1.img_extract(9, 1, 40, 40), ("%stext.txt" % (path)), 10), [(40 * cnt), 0, 2, 2] ]) animatedSprites[0][0].addImages(spriteSheet2.img_extract(9, 1, 40, 40)) animatedSprites.append([ AnimatedSprite(spriteSheet1.img_extract(9, 1, 40, 40), ("%stext.txt" % (path)), 10), [(40 * cnt), 40, 2, 2] ]) animatedSprites[1][0].addImages(spriteSheet2.img_extract(9, 1, 40, 40)) instances.append(animatedSprites) cnt = cnt - 1 trials = 0 while trials < 5: groups = len(instances) - 1 while groups >= 0: instances[groups][0][1][0] = 40 * groups instances[groups][0][1][1] = 0 instances[groups][1][1][0] = 40 * groups instances[groups][1][1][1] = 40 groups = groups - 1 changes = 0 start = time.time() while changes < 500: groups = len(instances) - 1 while groups >= 0: instances[groups][0][0].nextAnimFrame("anim1") instances[groups][1][0].nextAnimFrame("anim2") if instances[groups][0][1][0] < 0 or instances[groups][0][1][ 0] > WIDTH - 40: instances[groups][0][1][ 2] = instances[groups][0][1][2] * -1 if instances[groups][0][1][1] < 0 or instances[groups][0][1][ 1] > HEIGHT - 40: instances[groups][0][1][ 3] = instances[groups][0][1][3] * -1 if instances[groups][1][1][0] < 0 or instances[groups][1][1][ 0] > WIDTH - 40: instances[groups][1][1][ 2] = instances[groups][1][1][2] * -1 if instances[groups][1][1][1] < 0 or instances[groups][1][1][ 1] > HEIGHT - 40: instances[groups][1][1][ 3] = instances[groups][1][1][3] * -1 instances[groups][0][1][0] += instances[groups][0][1][2] instances[groups][0][1][1] += instances[groups][0][1][3] instances[groups][1][1][0] += instances[groups][1][1][2] instances[groups][1][1][1] += instances[groups][1][1][3] screen.blit(instances[groups][0][0].image, (instances[groups][0][0].image.get_rect().move( instances[groups][0][1][0], instances[groups][0][1][1]))) screen.blit(instances[groups][1][0].image, (instances[groups][1][0].image.get_rect().move( instances[groups][1][1][0], instances[groups][1][1][1]))) groups = groups - 1 pygame.display.flip() screen.fill((BACKGROUNDR, BACKGROUNDG, BACKGROUNDB)) changes = changes + 1 trials = trials + 1 print(trials) print(1 / ((time.time() - start) / 500))
pos_y = ret[1] # load textures textureEven = load_texture(textureEven, "Player.png") textureOdd = load_texture(textureOdd, "Player2.png") textureSwim = load_texture(textureSwim, "PlayerSwim.png") texture_fight = load_texture(texture_fight, "fight.png") font = sf.Font.from_file("arial.ttf") vDatoInt = 0 # Asigno teclas segun si es par o impar texture = define_keyboard(keyboard, ident) animated_sprite = AnimatedSprite(sf.seconds(0.2), True, False) ret = set_animations(texture) walking_down = ret[0] walking_left = ret[1] walking_right = ret[2] walking_up = ret[3] current_animation = walking_down animated_sprite.texture = texture animated_sprite.position = (320, 320) final_tile = map_j.final_tile # TODO map_j.get_final_tile() frame_clock = sf.Clock()