-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemies.py
102 lines (88 loc) · 3.12 KB
/
enemies.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
import pygame
from settings import *
class Enemies(pygame.sprite.Sprite):
def __init__(self,name,pos,groups,obstacle_sprites,damage_player):
super().__init__(groups)
self.image = pygame.transform.scale_by(pygame.image.load('Grafika/Enemies/2.png').convert_alpha(),SCALE)
self.rect = self.image.get_rect(topleft = pos)
self.direction = pygame.math.Vector2()
self.sprite_type = 'enemy'
self.obstacle_sprites = obstacle_sprites
self.name = name
monster_info = monster_data[self.name]
self.health = monster_info['health']
self.speed = monster_info['speed']
self.damage = monster_info['strength']
self.notice_radius = monster_info['notice_radius']
self.attack_radius = 10
self.status = 'idle'
self.can_attack = True
self.attack_time = None
self.attack_cooldown = 1000
self.damage_player = damage_player
self.can_be_damaged = True
self.hit_time = None
self.invincibility_time = 500
def actions(self, player):
if self.status == 'attack':
self.attack_time = pygame.time.get_ticks()
self.damage_player(self.damage)
elif self.status == 'move':
self.direction = self.get_player_distance_direction(player)[1]
else:
self.direction = pygame.math.Vector2()
def cooldown(self):
current_time = pygame.time.get_ticks()
if not self.can_attack:
if current_time - self.attack_time >= self.attack_cooldown:
self.can_attack = True
if not self.can_be_damaged:
if current_time - self.hit_time >= self.invincibility_time:
self.can_be_damaged = True
def get_status(self, player):
distance = self.get_player_distance_direction(player)[0]
if distance <= self.attack_radius and self.can_attack:
if self.status != 'attack':
self.frame_index = 0
self.status = 'attack'
elif distance <= self.notice_radius:
self.status = 'move'
else:
self.status = 'idle'
def get_player_distance_direction(self,player):
enemy_vec = pygame.math.Vector2(self.rect.center)
player_vec = pygame.math.Vector2(player.rect.center)
distance = (player_vec - enemy_vec).magnitude()
if distance > 0:
direction = (player_vec - enemy_vec).normalize()
else:
direction = pygame.math.Vector2()
return (distance,direction)
def move(self, speed):
if self.direction.magnitude() != 0:
self.direction = self.direction.normalize()
self.rect.x += self.direction.x * speed
self.collision('horizontal')
self.rect.y += self.direction.y * speed
self.collision('vertical')
def collision(self, direction):
if direction == 'horizontal':
for sprite in self.obstacle_sprites:
if sprite.rect.colliderect(self.rect):
if self.direction.x > 0: # moving right
self.rect.right = sprite.rect.left
if self.direction.x < 0: # moving left
self.rect.left = sprite.rect.right
if direction == 'vertical':
for sprite in self.obstacle_sprites:
if sprite.rect.colliderect(self.rect):
if self.direction.y > 0: # moving down
self.rect.bottom = sprite.rect.top
if self.direction.y < 0: # moving up
self.rect.top = sprite.rect.bottom
def update(self):
self.move(self.speed)
self.cooldown()
def enemy_update(self,player):
self.get_status(player)
self.actions(player)