def load_image(self): self.img = None self.imgs = [] img = pygame.Surface((Game.BLOCK_SIZE * 3, Game.BLOCK_SIZE * 3), pygame.SRCALPHA, 32).convert_alpha() #not game scale center = img.get_rect().center #TODO: blit small diagonal arm image on img (after blitting item? but before rotation) if self.item is not None: #TODO: self.item may be a dummy pickled object that hasn't been loaded- what should we do? #does the parent reference also get lost? that would be bad #self.item.load_image() item_img = self.item.imgs[0] item_img = Images.scale(item_img, 1 / Game.SCALE) img.blit(item_img, (0, 0)) start_deg = math.degrees(self.angle_start - 2 * self.angle_mid) end_deg = math.degrees(self.angle_end - 2 * self.angle_mid) for i in range(0, self.max_decay + 1, FRAME_LENGTH): t = (self.max_decay - i) / self.max_decay angle = (1 - t) * start_deg + t * end_deg + 225 rotated_img = Images.rotate(img, angle) rotated_img.get_rect().center = center self.imgs.append(Images.scale(rotated_img, Game.SCALE))
def load_blocks(): blocks_file = open("blocks.json", "r") global blocks blocks = json.load(blocks_file)["blocks"] blocks_file.close() water_image = pygame.image.load("img/water.png") st_water_image = water_image.copy() st_water_image.set_alpha(128) pygame.display.set_icon(water_image) #TODO: change the icon to something better bid = 0 global block_images block_images = {False:{}, True:{}} global ctm_block_images ctm_block_images = {False:{}, True:{}} for block in blocks: #set some default attributes if "breakable" not in block.keys(): block["breakable"] = True if "connectedTexture" not in block.keys(): block["connectedTexture"] = None if "solid" not in block.keys(): block["solid"] = True if "entity" not in block.keys(): block["entity"] = "" if "item" not in block.keys(): block["item"] = "ItemStack" if "description" not in block.keys(): block["description"] = [""] if "harvestlevel" not in block.keys(): block["harvestlevel"] = 0 if "breaktime" not in block.keys(): block["breaktime"] = 100 if "image" not in block.keys(): block["image"] = "" #add an id to the block block["id"] = bid global block_mappings block_mappings[block["name"]] = bid global id_mappings id_mappings[bid] = block["name"] if isinstance(block["description"], str): block["description"] = [block["description"]] #load the block image path = block["image"] if path != "": blockimg = pygame.image.load(path).convert_alpha() if block["connectedTexture"]: icon = pygame.Surface((Game.BLOCK_SIZE, Game.BLOCK_SIZE), pygame.SRCALPHA, 32) icon = icon.convert_alpha() icon.blit(blockimg, (0, 0)) else: icon = blockimg.copy() item_images[block["name"]] = [Images.make_itemdrop_image(icon)] foreground_image = Images.scale(blockimg, Game.SCALE) block_images[False][bid] = Images.crop(foreground_image) #blit the image onto the water tile so it isn't just empty transparency image = blockimg.copy() for x in range(image.get_width() // Game.BLOCK_SIZE): for y in range(image.get_height() // Game.BLOCK_SIZE): image.blit(water_image, (x * Game.BLOCK_SIZE, y * Game.BLOCK_SIZE)) image.blit(blockimg, (0, 0)) for x in range(image.get_width() // Game.BLOCK_SIZE): for y in range(image.get_height() // Game.BLOCK_SIZE): image.blit(st_water_image, (x * Game.BLOCK_SIZE, y * Game.BLOCK_SIZE)) background_image = Images.scale(image, Game.SCALE) block_images[True][bid] = background_image if block["connectedTexture"]: ctm_block_images[False][bid] = {} ctm_block_images[True][bid] = {} for x in range(4): for y in range(4): foreground_surf = pygame.Surface((Game.BLOCK_SIZE * Game.SCALE, Game.BLOCK_SIZE * Game.SCALE)).convert_alpha() foreground_surf.fill((0, 0, 0, 0)) background_surf = foreground_surf.copy() foreground_surf.blit(foreground_image, (0, 0), pygame.Rect((x * Game.BLOCK_SIZE * Game.SCALE, y * Game.BLOCK_SIZE * Game.SCALE), (Game.BLOCK_SIZE * Game.SCALE, Game.BLOCK_SIZE * Game.SCALE))) ctm_block_images[False][bid][(x, y)] = foreground_surf background_surf.blit(background_image, (0, 0), pygame.Rect((x * Game.BLOCK_SIZE * Game.SCALE, y * Game.BLOCK_SIZE * Game.SCALE), (Game.BLOCK_SIZE * Game.SCALE, Game.BLOCK_SIZE * Game.SCALE))) ctm_block_images[True][bid][(x, y)] = background_surf #make the corresponding item items[block["name"]] = {"displayName": block["displayName"], "image": block["image"], "class": block["item"], "description": block["description"], "can_place": True} bid += 1 block_images[False][get_block_id("water")] = pygame.Surface((Game.BLOCK_SIZE, Game.BLOCK_SIZE), pygame.SRCALPHA, 32)