Added explosion and ability to kill monsters during battle.
Justin Armstrong justinmeister@gmail.com
Sun, 20 Apr 2014 10:48:57 -0700
5 files changed,
89 insertions(+),
15 deletions(-)
M
data/collision.py
→
data/collision.py
@@ -95,7 +95,7 @@ def check_for_battle(self):
""" Switch scene to battle 1/5 times if battles are allowed. """ - random_number = random.randint(0, 2) + random_number = random.randint(0, 10) if random_number == 0: self.level.switch_to_battle = True
A
data/components/attack.py
@@ -0,0 +1,47 @@
+""" +Sprites for attacks. +""" + +import pygame as pg +from .. import setup, tools + +class Fire(pg.sprite.Sprite): + """ + Fire animation for attacks. + """ + def __init__(self, x, y): + super(Fire, self).__init__() + self.spritesheet = setup.GFX['explosion'] + self.get_image = tools.get_image + self.image_list = self.make_image_list() + self.index = 0 + self.image = self.image_list[self.index] + self.rect = self.image.get_rect(left=x, top=y) + self.timer = 0.0 + + def make_image_list(self): + """ + Make a list of images to cycle through for the + animation. + """ + image_list = [] + + for row in range(8): + for column in range(8): + posx = column * 128 + posy = row * 128 + new_image = self.get_image(posx, posy, 128, 128, + self.spritesheet) + image_list.append(new_image) + + return image_list + + def update(self): + """ + Update fire explosion. + """ + if self.index < (len(self.image_list) - 1): + self.index += 1 + self.image = self.image_list[self.index] + elif self.index == (len(self.image_list) - 1): + self.kill()
M
data/components/person.py
→
data/components/person.py
@@ -289,6 +289,7 @@ def enter_attack_state(self, enemy):
""" Set values for attack state. """ + self.attacked_enemy = enemy self.x_vel = 1 self.state = 'attack'@@ -303,14 +304,15 @@
self.rect.x += self.x_vel if self.x_vel == SLOW_BACK: - if self.rect.x >= self.origin_pos[0] + 10: + if self.rect.x >= self.origin_pos[0] + 20: self.x_vel = FAST_FORWARD elif self.x_vel == FAST_FORWARD: if self.rect.topleft >= self.origin_pos: self.image = self.spritesheet_dict['facing left 1'] self.image = pg.transform.scale2x(self.image) - elif self.rect.x <= self.origin_pos[0] - 100: + elif self.rect.x <= self.origin_pos[0] - 110: self.x_vel = FAST_BACK + self.observer.on_notify('attack animation') else: if self.rect.x >= self.origin_pos[0]: self.rect.x = self.origin_pos[0]
M
data/states/battle.py
→
data/states/battle.py
@@ -4,7 +4,7 @@
import random import pygame as pg from .. import tools, setup -from .. components import person +from .. components import person, attack from .. import constants as c #STATES@@ -16,6 +16,7 @@ PLAYER_ATTACK = 'player attack'
SELECT_ITEM = 'select item' SELECT_MAGIC = 'select magic' RUN_AWAY = 'run away' +ATTACK_ANIMATION = 'attack animation' #EVENTS@@ -34,8 +35,11 @@ self.game_data = game_data
self.background = self.make_background() self.enemy_group, self.enemy_pos_list = self.make_enemies() self.player = self.make_player() + self.attack_animations = pg.sprite.Group() self.info_box = InfoBox(game_data) self.arrow = SelectArrow(self.enemy_pos_list) + self.attacked_enemy = None + self.attacking_enemy = None self.select_box = SelectBox() self.state = SELECT_ACTION self.select_action_state_dict = self.make_selection_state_dict()@@ -97,6 +101,7 @@ """Update the battle state"""
self.check_input(keys) self.enemy_group.update(current_time) self.player.update(keys, current_time) + self.attack_animations.update() self.arrow.update(keys) self.draw_battle(surface)@@ -109,14 +114,15 @@ if self.allow_input:
if keys[pg.K_RETURN]: self.notify(END_BATTLE) - elif keys[pg.K_SPACE] and self.state == SELECT_ACTION: - self.state = self.select_action_state_dict[ - self.arrow.rect.topleft] - self.notify(self.state) + elif keys[pg.K_SPACE]: + if self.state == SELECT_ACTION: + self.state = self.select_action_state_dict[ + self.arrow.rect.topleft] + self.notify(self.state) - elif keys[pg.K_SPACE] and self.state == SELECT_ENEMY: - self.state = PLAYER_ATTACK - self.notify(self.state) + elif self.state == SELECT_ENEMY: + self.state = PLAYER_ATTACK + self.notify(self.state) self.allow_input = False@@ -140,6 +146,7 @@ def draw_battle(self, surface):
"""Draw all elements of battle state""" self.background.draw(surface) self.enemy_group.draw(surface) + self.attack_animations.draw(surface) surface.blit(self.player.image, self.player.rect) surface.blit(self.info_box.image, self.info_box.rect) surface.blit(self.select_box.image, self.select_box.rect)@@ -170,6 +177,7 @@ SELECT_ITEM: self.select_item,
SELECT_ENEMY: self.select_enemy, ENEMY_ATTACK: self.enemy_attack, PLAYER_ATTACK: self.player_attack, + ATTACK_ANIMATION: self.attack_animation, RUN_AWAY: self.run_away} return event_dict@@ -194,6 +202,7 @@ """
self.level.state = SELECT_ACTION self.arrow.index = 0 self.arrow.state = SELECT_ACTION + self.arrow.image = setup.GFX['smallarrow'] def select_enemy(self): self.level.state = SELECT_ENEMY@@ -217,13 +226,22 @@ if enemy.rect.topleft == enemy_pos:
enemy_to_attack = enemy self.player.enter_attack_state(enemy_to_attack) + self.arrow.become_invisible_surface() - - - + def attack_animation(self): + """ + Make an attack animation over attacked enemy. + """ + enemy = self.player.attacked_enemy + if enemy: + enemy.kill() + posx = enemy.rect.x - 32 + posy = enemy.rect.y - 64 + fire_sprite = attack.Fire(posx, posy) + self.level.attack_animations.add(fire_sprite) def run_away(self): - pass + self.level.end_battle() class InfoBox(object):@@ -436,6 +454,13 @@
if keys[pg.K_DOWN] == False and keys[pg.K_UP] == False \ and keys[pg.K_RIGHT] == False and keys[pg.K_LEFT] == False: self.allow_input = True + + def become_invisible_surface(self): + """ + Make image attribute an invisible surface. + """ + self.image = pg.Surface((32, 32)) + self.image.set_colorkey(c.BLACK) def enter_select_action(self):