Untitled
unknown
python
a year ago
4.0 kB
7
Indexable
import pygame from animate import import_folder pygame.mixer.init() jumpObj = pygame.mixer.Sound('ScuttleKnight/Sounds/jump.mp3') class Character(pygame.sprite.Sprite): def __init__(self,pos): super().__init__() self.characterImages() self.frameNumber = 0 #number used to choose which image/frame we want to use out of the animation frames self.movementSpeed = 0.15 self.image = self.animations['idle'][self.frameNumber] #self.image = pygame.Surface((32,64)) #Placeholder shape 32 by 64 pixels self.rect = self.image.get_rect(topleft = pos) # get_rect creates a rectangle covering entire surface (helps detect collisions) #player status self.status = 'idle' self.right_side = True self.c_ground = False self.c_ceiling = False self.c_left = False self.c_right = False #player movement self.direction = pygame.math.Vector2(0,0) self.speed = 6 self.gravity = 1 self.jump_power = -18 # minus number so character jumps upwards def characterImages(self): images_path = 'ScuttleKnight/animations/knight/' # path for code to find the right files on my computer self.animations = {'idle':[],'run':[],'jump':[],'fall':[]} for animation in self.animations.keys(): path = images_path + animation self.animations[animation] = import_folder(path) def animate(self): animation = self.animations[self.status] # looping over frame index self.frameNumber += self.movementSpeed if self.frameNumber >= len(animation): self.frameNumber = 0 image = animation[int(self.frameNumber)] if self.right_side: self.image = image else: swap_img = pygame.transform.flip(image,True,False) self.image = swap_img if self.c_ground and self.c_right: # to ensure the rectangle used for collisions is in the right place self.rect = self.image.get_rect(bottomright = self.rect.bottomright) elif self.c_ground and self.c_left: self.rect = self.image.get_rect(bottomleft = self.rect.bottomleft) elif self.c_ground: self.rect = self.image.get_rect(midbottom = self.rect.midbottom) elif self.c_ceiling and self.c_right: # to ensure the rectangle used for collisions is in the right place self.rect = self.image.get_rect(topright = self.rect.topright) elif self.c_ceiling and self.c_left: self.rect = self.image.get_rect(topleft = self.rect.topleft) elif self.c_ceiling: self.rect = self.image.get_rect(midtop = self.rect.midtop) def moveStatus(self): if self.direction.y < 0: self.status = 'jump' elif self.direction.y > 0.1: self.status = 'fall' else: if self.direction.x != -0.4: self.status = 'run' else: self.status = 'idle' def inputKeys(self): choice = pygame.key.get_pressed() if choice[pygame.K_a]: self.direction.x = -1.4 self.right_side = False elif choice[pygame.K_d]: self.direction.x = 0.6 self.right_side = True elif choice [pygame.K_d] and [pygame.K_a]: self.direction.x = -0.4 else: self.direction.x = -0.4 if choice[pygame.K_SPACE] and self.c_ground: jumpObj.play() self.jump() #while not World.check_bounds(): def insertGravity(self): self.direction.y += self.gravity self.rect.y += self.direction.y def jump(self): self.direction.y = self.jump_power def update(self): self.inputKeys() self.moveStatus() self.animate()
Editor is loading...
Leave a Comment