all repos — Legends-RPG @ b5733447a499f57ee5e055cff73e18dbfac21354

A fantasy mini-RPG built with Python and Pygame.

Player attack strength is random based on his level. Enemy has health based on level.
Justin Armstrong justinmeister@gmail.com
Mon, 28 Apr 2014 16:21:04 -0700
commit

b5733447a499f57ee5e055cff73e18dbfac21354

parent

6631839e2ae03b23f4881a7a0de242c63c8dd613

M data/battlegui.pydata/battlegui.py

@@ -14,6 +14,7 @@ related information.

""" def __init__(self, game_data): self.game_data = game_data + self.enemy_damage = 0 self.state = c.SELECT_ACTION self.title_font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22) self.title_font.set_underline(True)

@@ -33,12 +34,17 @@ c.SELECT_ENEMY: 'Select an enemy.',

c.ENEMY_ATTACK: 'Enemy attacks player!', c.PLAYER_ATTACK: 'Player attacks enemy.', c.RUN_AWAY: 'Run away', - c.ENEMY_HIT: 'Enemy hit with 20 damage.', + c.ENEMY_HIT: self.enemy_hit(), c.ENEMY_DEAD: 'Enemy killed.', c.DISPLAY_ENEMY_ATTACK_DAMAGE: 'Player hit with 5 damage'} return state_dict + def enemy_hit(self): + """ + Return text of enemy being hit using calculated damage. + """ + return "Enemy hit with {} damage.".format(self.enemy_damage) def make_item_text(self): """

@@ -119,6 +125,13 @@ text_rect = text_surface.get_rect(x=50, y=50)

surface.blit(text_surface, text_rect) return surface + + def set_enemy_damage(self, enemy_damage): + """ + Set enemy damage in state dictionary. + """ + self.enemy_damage = enemy_damage + self.state_dict[c.ENEMY_HIT] = self.enemy_hit() def update(self): """Updates info box"""

@@ -349,8 +362,8 @@ """

Basic health meter for player. """ def __init__(self, select_box_rect, game_data): - self.health_stats = game_data['player stats']['Health'] - self.magic_stats = game_data['player stats']['Magic Points'] + self.health_stats = game_data['player stats']['health'] + self.magic_stats = game_data['player stats']['magic points'] self.title_font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22) self.posx = select_box_rect.centerx self.posy = select_box_rect.y - 5
M data/components/person.pydata/components/person.py

@@ -1,3 +1,4 @@

+from __future__ import division import math, random import pygame as pg from .. import setup

@@ -35,6 +36,8 @@ self.default_direction = direction

self.item = None self.wander_box = self.make_wander_box() self.observers = [] + self.level = 1 + self.health = 0 def create_spritesheet_dict(self, sheet_key): """Implemented by inheriting classes"""

@@ -373,6 +376,14 @@ Notify all observers of events.

""" for observer in self.observers: observer.on_notify(event) + + def calculate_hit(self): + """ + Calculate hit strength based on attack stats. + """ + max_strength = 5 + (self.level * 5) + min_strength = max_strength // 2 + return random.randint(min_strength, max_strength) class Player(Person):
M data/menugui.pydata/menugui.py

@@ -146,13 +146,14 @@

def show_player_stats(self): """Show the player's main stats""" title = 'STATS' - stat_list = ['Level', 'Health', - 'Magic Points', 'Experience to next level'] + stat_list = ['Level', 'health', + 'magic points', 'experience to next level'] surface, rect = self.make_blank_info_box(title) for i, stat in enumerate(stat_list): - if stat == 'Health' or stat == 'Magic Points': - text = (stat + ": " + str(self.player_stats[stat]['current']) + + if stat == 'health' or stat == 'magic points': + text = (stat[0].upper() + stat[1:] + ": " + + str(self.player_stats[stat]['current']) + " / " + str(self.player_stats[stat]['maximum'])) else: text = stat + ": " + str(self.player_stats[stat])
M data/observer.pydata/observer.py

@@ -36,11 +36,10 @@ c.SELECT_MAGIC: self.select_magic,

c.ENEMY_ATTACK: self.enemy_attack, c.SWITCH_ENEMY: self.switch_enemy, c.PLAYER_ATTACK: self.player_attack, - c.ATTACK_ANIMATION: self.attack_animation, + c.ATTACK_ANIMATION: self.enemy_damaged, c.RUN_AWAY: self.run_away, c.BATTLE_WON: self.battle_won, - c.ENEMY_ATTACK_DAMAGE: self.display_enemy_attack_damage, - c.PLAYER_FINISHED_ATTACK: self.player_finished_attack} + c.ENEMY_ATTACK_DAMAGE: self.display_enemy_attack_damage} return event_dict

@@ -112,17 +111,20 @@

self.player.enter_attack_state(enemy_to_attack) self.arrow.become_invisible_surface() - def attack_animation(self): + def enemy_damaged(self): """ Make an attack animation over attacked enemy. """ - self.arrow.remove_pos(self.player.attacked_enemy) + enemy_damage = self.player.calculate_hit() + + self.info_box.set_enemy_damage(enemy_damage) + self.info_box.state = c.ENEMY_HIT + self.arrow.state = c.SELECT_ACTION self.arrow.index = 0 - self.level.attack_enemy() + self.level.attack_enemy(enemy_damage) self.level.set_timer_to_current_time() self.level.state = c.ENEMY_HIT - self.info_box.state = c.ENEMY_HIT def run_away(self): self.level.end_battle()

@@ -130,5 +132,3 @@

def battle_won(self): self.level.end_battle() - def player_finished_attack(self): - pass
M data/shopgui.pydata/shopgui.py

@@ -291,8 +291,8 @@ item_type = item['type']

quantity = item['quantity'] value = item['price'] player_items = self.level.game_data['player inventory'] - player_health = self.level.game_data['player stats']['Health'] - player_magic = self.level.game_data['player stats']['Magic Points'] + player_health = self.level.game_data['player stats']['health'] + player_magic = self.level.game_data['player stats']['magic points'] item_to_add = {'quantity': quantity, 'value': value}
M data/states/battle.pydata/states/battle.py

@@ -95,6 +95,8 @@ for i, enemy in enumerate(enemy_group):

enemy.rect.topleft = pos_list[i] enemy.image = pg.transform.scale2x(enemy.image) enemy.index = i + enemy.level = 1 + enemy.health = enemy.level * 7 enemy_list = [enemy for enemy in enemy_group]

@@ -205,13 +207,16 @@ """

self.game_data['last state'] = self.name self.done = True - def attack_enemy(self): + def attack_enemy(self, enemy_damage): enemy = self.player.attacked_enemy + enemy.health -= enemy_damage self.set_enemy_indices() if enemy: - enemy.kill() - self.enemy_list.pop(enemy.index) + if enemy.health <= 0: + enemy.kill() + self.enemy_list.pop(enemy.index) + self.arrow.remove_pos(self.player.attacked_enemy) self.enemy_index = 0 posx = enemy.rect.x - 32 posy = enemy.rect.y - 64

@@ -236,7 +241,7 @@ self.player_health_box.draw(surface)

self.sword.draw(surface) def player_damaged(self, damage): - self.game_data['player stats']['Health']['current'] -= damage + self.game_data['player stats']['health']['current'] -= damage def set_timer_to_current_time(self): """Set the timer to the current time."""
M data/tools.pydata/tools.py

@@ -182,11 +182,11 @@

player_magic = {'current': 100, 'maximum': 100} - player_stats = {'Health': player_health, + player_stats = {'health': player_health, 'Level': 1, - 'Experience to next level': 100, - 'Magic Points': player_magic, - 'Attack Points': 10, + 'experience to next level': 100, + 'magic points': player_magic, + 'attack points': 10, 'Defense Points': 10}