-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
136 lines (96 loc) · 3.44 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import pygame
import global_settings
from support import import_folder
class Player(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pygame.image.load('Ino_game_asssets/player/walk/1.png')
# self.image.fill("green")
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 = -20 #-20
#player anim
self.frame_index = 0
self.animation_speed = 0.25
self.import_hero_assets()
#player status
self.health = 100
self.is_green = True
self.status = 'stand'
self.facing_right = True
self.on_ground = False
#sounds
self.jump_sound = pygame.mixer.Sound("Ino_game_asssets/music/jump.mp3")
def import_hero_assets(self):
path_to_anim = "C:/Users/Dr_smail/PycharmProjects/Lesson2/anim/"
path_to_anim = 'C:/Users/Dr_smail/PycharmProjects/Lesson2/Ino_game_asssets/player'
self.animations = {'walk': [], 'jump': [], 'death': [],'stand': []}
for anim in self.animations.keys():
full_path = path_to_anim + '/' + anim
self.animations[anim] = import_folder(full_path)
def get_status(self):
if self.direction.y < 0:
self.status ='jump'
elif self.direction.y > 0:
self.status = 'fall'
else:
if self.direction.x != 0:
self.status = 'walk'
else:
self.status = 'stand'
def get_input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
self.facing_right = True
self.direction.x = 1
if keys[pygame.K_LEFT]:
self.facing_right = False
self.direction.x = -1
if not keys[pygame.K_LEFT] and not keys[pygame.K_RIGHT]:
self.direction.x = 0
if keys[pygame.K_UP] and self.on_ground:
self.on_ground = False
self.jump()
if keys[pygame.K_f]:
self.kill()
# if keys[pygame.K_DOWN]:
# self.direction.y = 1
def apply_grav(self):
self.direction.y += self.gravity
self.rect.y += self.direction.y
def jump(self):
self.direction.y = self.jump_speed
self.jump_sound.play()
def animate(self):
status = self.status
if status in self.animations.keys():
animation = self.animations[status]
else:
return
self.frame_index += self.animation_speed
if self.frame_index > len(animation) - 1:
self.frame_index = 0
#print(f'frame_index {self.frame_index}')
#print(f'status {self.status}')
img = animation[int(self.frame_index)]
if self.facing_right == True:
self.image = img
else:
flip_img = pygame.transform.flip(img,True,False)
self.image = flip_img
def death(self):
print(f"Игрок умер")
status = "death"
global_settings.GameIsRuning = False
def recive_demage(self, amount):
self.health = self.health - amount;
print(f"Игрок получил урон, осталось {self.health} хп")
if self.health <= 0:
self.death()
def update(self):
self.get_input()
self.get_status()
self.animate()