def __init__(self): """""" super(InfiniteQuestWindow, self).__init__() self.setWindowTitle('Infinite Quest') self.mapview = MapView() #self.mapview = QGraphicsView() #self.sc = MapScene() #self.mapview.setScene(self.sc) self.mapview.installEventFilter(self) self.setCentralWidget(self.mapview) self.minimap = MiniMapView(self.mapview) minimap_dw = QDockWidget() minimap_dw.setWidget(self.minimap) self.addDockWidget(Qt.LeftDockWidgetArea, minimap_dw) self.statushero = StatusHeroWidget() statushero_dw = QDockWidget() statushero_dw.setWidget(self.statushero) self.addDockWidget(Qt.LeftDockWidgetArea, statushero_dw) #self.mapview.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding,QSizePolicy.MinimumExpanding)) self.world = World() self.hero = Hero(self.world.map(), *self.world.map().find_passable_landscape_coords()) self.connection = Connection(self.hero, self.world) self.game_state_update()
def main(screen): # -----------Debug------------ enable_pathfinder_screen = False # Draws the found pathfinder path # ---------------------------- clock = pygame.time.Clock() floor_s = pygame.Surface((10 * 64, 20 * 64)) world_s = pygame.Surface((10 * 64, 20 * 64)) pathfinder_screen_s = floor_s.copy() pathfinder_screen_s.set_colorkey(colorBlack) pathfinding_screen_reset = False sword_surface = resource_loader.load_sprite('sword') main_menu = MainMenu() map_loader = MapLoader(MapData, NPC, Intent) map_data = map_loader.load_default_map() path_finder = PathFinder(map_data.passable_tiles) dirty_drawing = DirtyDrawing() dirty_drawing.prepare_floor(floor_s, map_data.get_texture_layer) dirty_drawing.issue_world_surface_redraw() intent = Intent() hero = Hero(surface=resource_loader.load_sprite('hero'), intent_instance=Intent(), inventory_instance=Inventory()) if not map_data.tile_occupied((1, 1)): map_data.set_character_on_map(hero, (1, 1)) hero.move(1, 1) new_weapon = Weapon(generate_item_name('sword')) print hero.inventory hero.inventory.add_item(new_weapon) camera = Camera(starting_position=hero.positionOnMap, viewport_size=(10, 10)) camera.set_viewport_boundaries((0, 0), map_data.mapBoundaries) message_log = MessageLog(defaultFont) message_log.position = (screen.get_size()[0] - message_log.render.get_size()[0], 0) # Temporary. Combat is on at the start in_combat = False entities_in_combat = [] for entity in map_data.get_characters_on_map: entities_in_combat.append(entity) reaction_order = [] creature_in_turn = None new_event = pygame.event.Event(pygame.USEREVENT, subtype="combat", combat_situation="start") pygame.event.post(new_event) screen.fill(colorBlack) new_sword = Weapon(generate_item_name(), surface=sword_surface) map_data.set_item_on_map(new_sword, (3, 4)) # MAINLOOP-------------------------- while 1: clock.tick(40) hero.intent.type = 0 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: hero.intent.type = intent.WAIT elif event.key == pygame.K_UP or event.key == pygame.K_KP8: hero.intent.type = intent.MOVE hero.intent.direction = (0, -1) elif event.key == pygame.K_DOWN or event.key == pygame.K_KP2: hero.intent.type = intent.MOVE hero.intent.direction = (0, 1) elif event.key == pygame.K_LEFT or event.key == pygame.K_KP4: hero.intent.type = intent.MOVE hero.intent.direction = (-1, 0) elif event.key == pygame.K_RIGHT or event.key == pygame.K_KP6: hero.intent.type = intent.MOVE hero.intent.direction = (1, 0) elif event.key == pygame.K_KP7: hero.intent.type = intent.MOVE hero.intent.direction = (-1, -1) elif event.key == pygame.K_KP9: hero.intent.type = intent.MOVE hero.intent.direction = (1, -1) elif event.key == pygame.K_KP3: hero.intent.type = intent.MOVE hero.intent.direction = (1, 1) elif event.key == pygame.K_KP1: hero.intent.type = intent.MOVE hero.intent.direction = (-1, 1) elif event.key == pygame.K_KP_PLUS: npc = add_monster_to_random_position(map_data, NPC(resource_loader.load_sprite('thug'), Intent(), Inventory())) entities_in_combat.append(npc) elif event.key == pygame.K_KP_MINUS: pygame.event.post(pygame.event.Event(pygame.USEREVENT, subtype="menu")) elif event.key == pygame.K_i: message_log.newline("-----Inventory:-----") for item in hero.inventory.get_items: assert isinstance(item, Item) message_log.newline(item.name) elif event.key == pygame.K_COMMA: pick_up_item(hero, map_data) if event.type == pygame.USEREVENT: if event.subtype is "combat": if event.combat_situation == 'start': in_combat = True reaction_order = Dice.roll_reactions(entities_in_combat) message_log.newline('RR') pygame.event.post(pygame.event.Event(pygame.USEREVENT, subtype="combat", combat_situation='first_turn')) if event.combat_situation == 'first_turn': creature_in_turn = reaction_order[0] if creature_in_turn == hero: message_log.newline("You start") else: message_log.newline('{} starts'.format(reaction_order[0].name)) if event.combat_situation == 'turn_change': try: reaction_order.remove(creature_in_turn) except ValueError: print '{} not in reaction order.'.format(creature_in_turn) try: creature_in_turn = reaction_order[0] if creature_in_turn == hero: message_log.newline("Your turn.") else: message_log.newline("{}'s turn.".format(reaction_order[0].name)) except IndexError: creature_in_turn = None pygame.event.post(pygame.event.Event(pygame.USEREVENT, subtype="combat", combat_situation='end_of_phase')) if event.combat_situation == "end_of_phase" and in_combat: reaction_order = Dice.roll_reactions(entities_in_combat) creature_in_turn = None message_log.newline('EOP- RR') pathfinding_screen_reset = True pygame.event.post(pygame.event.Event(pygame.USEREVENT, subtype="combat", combat_situation='first_turn')) if event.combat_situation == "end": in_combat = False elif event.subtype is 'menu': main_menu.open_menu(screen) if in_combat is True: if isinstance(creature_in_turn, Hero): hero_makes_decision = hero_turn(hero, map_data, message_log) if hero_makes_decision: pygame.event.post(pygame.event.Event(pygame.USEREVENT, subtype="combat", combat_situation="turn_change")) if hero.intent.type is intent.MOVE: map_data.attempt_move("char", hero.positionOnMap, direction=hero.intent.direction) hero.move(*hero.intent.direction) elif hero.intent.type is intent.ATTACK: handle_attack(hero, hero.intent.target, message_log, map_data, entities_in_combat, reaction_order, sword_surface=sword_surface) elif hero.intent.type == intent.WAIT: pass else: raise "No Hero intention_:{}".format(hero.intent.type) elif isinstance(creature_in_turn, NPC): npc = creature_in_turn path = path_finder.find_path_between_points(npc.positionOnMap, hero.positionOnMap) if enable_pathfinder_screen == True: if pathfinding_screen_reset == True: pathfinder_screen_s.fill(colorBlack) pathfinding_screen_reset = False draw_pathfinder_path(pathfinder_screen_s, path) if path == 'path not found': pygame.event.post(pygame.event.Event(pygame.USEREVENT, subtype="combat", combat_situation="turn_change")) else: move_success = map_data.attempt_move("char", npc.positionOnMap, destination=path[1]) if move_success is True: # npc.move(*npc.intent.direction) npc.set_position(path[1]) pygame.event.post(pygame.event.Event(pygame.USEREVENT, subtype="combat", combat_situation="turn_change")) elif isinstance(move_success, NPC): npc.intent = Intent.WAIT pygame.event.post(pygame.event.Event(pygame.USEREVENT, subtype="combat", combat_situation="turn_change")) elif isinstance(move_success, Hero): handle_attack(npc, hero, message_log, map_data, entities_in_combat, reaction_order) pygame.event.post(pygame.event.Event(pygame.USEREVENT, subtype="combat", combat_situation="turn_change")) else: print "ERRORROROREREERRROR" camera.set_tile_position(hero.positionOnMap) if enable_pathfinder_screen: dirty_drawing.issue_world_surface_redraw() dirty_drawing.draw(screen, world_s, camera, floor_s, map_data) if message_log.get_is_dirty is True: dirty_drawing.draw_message_log(screen, message_log) if enable_pathfinder_screen: pass # dirty_drawing.draw_pathfinder_screen(screen, pathfinder_screen_s) pygame.display.flip()