-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
69 lines (57 loc) · 2.14 KB
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import pygame
from support import import_folder
class Player(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.import_character_assets()
self.frame_index = 0
self.animation_speed = 0.15
# self.image = pygame.Surface((32,64))
# self.image.fill('red')
self.image = self.animations['idle'][self.frame_index]
self.rect = self.image.get_rect(topleft = pos)
# player movement
self.direction = pygame.math.Vector2(0,0)
self.speed = 8
self.gravity = 0.8
self.jump_speed = -16 # negative because I want to jump up
"""player animation"""
def import_character_assets(self):
# path to folder with animations
character_path ='./graphics/character/'
# there are folders for idle, run, jump etc..
self.animations = {'idle':[], 'run':[],'jump':[],'fall':[]}
for animation in self.animations.keys():
full_path = character_path + animation
self.animations[animation] = import_folder(full_path)
def animate(self):
animation = self.animations['run']
# loop over frame index
self.frame_index += self.animation_speed
if self.frame_index >= len(animation):
self.frame_index = 0
self.image = animation[int(self.frame_index)]
def get_input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
self.direction.x = 1
pass
elif keys[pygame.K_LEFT]:
self.direction.x = -1
pass
else:
self.direction.x = 0
pass
if keys[pygame.K_SPACE]:
self.jump()
def apply_gravity(self):
self.direction.y += self.gravity
self.rect.y += self.direction.y
"""Method for counter acting gravity force"""
def jump(self):
self.direction.y = self.jump_speed
def update(self):
self.get_input()
# self.rect.x += self.direction.x * self.speed # separate because we want detect horizontal collisions
# self.apply_gravity() # moved to the level class
self.animate()