Textboxes are now closed based on user input. Level freezes while dialogue box is on screen
Justin Armstrong justinmeister@gmail.com
Sat, 15 Mar 2014 11:46:20 -0700
5 files changed,
61 insertions(+),
20 deletions(-)
M
data/components/textbox.py
→
data/components/textbox.py
@@ -3,7 +3,7 @@ import pygame as pg
from .. import setup from .. import constants as c -class Dialogue(object): +class DialogueBox(object): """Text box used for dialogue""" def __init__(self, x, dialogue): self.bground = setup.GFX['dialoguebox']@@ -17,12 +17,16 @@ self.dialogue_image = self.font.render(dialogue, False, c.NEAR_BLACK)
self.dialogue_rect = self.dialogue_image.get_rect(left=50, top=50) self.image.blit(self.dialogue_image, self.dialogue_rect) self.done = False + self.arrow_image = setup.GFX['rightarrow'] + self.arrow_rect = self.arrow_image.get_rect(right=self.rect.right - 20, + bottom=self.rect.bottom - 10) + self.image.blit(self.arrow_image, self.arrow_rect) - def update(self, current_time): + def update(self, current_time, keys): """Updates scrolling text""" self.current_time = current_time self.animate_dialogue() - self.terminate_check() + self.terminate_check(keys) def animate_dialogue(self):@@ -33,35 +37,42 @@
self.image.blit(text_image, text_rect) - def terminate_check(self): + def terminate_check(self, keys): """Remove textbox from sprite group after 2 seconds""" if self.timer == 0.0: self.timer = self.current_time - elif (self.current_time - self.timer) > 3000: - self.done = True + elif (self.current_time - self.timer) > 300: + if keys[pg.K_SPACE]: + self.done = True class DialogueHandler(object): """Handles interaction between sprites to create dialogue boxes""" - def __init__(self, player, sprites): + def __init__(self, player, sprites, level_object): self.player = player self.sprites = sprites self.textbox = None + self.level = level_object + self.last_textbox_timer = 0.0 def update(self, keys, current_time): """Checks for the creation of Dialogue boxes""" if keys[pg.K_SPACE] and not self.textbox: for sprite in self.sprites: - self.check_for_dialogue(sprite) + if (current_time - self.last_textbox_timer) > 300: + self.check_for_dialogue(sprite) if self.textbox: - self.textbox.update(current_time) + self.level.state = 'dialogue' + self.textbox.update(current_time, keys) if self.textbox.done: + self.level.state = 'normal' self.textbox = None + self.last_textbox_timer = current_time def check_for_dialogue(self, sprite):@@ -71,16 +82,16 @@ tile_x, tile_y = player.location
if player.direction == 'up': if sprite.location == (tile_x, tile_y - 1): - self.textbox = Dialogue(400, sprite.dialogue) + self.textbox = DialogueBox(400, sprite.dialogue) elif player.direction == 'down': if sprite.location == (tile_x, tile_y + 1): - self.textbox = Dialogue(400, sprite.dialogue) + self.textbox = DialogueBox(400, sprite.dialogue) elif player.direction == 'left': if sprite.location == (tile_x - 1, tile_y): - self.textbox = Dialogue(400, sprite.dialogue) + self.textbox = DialogueBox(400, sprite.dialogue) elif player.direction == 'right': if sprite.location == (tile_x + 1, tile_y): - self.textbox = Dialogue(400, sprite.dialogue) + self.textbox = DialogueBox(400, sprite.dialogue) def draw(self, surface):
M
data/states/sprite_start_pos.txt
→
data/states/sprite_start_pos.txt
@@ -44,7 +44,7 @@ T0T2220000011000000010T0T
0T0010000001100000001T0T0 T0T0111111111111111110T0T 0T0000000001100000000T0T0 -T0T0T00W000110000000T0T0T +T0T0T00W0F0110000000T0T0T 000T00000001100000000T0T0 -0T000T00F0011000000T00T00 +0T000T0000011000000T00T00 00000000000P1000000000000
M
data/states/town.py
→
data/states/town.py
@@ -16,6 +16,7 @@ def startup(self, current_time, persist):
"""Called when the State object is created""" self.persist = persist self.current_time = current_time + self.state = 'normal' self.town_map = tm.create_town_map() self.viewport = tm.create_viewport(self.town_map) self.blockers = tm.create_blockers()@@ -30,13 +31,15 @@ self.collision_handler = collision.CollisionHandler(self.player,
self.blockers, self.town_sprites) self.dialogue_handler = textbox.DialogueHandler(self.player, - self.town_sprites) + self.town_sprites, + self) + self.state_dict = self.make_state_dict() def set_sprite_dialogue(self): """Sets unique dialogue for each sprite""" for sprite in self.town_sprites: - if sprite.location == (9, 49): + if sprite.location == (10, 47): sprite.dialogue = 'Welcome to our town, Mr. Traveller!' elif sprite.location == (16, 42): sprite.dialogue = 'You seem tired, why not rest at our Inn?'@@ -51,8 +54,16 @@ elif sprite.location == (14, 8):
sprite.dialogue = 'Move along, citizen.' - def update(self, surface, keys, current_time): - """Updates state""" + def make_state_dict(self): + """Make a dictionary of states the level can be in""" + state_dict = {'normal': self.running_normally, + 'dialogue': self.handling_dialogue} + + return state_dict + + + def running_normally(self, surface, keys, current_time): + """Update level normally""" self.player.update(keys, current_time) self.town_sprites.update(current_time) self.collision_handler.update(keys, current_time)@@ -62,6 +73,18 @@
self.draw_level(surface) + def handling_dialogue(self, surface, keys, current_time): + """Update only dialogue boxes""" + self.dialogue_handler.update(keys, current_time) + self.draw_level(surface) + + + def update(self, surface, keys, current_time): + """Updates state""" + state_function = self.state_dict[self.state] + state_function(surface, keys, current_time) + + def viewport_update(self): """Viewport stays centered on character, unless at edge of map""" self.viewport.center = self.player.rect.center@@ -76,6 +99,11 @@ self.town_sprites.draw(self.level_surface)
surface.blit(self.level_surface, (0, 0), self.viewport) self.dialogue_handler.draw(surface) + + + def get_event(self, event): + """Set event to level attribute""" + self.event = event
M
data/tools.py
→
data/tools.py
@@ -44,7 +44,9 @@ self.state.startup(self.current_time, persist)
self.state.previous = previous def event_loop(self): - for event in pg.event.get(): + self.events = pg.event.get() + + for event in self.events: if event.type == pg.QUIT: self.done = True elif event.type == pg.KEYDOWN: