def __init__(self, x, y, level): # Call the parent's constructor pygame.sprite.Sprite.__init__(self) # Set the image the player starts with # Set speed vector of player self.image = IMAGE_SLIDER.get_mario('dying') self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.level = level self.change_y = -PY_JUMP_Y_VELOCITY_1 * self.level.physics_info['seconds'] self.gravity = 0
def __init__(self, x, y, level): # Call the parent's constructor pygame.sprite.Sprite.__init__(self) # Set the image the player starts with # Set speed vector of player self.image = IMAGE_SLIDER.get_mario('dying') self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.level = level self.change_y = -PY_JUMP_Y_VELOCITY_1 * self.level.physics_info[ 'seconds'] self.gravity = 0
def __init__(self): """ Constructor function """ # Call the parent's constructor pygame.sprite.Sprite.__init__(self) # Set the image the player starts with # Set speed vector of player self.change_x = 0.0 self.speed = 0 self.gravity = 0 self.jump_physics = {'vel':0, 'antigravity': 0, 'gravity': 0} self.__anti_gravity = False self.__cutjump = False self.__change_y = 0.0 self.__max_vel = PY_MAX_MARIO_WALK_VEL self.__speed_acc = PY_MAX_WALK_ACC self.__running = False # This holds all the images for the animated walk left/right # of our player self.walking_frames_l = [] self.walking_frames_r = [] # What direction is the player facing? self.direction = PY_RIGHT # List of sprites we can bump against self.level = None self.state = MARIO_STATE_NORMAL for index in xrange(5): #print index mario_image = IMAGE_SLIDER.get_mario('small_walk_{}'.format(index)) self.walking_frames_r.append(mario_image) rotated = pygame.transform.flip(mario_image, True, False) self.walking_frames_l.append(rotated) self.image = self.walking_frames_r[0] # Set a referance to the image rect. self.rect = self.image.get_rect()
def __init__(self): """ Constructor function """ # Call the parent's constructor pygame.sprite.Sprite.__init__(self) # Set the image the player starts with # Set speed vector of player self.change_x = 0.0 self.speed = 0 self.gravity = 0 self.jump_physics = {'vel': 0, 'antigravity': 0, 'gravity': 0} self.__anti_gravity = False self.__cutjump = False self.__change_y = 0.0 self.__max_vel = PY_MAX_MARIO_WALK_VEL self.__speed_acc = PY_MAX_WALK_ACC self.__running = False # This holds all the images for the animated walk left/right # of our player self.walking_frames_l = [] self.walking_frames_r = [] # What direction is the player facing? self.direction = PY_RIGHT # List of sprites we can bump against self.level = None self.state = MARIO_STATE_NORMAL for index in xrange(5): #print index mario_image = IMAGE_SLIDER.get_mario('small_walk_{}'.format(index)) self.walking_frames_r.append(mario_image) rotated = pygame.transform.flip(mario_image, True, False) self.walking_frames_l.append(rotated) self.image = self.walking_frames_r[0] # Set a referance to the image rect. self.rect = self.image.get_rect()
def update(self): """ Move the player. """ # Gravity self.calc_grav() # Move left/right self.rect.x += self.change_x pos = self.rect.x + self.level.world_shift if self.state == MARIO_STATE_NORMAL: if self.change_x: frame = int(pos % 30 / 10) + 1 if self.direction == PY_RIGHT: self.image = self.walking_frames_r[frame] else: self.image = self.walking_frames_l[frame] else: if self.direction == PY_RIGHT: self.image = self.walking_frames_r[0] else: self.image = self.walking_frames_l[0] elif self.state == MARIO_STATE_JUMPING: self.image = IMAGE_SLIDER.get_mario("small_jumping") # See if we hit anything block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False) for block in block_hit_list: # If we are moving right, # set our right side to the left side of the item we hit if self.change_x > 0: self.rect.right = block.rect.left elif self.change_x < 0: # Otherwise if we are moving left, do the opposite. self.rect.left = block.rect.right #we don't walk to walk outside the map. if self.rect.x < 0: self.rect.x = 0 # Move up/down self.rect.y += self.change_y # Check and see if we hit anything block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False) for block in block_hit_list: # Reset our position based on the top/bottom of the object. if self.change_y > 0: self.rect.bottom = block.rect.top if self.state == MARIO_STATE_JUMPING: self.state = MARIO_STATE_NORMAL self.__cutjump = False elif self.change_y < 0: self.rect.top = block.rect.bottom self.__anti_gravity = False self.__cutjump = True if isinstance(block, Brick): block.start_bump() # Stop our vertical movement self.gravity = 0 self.change_y = 0 #let's kill some enemies. or be killed :P enemies_hit_list = pygame.sprite.spritecollide(self, self.level.enemy_list, False) for enemy in enemies_hit_list: if enemy.rect.collidepoint(self.rect.midbottom) or \ enemy.rect.collidepoint(self.rect.bottomright) or \ enemy.rect.collidepoint(self.rect.bottomleft) : #We kill it! self.change_y = -PY_ENEMY_STOMP_Y_SPEED * self.level.physics_info['seconds'] self.jump_physics['vel'] = PY_ENEMY_STOMP_Y_SPEED self.gravity = 0 self.state = MARIO_STATE_JUMPING enemy.jumped_on() else: if enemy.state != JUMPED_ON: self.kill() self.level.add_animation(DyingMario(self.rect.x, self.rect.y, self.level))
def update(self): """ Move the player. """ # Gravity self.calc_grav() # Move left/right self.rect.x += self.change_x pos = self.rect.x + self.level.world_shift if self.state == MARIO_STATE_NORMAL: if self.change_x: frame = int(pos % 30 / 10) + 1 if self.direction == PY_RIGHT: self.image = self.walking_frames_r[frame] else: self.image = self.walking_frames_l[frame] else: if self.direction == PY_RIGHT: self.image = self.walking_frames_r[0] else: self.image = self.walking_frames_l[0] elif self.state == MARIO_STATE_JUMPING: self.image = IMAGE_SLIDER.get_mario("small_jumping") # See if we hit anything block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False) for block in block_hit_list: # If we are moving right, # set our right side to the left side of the item we hit if self.change_x > 0: self.rect.right = block.rect.left elif self.change_x < 0: # Otherwise if we are moving left, do the opposite. self.rect.left = block.rect.right #we don't walk to walk outside the map. if self.rect.x < 0: self.rect.x = 0 # Move up/down self.rect.y += self.change_y # Check and see if we hit anything block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False) for block in block_hit_list: # Reset our position based on the top/bottom of the object. if self.change_y > 0: self.rect.bottom = block.rect.top if self.state == MARIO_STATE_JUMPING: self.state = MARIO_STATE_NORMAL self.__cutjump = False elif self.change_y < 0: self.rect.top = block.rect.bottom self.__anti_gravity = False self.__cutjump = True if isinstance(block, Brick): block.start_bump() # Stop our vertical movement self.gravity = 0 self.change_y = 0 #let's kill some enemies. or be killed :P enemies_hit_list = pygame.sprite.spritecollide(self, self.level.enemy_list, False) for enemy in enemies_hit_list: if enemy.rect.collidepoint(self.rect.midbottom) or \ enemy.rect.collidepoint(self.rect.bottomright) or \ enemy.rect.collidepoint(self.rect.bottomleft) : #We kill it! self.change_y = -PY_ENEMY_STOMP_Y_SPEED * self.level.physics_info[ 'seconds'] self.jump_physics['vel'] = PY_ENEMY_STOMP_Y_SPEED self.gravity = 0 self.state = MARIO_STATE_JUMPING enemy.jumped_on() else: if enemy.state != JUMPED_ON: self.kill() self.level.add_animation( DyingMario(self.rect.x, self.rect.y, self.level))