def __init__(self, screen_dim): super().__init__(pymunk.Space()) pygame.font.init() self.font = pygame.font.SysFont('Comic Sans MS', 30) # Distance of mouse pointer from screen border that forces camera movement: self.dist_camera_mv = 200 self.grid_size = None self.grid_dimension = None self.block_size = 32 self.grid_pos = (0, 0) self.grid = [] self.screen_dim = screen_dim self.generate_grid() self.dragging = None self.text_current_orig = (5, 5) self.text_current = self.font.render('Current object:', False, (255, 255, 255)) self.text_obj_orig = (self.text_current_orig[0], self.text_current_orig[1] + self.text_current.get_height()) self.toolbox = self.load_objects(self.text_obj_orig[0] + self.text_current.get_width() + 50) self.curr_obj = ToolIcon(None, None) self.button_export = Button(ResourcesManager.get_image('export_icon'), (self.screen_dim[0] - 184, 10)) self.button_save = Button(ResourcesManager.get_image('save_icon'), (self.screen_dim[0] - 142, 10)) self.button_load = Button(ResourcesManager.get_image('load_icon'), (self.screen_dim[0] - 100, 10))
def setup_components(self): """Setting up and registering (happens automatically) components""" width, height = 1920, 1080 # Setup up background, title and clouds self.add_label("background", (width, height), (0, 0), "background", (width, height)) left_cloud = Cloud( position=(92, 175), dimension=(595, 259), height_coefficient=(176 / 1080), width_coefficient=(186 / 1920), image=ResourcesManager.get_image("left_cloud"), name="left_cloud", move_dir="right", obj_mgr=self.gui_mgr, default_res=(width, height) ) right_cloud = Cloud( position=(1392, 66), dimension=(476, 207), height_coefficient=(66 / 1080), width_coefficient=(1052 / 1920), image=ResourcesManager.get_image("right_cloud"), name="right_cloud", move_dir="left", obj_mgr=self.gui_mgr, default_res=(width, height) ) self.add_label("title", (width, height), (0, 0), "title", (width, height)) # setup labels etiquette_size = (416, 100) numbers_size = (100, 100) # setup buttons up_down_btn_size = (50, 50) # setup lower buttons lower_button_size = (268, 100) # Setting up players choice if self.multi: self.add_label("players_label", etiquette_size, (702, 467), "players_label", (width, height)) self.add_label("players_amount", numbers_size, (1118, 467), "amount_2", (width, height)) self.add_button("players_up", up_down_btn_size, (1218, 467), "btn_up", (width, height)) self.add_button("players_down", up_down_btn_size, (1218, 517), "btn_down", (width, height)) # Setting up maps choice self.add_label("maps_label", etiquette_size, (702, 617), "maps_label", (width, height)) self.add_label("maps_amount", numbers_size, (1118, 617), "amount_1", (width, height)) self.add_button("maps_up", up_down_btn_size, (1218, 617), "btn_up", (width, height)) self.add_button("maps_down", up_down_btn_size, (1218, 667), "btn_down", (width, height)) # Setting up lower buttons self.add_button("play_btn", lower_button_size, (1000, 767), "btn_play", (width, height)) self.add_button("cancel_btn", lower_button_size, (702, 767), "btn_cancel", (width, height))
def add_button(self, name, dim, pos, image, res): Button( name=name, dimension=dim, position=pos, image=ResourcesManager.get_image(image), image_hover=ResourcesManager.get_image(image + "_hover"), obj_mgr=self.gui_mgr, default_res=res )
def update_images(self): """function to change numbers while clicking on up/down buttons""" for element in self.gui_mgr.gui_elements: if isinstance(element, Label): if element.name == "players_amount": new_image = ResourcesManager.get_image("amount_" + str(self.players_amount)) element.set_image_original(new_image) element.set_size(element.get_size()) elif element.name == "maps_amount": new_image = ResourcesManager.get_image("amount_" + str(self.maps_to_play)) element.set_image_original(new_image) element.set_size(element.get_size())
def __init__(self, position, dimension, follow=None, id=-1, obj_mgr=None): image_set = [ ResourcesManager.get_image('part_fire01'), ResourcesManager.get_image('part_fire02'), ResourcesManager.get_image('part_fire03'), ResourcesManager.get_image('part_fire04') ] super().__init__(position, dimension, image_set, 0.3, follow=follow, id=id, obj_mgr=obj_mgr)
def from_template(cls, name, pos, dim, obj_mgr, vertical=1, horizontal=1, rotation=0, **kwargs): with open(template_dir + name + ".yaml") as file: config = yaml.load(file, Loader=yaml.FullLoader) if vertical != 1 or horizontal != 1: img = pygame.Surface((horizontal * dim[0], vertical * dim[1])) texture = pygame.transform.scale(ResourcesManager.get_image(name), dim) for x in range(horizontal): for y in range(vertical): img.blit(texture, (x * dim[0], y * dim[1])) Obstacle(pos, img.get_size(), obj_mgr, rotation=rotation, overlap=img, **config) else: Obstacle(pos, dim, obj_mgr, rotation=rotation, **config)
def __init__(self, pos, dim, obj_mgr, name=None, id=-1, elasticity=1.0, rotation=0, overlap=None, **kwargs): self.offset = None self.elasticity = elasticity if overlap is None: img = ResourcesManager.get_image(name) else: img = overlap super().__init__(pos, dim, img, id, name=name, obj_mgr=obj_mgr, rotation=rotation)
def load(map): root = tk.Tk() root.withdraw() file_path = filedialog.askopenfilename() if len(file_path) == 0 or file_path == "()": return with open(file_path) as file: data = json.load(file) map_data = data['map'] for i in range(len(map)): obj_list = map_data[i] if len(obj_list) > 0: tile = MapTile(map[i].rect, None) for obj in obj_list: tile.add_object( Object(pygame.transform.scale( ResourcesManager.get_image(obj['name']), obj['dim']), obj['name'], obj['rotation'], obj['type'], horiz=obj['horizontal'], vert=obj['vertical'])) map[i] = tile
def add_label(self, name, dim, pos, image, res): Label( name=name, dimension=dim, position=pos, image=ResourcesManager.get_image(image), obj_mgr=self.gui_mgr, default_res=res )
def __init__(self, position, dimension, obj_mgr, color): image = ResourcesManager.get_image('obj_ball_' + color) self.radius = dimension[0] / 2 super().__init__(position, dimension, image, obj_mgr=obj_mgr, name='ball', obj_type='dynamic') self.turn = False self.state = BallState.NOT_MOVING self.last_pos = self.get_position() self.particles_effect = None
def setup_components(self): """Setting up and registering(happens automatically) components""" width, height = 1920, 1080 # Setup up background, title and clouds background = Label(name="background", dimension=(width, height), position=(0, 0), image=ResourcesManager.get_image("background"), obj_mgr=self.gui_mgr, default_res=(width, height)) left_cloud = Cloud(position=(92, 175), dimension=(595, 259), height_coefficient=(176 / 1080), width_coefficient=(186 / 1920), image=ResourcesManager.get_image("left_cloud"), name="left_cloud", move_dir="right", obj_mgr=self.gui_mgr, default_res=(width, height)) right_cloud = Cloud(position=(1392, 66), dimension=(476, 207), height_coefficient=(66 / 1080), width_coefficient=(1052 / 1920), image=ResourcesManager.get_image("right_cloud"), name="right_cloud", move_dir="left", obj_mgr=self.gui_mgr, default_res=(width, height)) title = Label(name="title", dimension=(width, height), position=(0, 0), image=ResourcesManager.get_image("title"), obj_mgr=self.gui_mgr, default_res=(width, height)) # setup about label about = Label(name="about", dimension=(500, 500), position=(750, 450), image=ResourcesManager.get_image("about_label"), obj_mgr=self.gui_mgr, default_res=(width, height)) # setup buttons cancel_btn = Button( name="cancel_btn", dimension=(134, 50), position=(933, 970), image=ResourcesManager.get_image("btn_cancel"), image_hover=ResourcesManager.get_image("btn_cancel_hover"), obj_mgr=self.gui_mgr, default_res=(width, height))
def load_objects(origin): objects = {} off = 0 for root, dirnames, filenames in os.walk('./client/objects/templates'): for filename in filenames: if filename.endswith('.yaml'): name = os.path.splitext(filename)[0] yaml_file = open(os.path.join(root, filename)) parsed_yaml_file = yaml.load(yaml_file, Loader=yaml.FullLoader) t = parsed_yaml_file["type"] yaml_file.close() obj = Object( pygame.transform.scale( ResourcesManager.get_image(name), (32, 32)), name, 0, t) icon = ToolIcon(obj, pygame.Rect(origin + off * 32, 5, 32, 32)) objects[name] = icon off += 1 objects['cup'] = ToolIcon( Object( pygame.transform.scale(ResourcesManager.get_image('obj_hole'), (32, 32)), 'obj_hole', 0, 'cup'), pygame.Rect(origin + off * 32, 5, 32, 32)) off += 1 objects['ball'] = ToolIcon( Object( pygame.transform.scale( ResourcesManager.get_image('obj_ball_white'), (32, 32)), 'obj_ball_white', 0, 'ball'), pygame.Rect(origin + off * 32, 5, 32, 32)) off += 1 return objects
def setup_components(self): """setting up and registering(happens automatically) components""" width, height = 1920, 1080 # Setup up background, title and clouds background = Label(name="background", dimension=(width, height), position=(0, 0), image=ResourcesManager.get_image("background"), obj_mgr=self.gui_mgr, default_res=(width, height)) left_cloud = Cloud(position=(92, 175), dimension=(595, 259), height_coefficient=(176 / 1080), width_coefficient=(186 / 1920), image=ResourcesManager.get_image("left_cloud"), name="left_cloud", move_dir="right", obj_mgr=self.gui_mgr, default_res=(width, height)) right_cloud = Cloud(position=(1392, 66), dimension=(476, 207), height_coefficient=(66 / 1080), width_coefficient=(1052 / 1920), image=ResourcesManager.get_image("right_cloud"), name="right_cloud", move_dir="left", obj_mgr=self.gui_mgr, default_res=(width, height)) title = Label(name="title", dimension=(width, height), position=(0, 0), image=ResourcesManager.get_image("title"), obj_mgr=self.gui_mgr, default_res=(width, height)) # setup buttons button_names = ["single", "multi", "about", "quit"] button_size = (416, 98) offset = 30 for i, name in enumerate(button_names): Button(name=name, dimension=button_size, position=((width - button_size[0]) // 2 + offset, height // 2 + button_size[1] * (i - 1) + offset), image=ResourcesManager.get_image(name), image_hover=ResourcesManager.get_image(name + "_hover"), obj_mgr=self.gui_mgr, default_res=(width, height))
def __init__(self, pos, dim, obj_mgr, name=None, id=-1, friction=4, velocity_func="default", rotation=0, **kwargs): self.position = pos self.friction = friction self.velocity_func = SurfaceVelocities.get_by_name(velocity_func) super().__init__(pos, dim, ResourcesManager.get_image(name), id, name=name, obj_mgr=obj_mgr, rotation=rotation)
def show_score(self): text = 'level: ' + str(self.map.id) + ' score: ' label = self.font.render(text, 1, (200, 190, 140), self.fill_color) self.object_mgr.display.blit(label, (96, 4)) offset = 664 for player in self.players: if player.ball.turn: pygame.draw.rect(self.object_mgr.display, (240, 50, 50), (offset-2, 2, 60, 60), 2) else: pygame.draw.rect(self.object_mgr.display, self.fill_color, (offset-2, 2, 60, 60), 2) ball_img = ResourcesManager.get_image('obj_ball_' + player.color) ball_img = pygame.transform.scale(ball_img, (56, 56)) self.object_mgr.display.blit(ball_img, (offset, 4)) label = self.font.render(str(sum(v for k, v in player.points.items())), 1, (0, 0, 0)) label = pygame.transform.scale(label, (48, 56)) self.object_mgr.display.blit(label, (offset+4, 4)) offset += 70
def __init__(self, obj_mgr, pos=None, dim=None, id=-1, **kwargs): image = ResourcesManager.get_image('obj_hole') self.radius = dim[0] / 2 super().__init__(pos, dim, image, id=id, name='cup', obj_mgr=obj_mgr)