-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.py
148 lines (114 loc) · 4.63 KB
/
enemy.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import pygame
GRAVITY = 0.2
SCREEN_HEIGHT = 720
JUMP_STRENGTH = -3.4
FPS = 60
ENEMY_ATTACK_TIME = 0.5
class Enemy(pygame.sprite.Sprite):
"""player"""
def __init__(self, x, image_path, TOTALLIVES=3, FOLLOW_SPEED = 3, KNOCKBACK_DIST = 300):
self.FOLLOW_SPEED = FOLLOW_SPEED
self.KNOCKBACK_DIST = KNOCKBACK_DIST
self.TOTALLIVES = TOTALLIVES
self.lives = self.TOTALLIVES
self.knockbacked = False
pygame.sprite.Sprite.__init__(self)
self.bark = pygame.mixer.Sound("assets/se/dog_bark_clip.ogg")
self.oof = pygame.mixer.Sound("assets/se/oof-clip.ogg")
self.bark.set_volume(0.3)
self.bark_channel = pygame.mixer.Channel(0)
self.x = x
self.y = 446
self.K_LEFT, self.K_RIGHT, self.K_A, self.K_D, self.K_CLICK, self.K_SPACE = False, False, False, False, False, False
self.image_R = pygame.image.load(image_path).convert_alpha()
self.image_L = pygame.transform.flip(self.image_R, True, False)
self.image = self.image_R
self.rect = self.image.get_rect()
self.rect.topleft = (self.x, self.y)
self.attack = False
self.can_attack = False
self.jump_velocity = JUMP_STRENGTH
self.is_jumping = False
self.facing_right = True
self.time_seg1 = ENEMY_ATTACK_TIME * FPS
self.time_seg1_store = self.time_seg1
def wait_time_done(self):
self.time_seg1 -= 1
if self.time_seg1 <= 0:
self.time_seg1 = self.time_seg1_store
return True
else:
return False
def attack_movement(self):
if self.is_jumping:
self.rect.y += self.jump_velocity
self.jump_velocity += GRAVITY
if self.rect.y >= SCREEN_HEIGHT - self.rect.height - 100:
self.rect.y = SCREEN_HEIGHT - self.rect.height - 100
# print("asdf")
self.is_jumping = False
if not self.is_jumping:
self.is_jumping = True
self.jump_velocity = JUMP_STRENGTH
# print(self.time_seg1)
def draw(self, screen):
screen.blit(self.image, (self.x,0))
def knockback(self):
if self.facing_right:
self.rect.x += self.KNOCKBACK_DIST
else:
self.rect.x -= self.KNOCKBACK_DIST
self.knockbacked = False
def jump(self):
if self.is_jumping:
self.rect.y += self.jump_velocity
self.jump_velocity += GRAVITY
if self.rect.y >= SCREEN_HEIGHT - self.rect.height - 100:
self.rect.y = SCREEN_HEIGHT - self.rect.height - 100
# print("asdf")
self.is_jumping = False
if not self.is_jumping:
self.is_jumping = True
self.jump_velocity = JUMP_STRENGTH
def follow_player(self, player, camera):
my_pos = pygame.math.Vector2(self.rect.center)
target_pos = pygame.math.Vector2(player.rect.center)
# Calculate direction vector (target - enemy)
direction = target_pos - my_pos
# what way is it moving?
if direction.x > 0:
self.facing_right = False
else:
self.facing_right = True
# print(direction)
# Normalize direction to get unit vector (length 1)
if direction.length() > 0:
direction = direction.normalize()
# Move enemy in the direction of the target
if self.rect.center[0] < 0 - camera.x - 1280/2 or self.rect.center[0] > 1280*2 -camera.x - 1280/2 + 2*self.rect.width:
my_pos += direction * self.FOLLOW_SPEED * 20
# print('fast')
else:
my_pos += direction * self.FOLLOW_SPEED
# Update enemy's position
self.rect.center = (my_pos.x, my_pos.y)
def update(self, player_group, background_rect, camera, screen):
# print(self.x)
# print(camera)
if not self.knockbacked:
if self.facing_right:
self.image = self.image_R
else:
self.image = self.image_L
self.jump()
self.x = self.rect.x
self.y = self.rect.y
for player in player_group:
self.follow_player(player, camera)
else:
self.knockback()
if self.rect.left < background_rect.left:
self.rect.left = background_rect.left
if self.rect.right > background_rect.right:
self.rect.right = background_rect.right
pygame.draw.rect(screen, 'green', pygame.Rect(self.rect.x, self.rect.top+self.rect.width/40, self.rect.width/self.TOTALLIVES*self.lives, 5))