Will flip to a mostly non-functional battle state 1/5 times on the overworld.
@@ -1,18 +1,22 @@
-__author__ = 'justinarmstrong' +import random import pygame as pg +from . import constants as c class CollisionHandler(object): """Handles collisions between the user, blockers and computer characters""" - def __init__(self, player, blockers, sprites): + def __init__(self, player, blockers, sprites, level): self.player = player self.static_blockers = blockers self.blockers = self.make_blocker_list(blockers, sprites) self.sprites = sprites + self.level = level def make_blocker_list(self, blockers, sprites): - """Return a combined list of sprite blockers and object blockers""" + """ + Return a combined list of sprite blockers and object blockers. + """ blocker_list = [] for blocker in blockers:@@ -25,7 +29,9 @@ return blocker_list
def update(self, keys, current_time): - """Checks for collisions between game objects""" + """ + Check for collisions between game objects. + """ self.blockers = self.make_blocker_list(self.static_blockers, self.sprites) self.player.rect.move_ip(self.player.x_vel, self.player.y_vel)@@ -36,6 +42,8 @@ sprite.rect.move_ip(sprite.x_vel, sprite.y_vel)
self.check_for_blockers() if self.player.rect.x % 32 == 0 and self.player.rect.y % 32 == 0: + if not self.player.state == 'resting': + self.check_for_battle() self.player.begin_resting() for sprite in self.sprites:@@ -84,6 +92,15 @@ if sprite.x_vel != 0:
sprite.rect.x -= sprite.x_vel else: sprite.rect.y -= sprite.y_vel + + def check_for_battle(self): + """ + Switch scene to battle 1/5 times if battles are allowed. + """ + random_number = random.randint(0, 5) + + if random_number == 0: + self.level.switch_to_battle = True
@@ -76,7 +76,8 @@ state_dict = {'resting': self.resting,
'moving': self.moving, 'animated resting': self.animated_resting, 'autoresting': self.auto_resting, - 'automoving': self.auto_moving} + 'automoving': self.auto_moving, + 'battle resting': self.battle_resting} return state_dict@@ -182,7 +183,7 @@ """
self.image = self.image_list[self.index] assert(self.rect.y % 32 == 0), ('Player not centered on tile: ' - + str(self.rect.y)) + + str(self.rect.y) + " : " + str(self.name)) assert(self.rect.x % 32 == 0), ('Player not centered on tile' + str(self.rect.x))@@ -275,6 +276,12 @@ random.shuffle(direction_list)
direction = direction_list[0] self.begin_auto_moving(direction) self.move_timer = self.current_time + + def battle_resting(self): + """ + Player stays still during battle state unless he attacks. + """ + pass def auto_moving(self): """@@ -291,8 +298,8 @@ """
User controlled character. """ - def __init__(self, direction, x=0, y=0): - super(Player, self).__init__('player', x, y, direction) + def __init__(self, direction, x=0, y=0, state='resting'): + super(Player, self).__init__('player', x, y, direction, state) def create_vector_dict(self): """Return a dictionary of x and y velocities set to@@ -327,12 +334,6 @@ elif self.keys[pg.K_RIGHT]:
self.begin_moving('right') - -class Soldier(Person): - """Soldier for the castle""" - - def __init__(self, x, y, direction='down', state='resting'): - super(Soldier, self).__init__('soldier', x, y, direction, state) class Well(pg.sprite.Sprite):
@@ -1,6 +1,5 @@
from data.states.main_menu import main_menu -from data.states import shop -from data.states import levels +from data.states import shop, levels, battle from . import setup, tools from . import constants as c@@ -27,13 +26,14 @@ state_dict = {MAIN_MENU: main_menu.Menu(),
TOWN: levels.LevelState(TOWN), CASTLE: levels.LevelState(CASTLE), HOUSE: levels.LevelState(HOUSE), - OVERWORLD: levels.LevelState(OVERWORLD), + OVERWORLD: levels.LevelState(OVERWORLD, True), BROTHER_HOUSE: levels.LevelState(BROTHER_HOUSE), INN: shop.Inn(), ARMOR_SHOP: shop.ArmorShop(), WEAPON_SHOP: shop.WeaponShop(), MAGIC_SHOP: shop.MagicShop(), POTION_SHOP: shop.PotionShop(), + BATTLE: battle.Battle() } run_it.setup_states(state_dict, c.MAIN_MENU)
@@ -18,6 +18,8 @@ self.background = self.make_background()
self.enemy_group = self.make_enemies() self.player_group = self.make_player() self.menu = None + self.name = 'battle' + self.next = game_data['last state'] def make_background(self): """Make the blue/black background"""@@ -32,29 +34,38 @@ return background_group
def make_enemies(self): """Make the enemies for the battle. Return sprite group""" - enemy = person.Devil(100, 100, 'down', 'resting') + enemy = person.Person('devil', 100, 100, 'down', 'battle resting') group = pg.sprite.Group(enemy) return group def make_player(self): """Make the sprite for the player's character""" - player = person.Player('left', 300, 300) + player = person.Player('left', 300, 300, 'battle resting') player_group = pg.sprite.Group(player) return player_group def update(self, surface, keys, current_time): """Update the battle state""" - self.enemy_group.update() - self.player_group.update() - self.menu.update() + self.check_input(keys) + self.enemy_group.update(current_time) + self.player_group.update(keys, current_time) + #self.menu.update() self.draw_battle(surface) + def check_input(self, keys): + """ + Check user input to navigate GUI. + """ + if keys[pg.K_SPACE]: + self.game_data['last state'] = self.name + self.done = True + def draw_battle(self, surface): """Draw all elements of battle state""" self.background.draw(surface) self.enemy_group.draw(surface) self.player_group.draw(surface) - self.menu.draw(surface) + #self.menu.draw(surface)
@@ -16,10 +16,11 @@ from .. import setup
class LevelState(tools._State): - def __init__(self, name): + def __init__(self, name, battles=False): super(LevelState, self).__init__() self.name = name self.tmx_map = setup.TMX[name] + self.allow_battles = battles def startup(self, current_time, game_data): """@@ -28,6 +29,7 @@ """
self.game_data = game_data self.current_time = current_time self.state = 'normal' + self.switch_to_battle = False self.allow_input = False self.cut_off_bottom_map = ['castle', 'town'] self.renderer = tilerender.Renderer(self.tmx_map)@@ -45,7 +47,8 @@ self.sprites = self.make_sprites()
self.collision_handler = collision.CollisionHandler(self.player, self.blockers, - self.sprites) + self.sprites, + self) self.dialogue_handler = textbox.TextHandler(self) self.state_dict = self.make_state_dict() self.portals = self.make_level_portals()@@ -79,14 +82,19 @@ """
player = person.Player(self.game_data['last direction']) last_state = self.game_data['last state'] - for object in self.renderer.tmx_data.getObjects(): - properties = object.__dict__ - if properties['name'] == 'start point': - if last_state == properties['state']: - posx = properties['x'] * 2 - posy = (properties['y'] * 2) - 32 - player.rect.x = posx - player.rect.y = posy + if last_state == 'battle': + player.rect.x = self.game_data['last location'][0] * 32 + player.rect.y = self.game_data['last location'][1] * 32 + + else: + for object in self.renderer.tmx_data.getObjects(): + properties = object.__dict__ + if properties['name'] == 'start point': + if last_state == properties['state']: + posx = properties['x'] * 2 + posy = (properties['y'] * 2) - 32 + player.rect.x = posx + player.rect.y = posy return player@@ -196,12 +204,11 @@ self.check_for_portals()
self.player.update(keys, current_time) self.sprites.update(current_time) self.collision_handler.update(keys, current_time) + self.check_for_battle() self.dialogue_handler.update(keys, current_time) self.check_for_menu(keys) self.viewport_update() - self.draw_level(surface) - def check_for_portals(self): """@@ -215,6 +222,16 @@ self.next = portal.name
self.update_game_data() self.done = True + def check_for_battle(self): + """ + Check if the flag has been made true, indicating + to switch state to a battle. + """ + if self.switch_to_battle and self.allow_battles: + self.player.location = self.player.get_tile_location() + self.update_game_data() + self.next = 'battle' + self.done = True def check_for_menu(self, keys): """