def set_size(self, new_width, new_height): # Set the actual width and height to the new width and height. self.actual_width = new_width self.actual_height = new_height # Temporarily store the old width and height, used for positioning the paddle later. old_width = self.rect.width old_height = self.rect.height # Change the rect width. if self.actual_width > self.max_width: self.rect.width = self.max_width elif self.actual_width < self.min_width: self.rect.width = self.min_width else: self.rect.width = self.actual_width # Change the rect height. if self.actual_height > self.max_height: self.rect.height = self.max_height elif self.actual_height < self.min_height: self.rect.height = self.min_height else: self.rect.height = self.actual_height # Make sure the position of the paddle isn't changed. self.x += (old_width - self.rect.width) / 2.0 self.y += (old_height - self.rect.height) / 2.0 if hasattr(self, 'image'): # Resize the current image. self.image = pygame.transform.scale(self.image, (self.rect.width, self.rect.height)) else: # Create the image attribute that is drawn to the surface. self.image = pygame.Surface((self.rect.width, self.rect.height), pygame.locals.SRCALPHA) # Blit the top part. self.image.blit(Paddle.top_image, (0, 0)) # Blit the middle parts. for i in range(Paddle.top_image.get_height(), self.rect.height - Paddle.bottom_image.get_height()): self.image.blit(Paddle.middle_image, (0, i)) # Blit the bottom part. self.image.blit(Paddle.bottom_image, (0, self.rect.height - Paddle.bottom_image.get_height())) useful.colorize_image(self.image, copy.copy(self.owner.color), False, False) # If the shadow already exists, kill it first. if hasattr(self, 'shadow'): self.shadow.kill() # Then, create a (new) shadow. self.shadow = shadow.Shadow(self)
def __init__(self, parent, duration): # We start by calling the superconstructor. effect.Effect.__init__(self, parent, duration) # Create the image attribute that is drawn to the surface. We only do this if the parent is a ball, however. if self.parent.__class__ == ball.Ball: self.image = Timeout.image.copy() useful.colorize_image(self.image, self.parent.color) # Set the rects width and height to the standard values. self.rect.width = Timeout.width self.rect.height = Timeout.height
def __init__(self, parent, color = pygame.Color(0, 0, 0, 128), linger = False, fill = False): # We start by calling the superconstructor. pygame.sprite.Sprite.__init__(self) # Keep track of the parent, used to position the shadow. self.parent = parent # Create the rect used for drawing the shadow. self.rect = pygame.rect.Rect(self.parent.rect.x, self.parent.rect.y, self.parent.rect.width, self.parent.rect.height) # Store the offset, the distance from the parent the shadow is drawn from. self.offset_x = Shadow.offset_x self.offset_y = Shadow.offset_y # Store the variables used when timing out the shadow. If set to linger, the shadow eventually fades away. self.linger = linger self.linger_time_left = Shadow.linger_time self.alpha_step = Shadow.alpha_step # Store whether or not to use image resource or fill blitting. self.fill = fill # Store the color. self.color = color # Copy the parents image, and then colorize it to the shadow color. if not self.fill: self.image = self.parent.image.copy() # Colorize the image. If the image doesn't have any alpha values, don't blend alphas. If it does, do blend alphas. if self.image.get_alpha() == None: useful.colorize_image(self.image, self.color) else: useful.colorize_image(self.image, self.color, True) # Convert to alpha and apply the alpha value. self.image.convert_alpha() self.image.set_alpha(self.color.a) # Store a copy of this image as the original image. Used when rotating the shadow. self.original_image = self.image.copy() else: # If using fill instead of image, create a new surface to handle alpha. self.surface = pygame.Surface((self.rect.width, self.rect.height), SRCALPHA) # Add self to the main shadow_group. groups.Groups.shadow_group.add(self)
def __init__(self, x, y, owner): # We start by calling the superconstructor. block.Block.__init__(self, owner, x, y, NormalBlock.width, NormalBlock.height, NormalBlock.health) # Create the image attribute that is drawn to the surface. self.image = NormalBlock.image.copy() # Colorize the block. self.color = self.owner.color useful.colorize_image(self.image, self.color) # Create the image that is drawn when health is half or less. self.half_health_image = NormalBlock.half_health_image.copy() # Colorize that image. self.half_health_color = useful.blend_colors(self.owner.color, block.Block.half_health_blend_color) useful.colorize_image(self.half_health_image, self.half_health_color) # Create a shadow. self.shadow = shadow.Shadow(self)
def __init__(self, x, y, owner): # We start by calling the superconstructor. block.Block.__init__(self, owner, x, y, StrongBlock.width, StrongBlock.height, StrongBlock.health) # Create the image attribute that is drawn to the surface. self.image = StrongBlock.image.copy() # Colorize the block. self.color = self.owner.color useful.colorize_image(self.image, self.color) # Create the image that is drawn when health is half or less. self.half_health_image = StrongBlock.half_health_image.copy() # Colorize that image. self.half_health_color = useful.blend_colors( self.owner.color, block.Block.half_health_blend_color) useful.colorize_image(self.half_health_image, self.half_health_color) # Create a shadow. self.shadow = shadow.Shadow(self)
def __init__(self, x, y, angle, owner): # We start by calling the superconstructor. pygame.sprite.Sprite.__init__(self) # Create the rect used for collision detection, position etc. self.rect = pygame.rect.Rect(x, y, Ball.width, Ball.height) # Keep track of x and y as floats, for preciseness sake (rect keeps track of x,y as ints) self.x = x self.y = y # Keep track of the balls position in the previous frame, used for collision handling. self.previous = pygame.rect.Rect(self.x, self.y, Ball.width, Ball.height) # Set the angle variable. self.angle = angle # Set maximum speed of the ball. self.max_speed = Ball.max_speed # Set the speed variable. self.speed = Ball.speed self.tick_speed = self.speed # Store the current level of smash stack. self.smash_stack = 0 # Store the owner. self.owner = owner # Create one image attribute per player. self.player_images = {} for player in groups.Groups.player_group: player_image = Ball.image.copy() # Colorize the image to the color of the player. useful.colorize_image(player_image, player.color) # Finally, add this image to the list of player images. self.player_images[player] = player_image # Create the image attribute that is drawn to the surface. self.image = self.player_images[self.owner] # We save a reference to the parents color in our own variable, so that classes and modules # that want to use our color do not have to call us.owner.color. self.color = self.owner.color # If collided is True, the ball sound is played. self.collided = False # Setup the trace time keeping variable. self.trace_spawn_time = 0 # Create a shadow. self.shadow = shadow.Shadow(self) # Store the ball in the owners ball_group and the main ball_group. self.owner.ball_group.add(self) groups.Groups.ball_group.add(self) # Create an effect group to handle effects on this ball. self.effect_group = pygame.sprite.Group()
def setup_info(self, file_path): texts = [] # Parse the JSON file. json_file = open(file_path, "r") try: parsed_json = json.load(json_file) except IOError: print("IOError when reading JSON file.") finally: json_file.close() # We try to parse the image tag in the JSON file. If it isn't found, an error is raised. if "image" in parsed_json: try: image_item = imageitem.ImageItem(parsed_json["image"]) except: raise ValueError("The image path could not be read.") else: raise SyntaxError("Image key not found in JSON file.") # We try to parse the color tag in the JSON file. If it isn't found, we simply ignore it. if "color" in parsed_json: try: color_list = parsed_json["color"] except: raise ValueError("The color value could not be read.") try: useful.colorize_image( image_item.image, pygame.Color(color_list[0], color_list[1], color_list[2])) except: raise ValueError( "The given color values cannot be applied: [{0}, {1}, {2}]" .format(color_list[0], color_list[1], color_list[2])) # We add the image item to the help_menu here, so that the text can be be positioned properly. self.help_menu.add(image_item, self.view_info) # We try to parse the title tag in the JSON file. If it isn't found, an error is raised. if "title" in parsed_json: text = textitem.TextItem(parsed_json["title"], pygame.Color(255, 255, 255), 255, self.font_size) text.x = (settings.SCREEN_WIDTH - text.get_width()) / 2 text.y = self.help_menu.y + self.help_menu.get_height( ) + text.get_height() texts.append(text) previous_text = text else: raise SyntaxError("Title key not found in JSON file.") if "body" in parsed_json: body = parsed_json["body"] odd = True first_line = True wrapped_body = useful.wrap_multi_line( body, pygame.font.Font(textitem.TextItem.font_path, self.font_size), self.max_width_of_text_line) for line in wrapped_body: if odd: color = pygame.Color(255, 255, 255) else: color = pygame.Color(150, 150, 150) text = textitem.TextItem(line, color, 255, self.font_size) if first_line: text.x = self.distance_from_screen_edge text.y = previous_text.y + (2 * text.get_height()) first_line = False else: text.x = self.distance_from_screen_edge text.y = previous_text.y + text.get_height() texts.append(text) previous_text = text if line != "": odd = not odd else: raise SyntaxError("Body tag not found in JSON file.") if "quote" in parsed_json: quote = parsed_json["quote"] odd = True quotes = [] font = pygame.font.Font(textitem.TextItem.font_path, self.font_size) wrapped_quote = useful.wrap_multi_line(quote, font, self.max_width_of_text_line) for line in wrapped_quote: if odd: color = pygame.Color(200, 0, 200) else: color = pygame.Color(200, 50, 200) text = textitem.TextItem(line, color, 255, self.font_size) text.x = settings.SCREEN_WIDTH - text.get_width( ) - self.distance_from_screen_edge quotes.append(text) texts.append(text) previous_text = text if line != "": odd = not odd for quote_line in quotes: quote_line.y = ( self.back_menu.y - (2 * quote_line.get_height()) - len(quotes) * quote_line.get_height() ) + quotes.index(quote_line) * quote_line.get_height() self.info[image_item] = texts return image_item
def setup_info(self, file_path): texts = [] # Parse the JSON file. json_file = open(file_path, "r") try: parsed_json = json.load(json_file) except IOError: print("IOError when reading JSON file.") finally: json_file.close() # We try to parse the image tag in the JSON file. If it isn't found, an error is raised. if "image" in parsed_json: try: image_item = imageitem.ImageItem(parsed_json["image"]) except: raise ValueError("The image path could not be read.") else: raise SyntaxError("Image key not found in JSON file.") # We try to parse the color tag in the JSON file. If it isn't found, we simply ignore it. if "color" in parsed_json: try: color_list = parsed_json["color"] except: raise ValueError("The color value could not be read.") try: useful.colorize_image(image_item.image, pygame.Color(color_list[0], color_list[1], color_list[2])) except: raise ValueError("The given color values cannot be applied: [{0}, {1}, {2}]".format(color_list[0], color_list[1], color_list[2])) # We add the image item to the help_menu here, so that the text can be be positioned properly. self.help_menu.add(image_item, self.view_info) # We try to parse the title tag in the JSON file. If it isn't found, an error is raised. if "title" in parsed_json: text = textitem.TextItem(parsed_json["title"], pygame.Color(255, 255, 255), 255, self.font_size) text.x = (settings.SCREEN_WIDTH - text.get_width()) / 2 text.y = self.help_menu.y + self.help_menu.get_height() + text.get_height() texts.append(text) previous_text = text else: raise SyntaxError("Title key not found in JSON file.") if "body" in parsed_json: body = parsed_json["body"] odd = True first_line = True wrapped_body = useful.wrap_multi_line(body, pygame.font.Font(textitem.TextItem.font_path, self.font_size), self.max_width_of_text_line) for line in wrapped_body: if odd: color = pygame.Color(255, 255, 255) else: color = pygame.Color(150, 150, 150) text = textitem.TextItem(line, color, 255, self.font_size) if first_line: text.x = self.distance_from_screen_edge text.y = previous_text.y + (2 * text.get_height()) first_line = False else: text.x = self.distance_from_screen_edge text.y = previous_text.y + text.get_height() texts.append(text) previous_text = text if line != "": odd = not odd else: raise SyntaxError("Body tag not found in JSON file.") if "quote" in parsed_json: quote = parsed_json["quote"] odd = True quotes = [] font = pygame.font.Font(textitem.TextItem.font_path, self.font_size) wrapped_quote = useful.wrap_multi_line(quote, font, self.max_width_of_text_line) for line in wrapped_quote: if odd: color = pygame.Color(200, 0, 200) else: color = pygame.Color(200, 50, 200) text = textitem.TextItem(line, color, 255, self.font_size) text.x = settings.SCREEN_WIDTH - text.get_width() - self.distance_from_screen_edge quotes.append(text) texts.append(text) previous_text = text if line != "": odd = not odd for quote_line in quotes: quote_line.y = (self.back_menu.y - (2 * quote_line.get_height()) - len(quotes) * quote_line.get_height()) + quotes.index(quote_line) * quote_line.get_height() self.info[image_item] = texts return image_item