def handle_click(mouse_pos: Tuple[int, int]): handled = False # tell interface to handle click if Interface().handle_interface_click(mouse_pos): handled = True # if interface couldn't handle click, tell objects to handle one else: for obj in Game().objects: if obj.handle_click(get_global_mouse_pos(mouse_pos)): handled = True if not handled: Interface().handle_empty_click(mouse_pos)
def __init__(self, screen: pygame.Surface): pygame.sprite.Sprite.__init__(self) self.image = screen self.rect = Interface().camera
def get_global_mouse_pos(mouse_pos: Tuple[int, int]) -> Tuple[int, int]: """Mouse position with a glance to camera position on the map""" return mouse_pos[0] + Interface().camera.x, mouse_pos[1] + Interface( ).camera.y
def handle_command_bad_execution(error_message): Interface().messages.add(error_message)
def handle_command_second_click(command): Interface().selected_command.clear()
def handle_command_first_click(command): Interface().selected_command.replace(command)
def handle_object_kill(obj): if Interface().selected_object.get() is obj: Interface().remove_all_info()
def handle_object_second_click(obj): Interface().remove_all_info()
def handle_object_first_click(obj): Interface().handle_object_click(obj)
def play_game(): """ Starts the game. """ # Game objects initialization starts. player_empire = Empire(user_configs.EMPIRE_RACE, name=user_configs.EMPIRE_NAME) enemy_empire = Empire(races.DWARFS, name='Durden') # Initialize game singletons. Game(player_empire, enemy_empire) Interface(player_empire, enemy_empire) Display(SCREEN) player_empire.set_city(user_configs.CITY_NAME) player_default_city = player_empire.get_city(user_configs.CITY_NAME) player_default_city.rect.x = 500 player_default_city.rect.centery = Map().rect.centery enemy_empire.set_city("Nuhen") enemy_default_city = enemy_empire.get_city("Nuhen") enemy_default_city.rect.right = Map().rect.right - 700 enemy_default_city.rect.centery = Map().rect.centery AI(enemy_empire) # Game objects initialization ends. while True: mouse_pressed = False for event in pygame.event.get(): if event.type == pygame.QUIT or \ event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: pygame.quit() quit() if event.type == pygame.MOUSEBUTTONDOWN: mouse_pressed = True key = pygame.key.get_pressed() mouse_pos = pygame.mouse.get_pos() if mouse_pressed: click_handler.handle_click(mouse_pos) # AI is singleton, which has been initialized before. AI().play_step() # If any of empires is out of cities, the game is finished. if not player_empire.alive() or not enemy_empire.alive(): finish_game(win=player_empire.alive(), screen=SCREEN) return Interface().move_view(key, mouse_pos) # Update objects. # It looks weird, but use Game().objects.update() causes an error in # specific case: if one of objects is killed during `update`, its `update` # method is still called. It happens because Game().objects.update() updates # ALL sprites which are contained in Game().objects at the moment of # Game().objects.update() is called. for obj in Game().objects: if obj in Game().objects: obj.update() # Make place of camera location visible. SCREEN.blit(Map().image, (-Interface().camera.x, -Interface().camera.y)) place_objects_on_display() Interface().draw_interface(SCREEN) # Show screen. pygame.display.update() # Cap the framerate. CLOCK.tick(50)
def place_objects_on_display(): """ Finds what objects can be displayed onto the screen and displays them. """ for obj in pygame.sprite.spritecollide(Display(), Game().objects, False): Display().image.blit(obj.image, (obj.rect.x - Interface().camera.x, obj.rect.y - Interface().camera.y))