def display_dialog(surface: Surface, name: str, dialog: str): WIDTH = 455 HEIGHT = 205 BORDER = 5 IN_WIDTH = WIDTH - BORDER IN_HEIGHT = HEIGHT - BORDER canevas = Surface((WIDTH, HEIGHT)) canevas.fill(color.TEXT_BACKGROUND_COLOR) pos = Vector(dimensions.WINDOW_WIDTH - (30 + IN_WIDTH), dimensions.WINDOW_HEIGHT - (30 + IN_HEIGHT)) sizes = Vector(IN_WIDTH, IN_HEIGHT) rect = pygame.Rect(Vector().to_pos(), sizes.to_pos()) draw.rect(canevas, color.WHITE, rect, 5) font_name = ImagesCache().fonts["dialog"].render("{}: ".format(name), True, color.TEXT_NAME_COLOR) canevas.blit(font_name, (5, 4)) height = 30 for line in break_dialog_lines(dialog): font_text = ImagesCache().fonts["dialog"].render( line, True, color.TEXT_FOREGROUND_COLOR) canevas.blit(font_text, (10, height)) height += 20 return functools.partial(surface.blit, canevas, pos.to_pos())
def display(self, game_display, dt): def nop(): pass if self.client_name is None: return nop image = ImagesCache().images[self.client_name] height = image.get_rect().size[1] return drawer.add_image(game_display, image, POS_CHARACTER - Vector(0, height))
def __init__(self): self.last_frame_ticks = pygame.time.get_ticks() self.delta_t = 0 self.display_width = dimensions.WINDOW_WIDTH self.display_height = dimensions.WINDOW_HEIGHT self.persistent_display = collections.OrderedDict() self.temporary_display = [] self.actions = [] self.state_executor = StateExecutor() self.image_cache = ImagesCache() self.init_cache()
def display(self, game_display, dt): self.image = ImagesCache().images[self.client_name] move = Vector(0, 500) if self.accumulated_time < IN_OUT_TIME_SECOND: self.accumulated_time += dt / 1000 else: self.finished = True per = self.accumulated_time / IN_OUT_TIME_SECOND if self.is_in: move = move.multiply(1 - per) else: move = move.multiply(per) height = self.image.get_rect().size[1] pos = POS_CHARACTER - Vector(0, height) + move return functools.partial(game_display.blit, self.image, pos.to_pos())
def __init__(self, sprite_sheet_id, idx=0): super().__init__() self.sprite_sheet: SpriteSheet = ImagesCache( ).sprites_sheets[sprite_sheet_id] self.elapsed_time = 0 self.pos = Vector() self.scale = Vector() self.sprites = self.sprite_sheet.all_sprites() self.idx = idx
def display(self, game_display, dt): sprite_sheet: SpriteSheet = ImagesCache().sprites_sheets[ self.button.idx] image = sprite_sheet.get_element(0, 1) if self.sound is None: self.sound = pygame.mixer.Sound( 'resource/sounds/Button-push-nolag-1s.wav') ChannelManager().play('effect', self.sound, 1) return drawer.add_image(game_display, image, self.button.coordinates, self.button.size)
def display(self, game_display, dt): self.accumulated_time += (dt / 1000) image = ImagesCache().images['elevator-gate'] self.start_sound_effect("resource/sounds/Porte-ouvre-ferme-2s.wav") if self.accumulated_time < DEFAULT_TIME_DOOR: delta_x = CLOSED_WIDTH - OPEN_WIDTH width = OPEN_WIDTH + easing(self.accumulated_time, 0, delta_x, DEFAULT_TIME_DOOR) self.size.x = int(width) else: self.stop_sound_effect() self.finished = True return drawer.add_image(game_display, image, self.coordinates, self.size)
def init_cache(self): background_idx, background_path = images.BACKGROUND_IMAGE self.image_cache.add_sprites_sheets(background_idx, background_path, Vector(1024, 1024)) self.image_cache.add_image(*images.FLOOR_INDICATOR) self.image_cache.add_image(*images.ELEVATOR_GATE) self.image_cache.add_font("dialog", "resource/font/OldNewspaperTypes.ttf", DIALOG_POLICE_SIZE) self.image_cache.add_font("tips", "resource/font/OldStandard-Regular.ttf", 20) with open('resource/json/clients.json') as json_file: json_raw = json.load(json_file) for client in json_raw["clients"]: ImagesCache().add_image(client["name"], client["ressource"]) for i in range(10): idx, path = images.BUTTON_PATTERN idx = idx.format(i) path = path.format(i) self.image_cache.add_sprites_sheets(idx, path, button.BUTTON_SPRITE_SIZE)
class Game: def __init__(self): self.last_frame_ticks = pygame.time.get_ticks() self.delta_t = 0 self.display_width = dimensions.WINDOW_WIDTH self.display_height = dimensions.WINDOW_HEIGHT self.persistent_display = collections.OrderedDict() self.temporary_display = [] self.actions = [] self.state_executor = StateExecutor() self.image_cache = ImagesCache() self.init_cache() def init_cache(self): background_idx, background_path = images.BACKGROUND_IMAGE self.image_cache.add_sprites_sheets(background_idx, background_path, Vector(1024, 1024)) self.image_cache.add_image(*images.FLOOR_INDICATOR) self.image_cache.add_image(*images.ELEVATOR_GATE) self.image_cache.add_font("dialog", "resource/font/OldNewspaperTypes.ttf", DIALOG_POLICE_SIZE) self.image_cache.add_font("tips", "resource/font/OldStandard-Regular.ttf", 20) with open('resource/json/clients.json') as json_file: json_raw = json.load(json_file) for client in json_raw["clients"]: ImagesCache().add_image(client["name"], client["ressource"]) for i in range(10): idx, path = images.BUTTON_PATTERN idx = idx.format(i) path = path.format(i) self.image_cache.add_sprites_sheets(idx, path, button.BUTTON_SPRITE_SIZE) def compute_delta_t(self): ticks = pygame.time.get_ticks() self.delta_t = ticks - self.last_frame_ticks self.last_frame_ticks = ticks def construct_background(self, game_display): background_sprite_sheet: SpriteSheet = self.image_cache.sprites_sheets['background'] background_sprite = background_sprite_sheet.get_element(0, 0) self.persistent_display['background'] = \ drawer.add_image(game_display, background_sprite, Vector(), Vector(dimensions.WINDOW_WIDTH, dimensions.WINDOW_HEIGHT), 0) self.persistent_display['floor-indicator'] = \ drawer.add_image(game_display, self.image_cache.images['floor-indicator'], DEFAULT_FLOOR_INDICATOR_POS, DEFAULT_FLOOR_INDICATOR_SCALE, 89) self.persistent_display['client'] = NoClientAction().display(game_display, 0) self.persistent_display['elevator-gate'] = ElevatorGateOpenAction().display(game_display, 0) def init_keypad(self, game_display): for i in range(NUMBER_OF_BUTTONS_COLS): for j in range(NUMBER_OF_BUTTONS_ROWS): floor = button.compute_floor(i, j) self.persistent_display["button-{}".format(floor)] = ButtonBuilder().add_button(game_display, i, j) def main(self): game_display: Surface = display.set_mode((self.display_width, self.display_height)) display.set_caption('Natural 20: Challenge Pixel 2017') clock: Clock = pygame.time.Clock() self.construct_background(game_display) self.init_keypad(game_display) crashed = False _ = BackgroundMusicTrack() while not crashed: game_display.fill(color.BLACK) self.compute_delta_t() for displayable in self.persistent_display.values(): displayable() for displayable in self.temporary_display: displayable() self.temporary_display.clear() str_tips = "{:>6.2f}$".format(Blackboard().tips) self.temporary_display.append(drawer.add_text(game_display, str_tips, Vector(90, 380), color.MONEY_COLOR)) for event in pygame.event.get(): if event.type == pygame.QUIT: crashed = True else: self.actions.append(handler.handle(game_display, event, self.persistent_display)) self.actions = [action for action in self.actions if action] domain_actions = self.state_executor.exec(self.delta_t, self.actions) self.actions.clear() for domain_action in domain_actions: if domain_action.persistent_name: self.persistent_display[domain_action.persistent_name] =\ domain_action.display(game_display, self.delta_t) else: self.temporary_display.append(domain_action.display(game_display, self.delta_t)) # TEST pygame.display.update() clock.tick(FPS)
def display(self, game_display, dt): self.sprite_sheet: SpriteSheet = ImagesCache().sprites_sheets[ self.button.idx] self.image = self.sprite_sheet.get_element(0, 0) return drawer.add_image(game_display, self.image, self.button.coordinates, self.button.size)
def add_text(surface: Surface, text: str, pos: Vector, text_color=color.TEXT_FOREGROUND_COLOR): font_text = ImagesCache().fonts["tips"].render(text, True, text_color) return functools.partial(surface.blit, font_text, pos.to_pos())
def draw_image(self, game_display): image = ImagesCache().images['floor-indicator'] pos = DEFAULT_FLOOR_INDICATOR_POS scale = DEFAULT_FLOOR_INDICATOR_SCALE return drawer.add_image(game_display, image, pos, scale, self.angle)
def display(self, game_display, dt): self.finished = True sprite_sheet: SpriteSheet = ImagesCache().sprites_sheets['background'] sprite = sprite_sheet.get_element(0, self.floor + 1) return drawer.add_image(game_display, sprite, Vector(), self.size)
def display(self, surface: Surface): img_cache = ImagesCache() sprite_sheet: SpriteSheet = img_cache.sprites_sheets[ 'button-{}'.format(self.floor)] sprite = sprite_sheet.get_element(0, 0) return drawer.add_image(surface, sprite, self.coordinates, self.size)