class PiranhaPlant(Sprite): def __init__(self, screen, pop_list): super(PiranhaPlant, self).__init__() # get screen dimensions self.screen = screen self.screen_rect = self.screen.get_rect() # list to hold animation images self.pop_list = pop_list # Timer class to animate sprites self.animation = Timer(frames=self.pop_list) # get the rect of the image self.imagePop = self.animation.imagerect() self.rect = self.imagePop.get_rect() self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom # store objects exact position self.x = float(self.rect.centerx) self.y = float(self.rect.centery) # movement flags self.going_up = False self.going_down = False self.direction = 1 def blitme(self): if self.going_up or self.going_down: self.screen.blit(self.imagePop, self.rect) def update(self): self.y += (1 * self.direction) # (2 * self.direction) self.rect.y = self.y if self.rect.top >= self.screen_rect.centery + 250: self.direction *= -1 self.going_up = False self.going_down = True if self.rect.bottom <= self.screen_rect.centery - 250: self.direction *= -1 self.going_up = True self.going_down = False if self.going_down or self.going_up: self.imagePop = self.pop_list[self.animation.frame_index()]
class Mario(Sprite): def __init__(self, screen, walk_right, walk_left): super().__init__() self.screen = screen self.screen_rect = self.screen.get_rect() self.walk_right = walk_right self.walk_left = walk_left self.mario_right = Timer(frames=self.walk_right) self.mario_left = Timer(frames=self.walk_left) # get the rect of the image self.right_image = self.mario_right.imagerect() self.left_image = self.mario_left.imagerect() self.rect = self.right_image.get_rect() #self.rect.x = self.screen_rect.x + 150 #self.rect.bottom = self.screen_rect.bottom self.rect.x = 100 self.rect.y = 300 # store objects exact position self.center = float(self.rect.centerx) # flags for movement and abilities self.jump_speed = 0 self.moving_right = False self.moving_left = False self.facing_right = True self.facing_left = False self.is_jumping = False self.break_brick = False self.invincible = False self.crouching = False #ken implementataion self.floor = False self.brick = False self.obstacleL = False self.obstacleR = False def blitme(self): if self.facing_right: self.screen.blit(self.right_image, self.rect) elif self.facing_left: self.screen.blit(self.left_image, self.rect) def update(self): self.gravity() # animations for moving right and left: excludes last image in list (jump image) if self.moving_right and not self.is_jumping and self.rect.x <= 480: if not self.obstacleR: self.center += 5 if self.mario_right.frame_index() < self.mario_right.lastframe - 1: self.right_image = self.walk_right[ self.mario_right.frame_index()] if self.obstacleL: self.obstacleL = False elif self.moving_left and not self.is_jumping and self.rect.x <= 480: if not self.obstacleL: self.center -= 5 if self.mario_left.frame_index() < self.mario_left.lastframe - 1: self.left_image = self.walk_left[self.mario_left.frame_index()] if self.obstacleR: self.obstacleR = False elif self.moving_right and not self.is_jumping and self.rect.x >= 480: if self.mario_right.frame_index() < self.mario_right.lastframe - 1: self.right_image = self.walk_right[ self.mario_right.frame_index()] if self.obstacleL: self.obstacleL = False elif self.moving_left and not self.is_jumping and self.rect.x >= 480: if not self.obstacleL: self.center -= 5 if self.mario_left.frame_index() < self.mario_left.lastframe - 1: self.left_image = self.walk_left[self.mario_left.frame_index()] if self.obstacleR: self.obstacleR = False # if not moving, display standing frame elif not self.moving_right and self.facing_right and not self.is_jumping: self.right_image = self.walk_right[0] elif not self.moving_left and self.facing_left and not self.is_jumping: self.left_image = self.walk_left[0] # animation to handle "jump" case elif self.is_jumping and self.facing_right and self.rect.x <= 480: if not self.obstacleR and self.moving_right: self.center += 5 self.right_image = self.walk_right[-2] elif self.is_jumping and self.facing_left and self.rect.x <= 480: if not self.obstacleL and self.moving_left: self.center -= 5 self.left_image = self.walk_left[-2] # animation to handle "jump" case when camera moving elif self.is_jumping and self.facing_right and self.rect.x >= 480: self.right_image = self.walk_right[-2] elif self.is_jumping and self.facing_left and self.rect.x >= 480: if not self.obstacleL and self.moving_left: self.center -= 5 self.left_image = self.walk_left[-2] self.rect.y += self.jump_speed #forced gravity by ken if not self.is_jumping: self.rect.y += 5 # display crouching image if self.crouching and self.break_brick: self.crouch() # Update the rect object from self.center self.rect.centerx = self.center def crouch(self): """ # store objects exact position self.x = float(self.rect.centerx)""" # get crouch image rect and set bottom to be bottom of screen if self.facing_right: image = self.walk_right[-1] self.rect = image.get_rect() self.rect.bottom = self.screen_rect.bottom self.right_image = image elif self.facing_left: image = self.walk_left[-1] self.rect = image.get_rect() self.rect.bottom = self.screen_rect.bottom self.left_image = image def jump(self): """ Function to handle when mario jumps """ if self.floor: self.jump_speed = -11 self.floor = False def gravity(self): """ Function to calculate and handle gravity """ if self.jump_speed == 0: self.jump_speed = 50 else: self.jump_speed += .45 # mario has landed on surface if self.floor and self.jump_speed >= 0: self.jump_speed = 0 self.is_jumping = False def become_big(self): """ Function to make Mario Big if interacted with mushroom """ big_mario = SuperMario(screen=self.screen) self.break_brick = True # set the frames to that of big mario self.walk_right = big_mario.walk_right self.walk_left = big_mario.walk_left # get the rect of the new image self.image = self.walk_right[0] self.rect = self.image.get_rect() # place in same spot as powerup self.rect.x = self.center self.rect.bottom = self.screen_rect.bottom def become_small(self): """ Function to make Mario small if interact with enemy""" little_mario = LittleMario(screen=self.screen) self.break_brick = False # set the frames to that of little mario self.walk_right = little_mario.walk_right self.walk_left = little_mario.walk_left # get the rect of the new image self.image = self.walk_right[0] self.rect = self.image.get_rect() # place in same spot as powerup self.rect.x = self.center self.rect.bottom = self.screen_rect.bottom self.invincible = True def death_animation(self): """ Function to run mario death animation """ print("rip mario") def reset_level(self): """ Function to reset Mario's position upon new level""" # time.sleep(1.0) self.rect.x = self.screen_rect.x + 150 self.rect.bottom = self.screen_rect.bottom self.center = float(self.rect.centerx)
class Enemy(Sprite): def __init__(self, screen, walk_list): super(Enemy, self).__init__() # get screen dimensions self.screen = screen self.screen_rect = self.screen.get_rect() # list to hold animation images self.walk_list = walk_list # Timer class to animate sprites self.animation = Timer(frames=self.walk_list) # get the rect of the image self.image = self.animation.imagerect() self.rect = self.image.get_rect() self.sur = pygame.Surface((32, 32)) self.rect.centerx = self.screen_rect.centerx self.rect.centery = self.screen_rect.centery self.rect.bottom = self.screen_rect.bottom # store objects exact position self.x = float(self.rect.centerx) self.y = float(self.rect.centery) # movement flags self.death_jump = 0 self.moving_left = False self.moving_right = False self.direction = 1 #collision flags self.floor = False self.brick = False self.obstacleL = False self.obstacleR = False # death flag self.died = False def blitme(self): if self.died: self.sur = self.image self.screen.blit(pygame.transform.flip(self.image, False, True), self.rect) if not self.died: self.sur = self.image self.screen.blit(self.image, self.rect) def update(self): self.x += (0.5 * self.direction) self.rect.x = self.x # self.image = self.walk_list[self.animation.frame_index()] if not self.died: self.image = self.walk_list[self.animation.frame_index()] if self.obstacleR: self.direction *= -1 print("going left") elif self.obstacleL: self.direction *= -1 print("going right") if self.died: self.direction = 0 self.image = self.walk_list[self.animation.frame_index()] if self.rect.bottom >= self.screen_rect.bottom: self.death_jump = -7 # self.rect.y += self.death_jump if self.death_jump != 0: self.death_jump += 8 print("goes here") self.rect.y += self.death_jump
class Koopa(Sprite): def __init__(self, screen, walk_list_left, walk_list_right, shell_list): super(Koopa, self).__init__() # get screen dimensions self.screen = screen self.screen_rect = self.screen.get_rect() # list to hold animation images self.walk_list_left = walk_list_left self.walk_list_right = walk_list_right self.shell_list = shell_list # self.x = self.rect.x # self.y = self.rect.y # Timer class to animate sprites self.animation = Timer(frames=self.walk_list_left) self.animation = Timer(frames=self.walk_list_right) self.animation = Timer(frames=self.shell_list) # get the rect of the image self.imageLeft = self.animation.imagerect() self.imageRight = self.animation.imagerect() self.imageShell = self.animation.imagerect() self.rect = self.imageLeft.get_rect() self.rect = self.imageRight.get_rect() self.rect = self.imageShell.get_rect() self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom # store objects exact position self.middle_x = float(self.rect.centerx) # movement flags self.moving_left = True self.moving_right = False self.direction = -2 # shell flag self.shell_mode = False self.shell_mode_moving = False #collision flags self.floor = False self.brick = False self.obstacleL = False self.obstacleR = False def blitme(self): if self.moving_left: self.sur = self.imageLeft print(self.sur) print("moving left") self.screen.blit(self.imageLeft, self.rect) if self.moving_right: self.sur = self.imageRight self.screen.blit(self.imageRight, self.rect) # TODO: handle blitme when the koopa is in shell mode if self.shell_mode: self.sur = self.imageShell self.screen.blit(self.imageShell, self.rect) if self.shell_mode_moving: self.sur = self.imageShell self.screen.blit(self.imageShell, self.rect) def update(self, level): self.middle_x += (1 * self.direction) # (2 * self.direction) self.rect.x = self.middle_x if level.move: self.rect.left -= (SPEED + self.direction) else: if self.direction > 0: if self.obstacleR: self.sur = self.imageLeft self.direction *= -1 self.moving_left = True self.moving_right = False elif self.direction < 0: if self.obstacleL: self.sur = self.imageRight self.direction *= -1 self.moving_right = True self.moving_left = False if self.obstacleR and self.shell_mode_moving: self.sur = self.imageShell self.direction *= -1 if self.obstacleL and self.shell_mode_moving: self.sur = self.imageShell self.direction *= 1 if self.moving_left: self.imageLeft = self.walk_list_left[ self.animation.frame_index()] if self.moving_right: self.imageRight = self.walk_list_right[ self.animation.frame_index()] # TODO: handle movement when turtle is in shell mode if self.shell_mode: self.moving_right = False self.moving_left = False self.direction = 0 self.imageShell = self.shell_list[self.animation.frame_index()] print("static") elif self.shell_mode_moving: self.moving_right = False self.moving_left = False if self.direction == 2: if self.obstacleR: self.direction *= -1 elif self.direction == -2: if self.obstacleL: self.direction *= -1 self.imageShell = self.shell_list[self.animation.frame_index()] print("moving")