-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.py
61 lines (49 loc) · 1.77 KB
/
entity.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
import pygame
from settings import *
class Entity:
def __init__(self, x, y, w, h, speed, begin_act):
self.x = x
self.y = y
self.w = w
self.h = h
self.rect = pygame.Rect(x, y, w, h)
self.life = 100
self.life_rect = pygame.Rect(x, y, w, 4)
self.action = begin_act
self.animations_database = {}
self.frame = 0
self.flip = False
self.velocity = [0, 0]
self.speed = speed
@staticmethod
def animations(path, frame_length):
db_animation = []
for i in range(len(frame_length)):
animation_dir = path + "_" + str(i) + ".png"
animation_image = pygame.transform.scale2x(pygame.image.load(animation_dir))
animation_image.set_colorkey(WHITE_COLOR)
for frame in range(frame_length[i]):
db_animation.append(animation_image)
return db_animation
def move_right(self):
self.rect.x += self.velocity[0]
def move_left(self):
self.rect.x += self.velocity[0]
def move_up(self):
self.rect.y += self.velocity[1]
def move_down(self):
self.rect.y += self.velocity[1]
def update(self):
self.frame += 1
if self.frame >= len(self.animations_database[self.action]):
self.frame = 0
self.life_rect.x = self.rect.x
self.life_rect.y = self.rect.y - 26
def damage(self, life_decrease):
self.life -= life_decrease
self.life_rect.width -= self.w / (100 / life_decrease)
def check_die(self):
if self.life <= 0:
return True
def draw(self, window):
window.blit(pygame.transform.flip(self.animations_database[self.action][self.frame], self.flip, False), (self.rect.x, self.rect.y - 20))