all repos — Legends-RPG @ edcd5c2abec6a2f46cf35d2023c924c837cce7ac

A fantasy mini-RPG built with Python and Pygame.

Added floating points for damage, sped up battle timing.
Justin Armstrong justinmeister@gmail.com
Tue, 29 Apr 2014 16:21:38 -0700
commit

edcd5c2abec6a2f46cf35d2023c924c837cce7ac

parent

ac0b622d09ad6f6bc948f4a6ed8f83fb7ad3b60c

4 files changed, 68 insertions(+), 9 deletions(-)

jump to
M data/components/attackitems.pydata/components/attackitems.py

@@ -4,6 +4,7 @@ """

import copy import pygame as pg from .. import tools, setup +from .. import constants as c class Sword(object):

@@ -52,3 +53,38 @@ """

if self.player.state == 'attack': surface.blit(self.image, self.rect) + +class DamagePoints(pg.sprite.Sprite): + """ + A sprite that shows how much damage an attack inflicted. + """ + def __init__(self, damage, topleft_pos): + super(DamagePoints, self).__init__() + self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 27) + self.image = self.make_surface(damage) + self.rect = self.image.get_rect(x=topleft_pos[0]+20, + bottom=topleft_pos[1]+10) + self.start_posy = self.rect.y + self.y_vel = -1 + self.dead = False + + def make_surface(self, damage): + """ + Make the surface for the sprite. + """ + return self.font.render(str(damage), True, c.RED) + + def update(self): + """ + Update sprite position or delete if necessary. + """ + self.rect.y += self.y_vel + if self.rect.y < (self.start_posy - 30): + self.kill() + + + + + + +
M data/constants.pydata/constants.py

@@ -26,6 +26,7 @@ BLACK_BLUE = (19, 15, 48)

NEAR_BLACK_BLUE = (20, 15, 48) LIGHT_BLUE = (0, 153, 204) DARK_RED = (118, 27, 12) +RED = (255, 0, 0) MAIN_FONT = 'DroidSans'
M data/observer.pydata/observer.py

@@ -2,7 +2,7 @@ """

Module for all game observers. """ from . import constants as c -from .components import attack +from .components import attackitems from . import setup class Battle(object):

@@ -97,6 +97,9 @@

def display_enemy_attack_damage(self): enemy = self.enemy_list[self.enemy_index] player_damage = enemy.calculate_hit() + self.level.damage_points.add( + attackitems.DamagePoints(player_damage, + self.player.rect.topright)) self.info_box.set_player_damage(player_damage) self.info_box.state = c.DISPLAY_ENEMY_ATTACK_DAMAGE

@@ -122,6 +125,9 @@ """

Make an attack animation over attacked enemy. """ enemy_damage = self.player.calculate_hit() + self.level.damage_points.add( + attackitems.DamagePoints(enemy_damage, + self.player.attacked_enemy.rect.topright)) self.info_box.set_enemy_damage(enemy_damage) self.info_box.state = c.ENEMY_HIT
M data/states/battle.pydata/states/battle.py

@@ -37,6 +37,7 @@ self.player_health_box = None

self.select_action_state_dict = {} self.next = None self.observers = [] + self.damage_points = pg.sprite.Group() def startup(self, current_time, game_data): """Initialize state attributes"""

@@ -130,6 +131,7 @@ self.attack_animations.update()

self.info_box.update() self.arrow.update(keys) self.sword.update(current_time) + self.damage_points.update() self.draw_battle(surface)

@@ -168,23 +170,34 @@ """

timed_states = [c.DISPLAY_ENEMY_ATTACK_DAMAGE, c.ENEMY_HIT, c.ENEMY_DEAD] + long_delay = timed_states[1:] - if self.state in timed_states: - if (self.current_time - self.timer) > 1200: - if self.state == c.DISPLAY_ENEMY_ATTACK_DAMAGE: - if self.enemy_index == (len(self.enemy_list) - 1): - self.state = c.SELECT_ACTION - else: - self.state = c.SWITCH_ENEMY - elif self.state == c.ENEMY_HIT: + if self.state in long_delay: + if (self.current_time - self.timer) > 600: + if self.state == c.ENEMY_HIT: self.state = c.ENEMY_DEAD elif self.state == c.ENEMY_DEAD: if len(self.enemy_list): self.state = c.ENEMY_ATTACK else: self.state = c.BATTLE_WON + self.timer = self.current_time + self.notify(self.state) + elif self.state == c.DISPLAY_ENEMY_ATTACK_DAMAGE: + if (self.current_time - self.timer) > 600: + if self.enemy_index == (len(self.enemy_list) - 1): + self.state = c.SELECT_ACTION + else: + self.state = c.SWITCH_ENEMY + self.timer = self.current_time self.notify(self.state) + + + + + + def check_if_battle_won(self): """

@@ -240,6 +253,7 @@ surface.blit(self.info_box.image, self.info_box.rect)

surface.blit(self.select_box.image, self.select_box.rect) surface.blit(self.arrow.image, self.arrow.rect) self.player_health_box.draw(surface) + self.damage_points.draw(surface) def player_damaged(self, damage):

@@ -248,3 +262,5 @@

def set_timer_to_current_time(self): """Set the timer to the current time.""" self.timer = self.current_time + +