def __init__(self, move_type, img, *args): """ Initializes the Enemy instance. Arguments: move_type = Movement type of the Enemy. See Enemy.move img = pygame.Surface of the Enemy bounds = pygame.Rect of the bounds to move in game_speed = Speed of the street """ super(Enemy, self).__init__(*args) self.move_type = move_type self.img = img self.rect = img.get_rect() self.hitmask = pixelperfect.get_alpha_hitmask(self.img, self.rect) if move_type < 2: x = self.bounds.right y = randint(self.bounds.top, self.bounds.bottom) elif move_type < 4: x = randint(self.bounds.right / 2, self.bounds.right) if move_type == 2: y = self.bounds.bottom else: y = self.bounds.top else: print "MOVE_TYPE > 3" self.rect.move_ip(x, y)
def parser_basic_info(self, parse): ''' @brief Método que parsea la información basica de un objeto del juego. @param parse Parse a Archivo xml con xml.dom.minidom ''' parent_node = parse.firstChild sprite_name = str(parent_node.getAttribute('sprite_code')) self.original_sprite = resource.get_new_sprite(sprite_name) #Cargamos las distintas animaciones del objeto for element in parse.getElementsByTagName('animation'): animation_name = str(element.getAttribute('name')) animation_frames = str(element.getAttribute('frames')) animation_delay = int(element.getAttribute('delay')) #Vemos que tipo de animación es y lo añadimos al mapa de imagenes if animation_name == 'normal': self.animations[NORMAL] = animation.Animation( animation_frames, animation_delay) elif animation_name == 'noaction': self.animations[NOACTION] = animation.Animation( animation_frames, animation_delay) elif animation_name == 'run': self.animations[RUN] = animation.Animation( animation_frames, animation_delay) elif animation_name == 'forward': self.animations[FORWARD] = animation.Animation( animation_frames, animation_delay) elif animation_name == 'reverse': self.animations[REVERSE] = animation.Animation( animation_frames, animation_delay) elif animation_name == 'damaged': self.animations[DAMAGED] = animation.Animation( animation_frames, animation_delay) elif animation_name == 'erase': self.animations[ERASE] = animation.Animation( animation_frames, animation_delay) elif animation_name == 'yaw': self.animations[YAW] = animation.Animation( animation_frames, animation_delay) elif animation_name == 'fall': self.animations[FALL] = animation.Animation( animation_frames, animation_delay) elif animation_name == 'turbo': self.animations[TURBO] = animation.Animation( animation_frames, animation_delay) #Inicializamos la imagen, el rectangulo y la mascara de pixeles self.image = self.original_sprite.get_frame( self.animations[NORMAL].get_frame()) self.rect = self.image.get_rect() self.mask = pygame.mask.from_surface(self.image) self.hitmask = pixelperfect.get_alpha_hitmask(self.image, self.rect)
def __init__(self, img, *args): """ Initializes the Player instance. Arguments: img = pygame.Surface of the Player bounds = pygame.Rect of the bounds to move in game_speed = Speed of the street """ super(Player, self).__init__(*args) self.img = img self.rect = self.img.get_rect() self.hitmask = pixelperfect.get_alpha_hitmask(self.img, self.rect) self.speed = self.bounds.height / 40
def parser_basic_info(self, parse): ''' @brief Método que parsea la información basica de un objeto del juego. @param parse Parse a Archivo xml con xml.dom.minidom ''' parent_node = parse.firstChild sprite_name = str(parent_node.getAttribute('sprite_code')) self.original_sprite = resource.get_new_sprite(sprite_name) #Cargamos las distintas animaciones del objeto for element in parse.getElementsByTagName('animation'): animation_name = str(element.getAttribute('name')) animation_frames = str(element.getAttribute('frames')) animation_delay = int(element.getAttribute('delay')) #Vemos que tipo de animación es y lo añadimos al mapa de imagenes if animation_name == 'normal': self.animations[NORMAL] = animation.Animation(animation_frames, animation_delay) elif animation_name == 'noaction': self.animations[NOACTION] = animation.Animation(animation_frames, animation_delay) elif animation_name == 'run': self.animations[RUN] = animation.Animation(animation_frames, animation_delay) elif animation_name == 'forward': self.animations[FORWARD] = animation.Animation(animation_frames, animation_delay) elif animation_name == 'reverse': self.animations[REVERSE] = animation.Animation(animation_frames, animation_delay) elif animation_name == 'damaged': self.animations[DAMAGED] = animation.Animation(animation_frames, animation_delay) elif animation_name == 'erase': self.animations[ERASE] = animation.Animation(animation_frames, animation_delay) elif animation_name == 'yaw': self.animations[YAW] = animation.Animation(animation_frames, animation_delay) elif animation_name == 'fall': self.animations[FALL] = animation.Animation(animation_frames, animation_delay) elif animation_name == 'turbo': self.animations[TURBO] = animation.Animation(animation_frames, animation_delay) #Inicializamos la imagen, el rectangulo y la mascara de pixeles self.image = self.original_sprite.get_frame(self.animations[NORMAL].get_frame()) self.rect = self.image.get_rect() self.mask = pygame.mask.from_surface(self.image) self.hitmask = pixelperfect.get_alpha_hitmask(self.image, self.rect)
def __init__(self, xpos, ypos, image, boxfit=False): self.boxfit = boxfit self.image = pygame.image.load(curdir + "\\sprites\\" + image) self.width, self.height = self.image.get_size() self.rect = self.image.get_rect() self.rect.x = xpos self.rect.y = ypos self.x = xpos self.y = ypos self.renderx = xpos + scrwidth / 2 - self.width / 2 self.rendery = scrheight / 2 - ypos + self.height / 2 self.hitmask = pixelperfect.get_alpha_hitmask(self.image, self.rect) self.dict = {}
def update_image(self): ''' @bnief Actualiza la imagen, según el estado actual de la animación y el angulo del objeto ''' #Rotamos la imagen actual de la animación self.image = pygame.transform.rotate(self.original_sprite.get_frame(self.animations[self.state].get_frame()), -self.actual_angle) #Actualizamos tanto el alto como el ancho self.rect.w = self.image.get_width() self.rect.h = self.image.get_height() #Obtenemos la nueva mascara de pixeles self.mask = pygame.mask.from_surface(self.image) self.hitmask = pixelperfect.get_alpha_hitmask(self.image, self.rect)
def update_image(self): ''' @bnief Actualiza la imagen, según el estado actual de la animación y el angulo del objeto ''' #Rotamos la imagen actual de la animación self.image = pygame.transform.rotate( self.original_sprite.get_frame( self.animations[self.state].get_frame()), -self.actual_angle) #Actualizamos tanto el alto como el ancho self.rect.w = self.image.get_width() self.rect.h = self.image.get_height() #Obtenemos la nueva mascara de pixeles self.mask = pygame.mask.from_surface(self.image) self.hitmask = pixelperfect.get_alpha_hitmask(self.image, self.rect)
class BaseEntity(pygame.sprite.Sprite): """ Base Entity Class. All Renderables should derive from this class. """ position = (0, 0) # Position in pixels velocity = (0, 0) # Velocity in pixels/s accel = (0, 0) # Acceleration in pixels/s^2 theta = 0 # Angle in degrees w.r.t. 0 degrees omega = 0 # Angular velocity in degrees/s alpha = 0 # Angular acceleration in degrees/s^2 hitrect = None image = None blit_image = None hitmask = None def __init__(self): pygame.sprite.Sprite.__init__(self) def render(self, screen): """ Render sprite to the screen. If own image is null, it errors. :param screen: pygame surface to blit to :return: void """ if self.image is not None: screen.blit(self.blit_image, self.hitrect) else: raise Exception("Can't render with a null image") def setImage(self, newImage): """ Sets own image to a surface, then uses PixelPerfect to calc hitmasks :param newImage: :return: void """ try: self.image = pygame.image.load(newImage) except pygame.error, message: print "Couldn't load image:{0}, reverting to fallback".format(newImage) print message self.image = pygame.image.load("triangle.png") self.image = self.image.convert_alpha() self.blit_image = self.image self.hitrect = self.image.get_rect() self.hitrect.center = self.position self.hitmask = pixelperfect.get_alpha_hitmask(self.image, self.hitrect)