def __init__(self, rectangle): MySprite.__init__(self) # The platforms will be rectangles self.rect = rectangle # The position will be set as the size of the platform self.setPosition((self.rect.left, self.rect.bottom)) # The platforms are invisible self.image = pygame.Surface((0, 0))
def __init__(self, rectangle): MySprite.__init__(self) # The flag will be a rectangle self.rect = rectangle # The position will be set as the size of the Flag self.setPosition((self.rect.left, self.rect.bottom)) # The flag area is invisible self.image = pygame.Surface((0, 0))
def __init__(self, imageFile, coordFile, nImages, runSpeed, jumpSpeed, animDelay): # First we call parent's constructor MySprite.__init__(self) # Loading the spritesheet. self.sheet = ResourcesManager.loadImage(imageFile, -1) self.sheet = self.sheet.convert_alpha() # Starting movement self.movement = STILL self.looking = RIGHT # Reading the coordinates data = ResourcesManager.loadFileCoordinates(coordFile) data = data.split() self.numStance = 1 self.numImageStance = 0 counter = 0 self.sheetCoords = [] for line in range(0, 3): self.sheetCoords.append([]) tmp = self.sheetCoords[line] for stance in range(1, nImages[line] + 1): tmp.append( pygame.Rect( (int(data[counter]), int(data[counter + 1])), (int(data[counter + 2]), int(data[counter + 3])))) counter += 4 self.rect = pygame.Rect( 100, 100, self.sheetCoords[self.numStance][self.numImageStance][2], self.sheetCoords[self.numStance][self.numImageStance][3]) # Default running and jumping speed self.runSpeed = runSpeed self.jumpSpeed = jumpSpeed # Default stance is standing self.numStance = STILL self.animationDelay = animDelay # Constant to reset sprite change self.movementDelay = 0 # Counter to delay sprite change self.jumpTime = PLAYER_BASE_JUMP # Time you can keep jumping to increase height self.attackTime = 0 # If this is larger than 0 character has to wait to attack self.attacking = False # This indicates if character should attack on current frame self.dead = False # This indicates if character should die on current frame self.stunDelay = 0 # This is to reset the stun counter self.stunnedTime = 0 # If this is larger than 0 character is hitstunned and cannot move self.invulTime = 0 # If this is larger than 0 character is invulnerable and cannot be hit # For most enemies these two are the same self.updateStance()
def __init__(self, imageFile, coordFile, nImages, moveSpeed, animDelay, looking): # First we call parent's constructor MySprite.__init__(self) # Loading the spritesheet. self.sheet = ResourcesManager.loadImage(imageFile, -1) self.sheet = self.sheet.convert_alpha() # Starting movement self.movement = looking self.looking = looking # Reading the coordinates data = ResourcesManager.loadFileCoordinates(coordFile) data = data.split() self.numStance = 1 self.numImageStance = 0 counter = 0 self.sheetCoords = [] line = 0 self.sheetCoords.append([]) tmp = self.sheetCoords[line] for stance in range(1, nImages[line] + 1): tmp.append( pygame.Rect((int(data[counter]), int(data[counter + 1])), (int(data[counter + 2]), int(data[counter + 3])))) counter += 4 # Delay when changing sprite image self.movementDelay = 0 self.numStance = STILL self.moveSpeed = moveSpeed self.animationDelay = animDelay self.rect = pygame.Rect( 100, 100, self.sheetCoords[self.numStance][self.numImageStance][2], self.sheetCoords[self.numStance][self.numImageStance][3]) self.updateStance()
def update(self, spriteStructure, time): self.updateStance() # The imp gets stunned if it hits the player, preventing it from following inside of him. self.scroll = spriteStructure.player.scroll if (self.stunnedTime <= 0): self.speed = self.direction if self.attackTime > 0: self.attackTime -= time if self.hitPlayer is not None and (self.attackTime <= 0): if (self.hitPlayer.looking == LEFT): self.hitPlayer.stun(self.knockback, self.damage) else: self.hitPlayer.stun( (-self.knockback[0], self.knockback[1]), self.damage) self.attackTime = IMP_ATTACK_DELAY self.stunnedTime = IMP_STUN_DELAY self.hitPlayer = None else: self.stunnedTime -= time MySprite.update(self, time) if self.dead: self.onDeath(spriteStructure, time)
def update(self, spriteStructure, time): self.updateStance() MySprite.update(self, time) return
def update(self, spriteStructure, time): # Separate speed into components for code legibility # these are local and are set to the character at the end of the procedure (speedx, speedy) = self.speed platforms = pygame.sprite.spritecollide(self, spriteStructure.platformGroup, False) # First we check if for any reason the character has been killed or fallen oob. if self.dead or (self.position[1] > KILLING_FLOOR): self.onDeath(spriteStructure, time) return # Next we update its invulnerability time if self.invulTime >= 0: self.invulTime -= time # Then we check its stun time and update position according to inertia. # Currently there is no friction preventing them from sliding along the x axis if self.movement == STUNNED: self.stunnedTime -= time if (speedx > 0): if self.checkWall(RIGHT, platforms): speedx = 0 elif (speedx < 0): if self.checkWall(LEFT, platforms): speedx = 0 # If moving left or right elif (self.movement == LEFT) or (self.movement == RIGHT): # Set direction the character is facing self.looking = self.movement # Set movement speeds if we are not running into a wall if self.movement == LEFT: if self.checkWall(LEFT, platforms): speedx = 0 else: speedx = -self.runSpeed else: if self.checkWall(RIGHT, platforms): speedx = 0 else: speedx = self.runSpeed if self.numStance != SPRITE_JUMP: # If player is standing on solid ground, reset jump timer self.jumpTime = PLAYER_BASE_JUMP self.numStance = SPRITE_WALK # If we've run out of solid ground (walking out of a platform), start falling if not platforms: self.numStance = SPRITE_JUMP elif (self.movement == UP) or (self.movement == UPLEFT) or (self.movement == UPRIGHT): # If player is jumping, decrease time to keep jumping and set vertical speed accordingly if self.numStance != SPRITE_JUMP: self.jumpTime = PLAYER_BASE_JUMP self.jumpTime -= time self.numStance = SPRITE_JUMP # Check if we bump into a ceiling if self.checkCeiling(platforms): speedy = 0 else: speedy = -self.jumpSpeed # These allow diagonal jumps and check for walls too if (self.movement == UPLEFT): if self.checkWall(LEFT, platforms): speedx = 0 else: speedx = -self.runSpeed elif (self.movement == UPRIGHT): if self.checkWall(RIGHT, platforms): speedx = 0 else: speedx = self.runSpeed # If not doing anything, stand still and reset jump timer elif self.movement == STILL: self.jumpTime = PLAYER_BASE_JUMP if not self.numStance == SPRITE_JUMP: self.numStance = SPRITE_STILL speedx = 0 # If on the air, we have to check if we are landing on a platform if self.numStance == SPRITE_JUMP: for platform in iter(platforms): if (speedy > 0) and (platform.rect.top < self.rect.bottom) \ and ((self.rect.bottom - self.rect.height/2) < platform.rect.top): # Set y value to top of the platform and break fall self.setPosition( (self.position[0], platform.position[1] - platform.rect.height + 1)) self.numStance = SPRITE_STILL speedy = 0 # Otherwise, keep falling accelerated by gravity if self.numStance != SPRITE_STILL: speedy += GRAVITY * time self.updateStance() self.speed = (speedx, speedy) MySprite.update(self, time) return
def __init__(self, pos_x, pos_y, image1, image2, image3): #Initialise MyHero avec une position (x, y), deux images et l'objet lui-meme. MySprite.__init__(self, pos_x, pos_y, image1, image2, image3) self.rect = pygame.Rect(pos_x, pos_y, 33, 80)
def __init__( self, pos_x, pos_y, image1, image2, image3 ): #Initialise MyHero avec une position (x, y), deux images et l'objet lui-meme. MySprite.__init__(self, pos_x, pos_y, image1, image2, image3) self.rect = pygame.Rect(pos_x, pos_y, 33, 80)
def __init__(self, pos_x, pos_y, image1, image2, image3, speed): MySprite.__init__(self, pos_x, pos_y, image1, image2, image3) self.rect = pygame.Rect(pos_x, pos_y, 28, 30) self.speed = speed
def __init__(self, pos_x, pos_y, image1, image2): MySprite.__init__(self, pos_x, pos_y, image1, image2)
def __init__(self, pos_x, pos_y, image1, image2, speed): MySprite.__init__(self, pos_x, pos_y, image1, image2) self.speed = speed