all repos — Legends-RPG @ 4725cd9192b0e1268a61e6f175a3ccac71c78e52

A fantasy mini-RPG built with Python and Pygame.

Added a red tinting effect when the player gets damaged.
Justin Armstrong justinmeister@gmail.com
Wed, 30 Apr 2014 10:36:32 -0700
commit

4725cd9192b0e1268a61e6f175a3ccac71c78e52

parent

8493ba3ac66e56efea1ab86e9a28500b1ac0e1c7

M data/battlegui.pydata/battlegui.py

@@ -141,7 +141,10 @@ self.player_damage = player_damage

self.state_dict[c.DISPLAY_ENEMY_ATTACK_DAMAGE] = self.player_hit() def player_hit(self): - return "Player hit with {} damage".format(self.player_damage) + if self.player_damage: + return "Player hit with {} damage".format(self.player_damage) + else: + return "Enemy missed!" def update(self): """Updates info box"""
M data/components/attackitems.pydata/components/attackitems.py

@@ -72,7 +72,11 @@ def make_surface(self, damage):

""" Make the surface for the sprite. """ - return self.font.render(str(damage), True, c.RED) + if damage > 0: + text = "-{}".format(str(damage)) + return self.font.render(text, True, c.RED) + else: + return self.font.render('Miss', True, c.WHITE) def update(self): """
M data/components/person.pydata/components/person.py

@@ -1,5 +1,5 @@

from __future__ import division -import math, random +import math, random, copy import pygame as pg from .. import setup from .. import constants as c

@@ -378,7 +378,7 @@ """

Calculate hit strength based on attack stats. """ max_strength = self.level * 5 - min_strength = max_strength // 2 + min_strength = 0 return random.randint(min_strength, max_strength)

@@ -389,6 +389,10 @@ """

def __init__(self, direction, x=0, y=0, state='resting', index=0): super(Player, self).__init__('player', x, y, direction, state, index) + self.damaged = False + self.damage_timer = 0.0 + self.damage_alpha = 0 + self.fade_in = True def create_vector_dict(self): """Return a dictionary of x and y velocities set to

@@ -402,13 +406,40 @@ return vector_dict

def update(self, keys, current_time): """Updates player behavior""" + self.current_time = current_time + self.damage_animation() self.blockers = self.set_blockers() self.keys = keys - self.current_time = current_time self.check_for_input() state_function = self.state_dict[self.state] state_function() self.location = self.get_tile_location() + + def damage_animation(self): + """ + Put a red overlay over sprite to indicate damage. + """ + if self.damaged: + self.image = copy.copy(self.spritesheet_dict['facing left 2']) + self.image = pg.transform.scale2x(self.image) + damage_image = copy.copy(self.image).convert_alpha() + damage_image.fill((255, 0, 0, self.damage_alpha), special_flags=pg.BLEND_RGBA_MULT) + self.image.blit(damage_image, (0, 0)) + if self.fade_in: + self.damage_alpha += 25 + if self.damage_alpha >= 255: + self.fade_in = False + self.damage_alpha = 255 + elif not self.fade_in: + self.damage_alpha -= 25 + if self.damage_alpha <= 0: + self.damage_alpha = 0 + self.damaged = False + self.fade_in = True + self.image = self.spritesheet_dict['facing left 2'] + self.image = pg.transform.scale2x(self.image) + + def check_for_input(self): """Checks for player input"""
M data/observer.pydata/observer.py

@@ -95,6 +95,8 @@ self.level.enemy_index += 1

self.on_notify(c.ENEMY_ATTACK) def display_enemy_attack_damage(self): + if self.enemy_index > len(self.enemy_list) - 1: + self.enemy_index = 0 enemy = self.enemy_list[self.enemy_index] player_damage = enemy.calculate_hit() self.level.damage_points.add(

@@ -106,6 +108,7 @@ self.info_box.state = c.DISPLAY_ENEMY_ATTACK_DAMAGE

self.level.state = c.DISPLAY_ENEMY_ATTACK_DAMAGE self.level.set_timer_to_current_time() self.level.player_damaged(player_damage) + self.player.damaged = True def player_attack(self): enemy_posx = self.arrow.rect.x + 60
M data/states/battle.pydata/states/battle.py

@@ -1,7 +1,6 @@

"""This is the state that handles battles against monsters""" - -import random +import random, copy import pygame as pg from .. import tools, battlegui, observer from .. components import person, attack, attackitems

@@ -108,7 +107,6 @@ def make_player():

"""Make the sprite for the player's character""" player = person.Player('left', 630, 220, 'battle resting', 1) player.image = pg.transform.scale2x(player.image) - return player def make_selection_state_dict(self):

@@ -192,12 +190,6 @@ else:

self.state = c.SWITCH_ENEMY self.timer = self.current_time self.notify(self.state) - - - - - - def check_if_battle_won(self): """