-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.py
149 lines (132 loc) · 4.08 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
import math
from Hero import *
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
class Enemy(pygame.sprite.Sprite):
def __init__(self, pos, health, spriteGroup, L,R ,U, D, speed = 2):
self.width = 50
self.height = 50
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
#self.image = pygame.Surface([self.width, self.height])
#self.image.fill(BLACK)
self.health = health
self.spriteGroup = spriteGroup
self.imagesL = L
self.imagesR = R
self.imagesU = U
self.imagesD = D
self.image = self.imagesD[0]
self.imageLeft = 0
self.imageRight = 0
self.imageDown = 0
self.imageUp = 0
self.speed = speed
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
self.rect.topleft = pos
def changeImage(self,U,R,D,L):
self.imagesL = L
self.imagesR = R
self.imagesU = U
self.imagesD = D
def resetImage(self, direction):
if direction == "left":
self.imageRight = 0
self.imageDown = 0
self.imageUp = 0
elif direction == "right":
self.imageLeft = 0
self.imageDown = 0
self.imageUp = 0
elif direction == "down":
self.imageRight = 0
self.imageLeft = 0
self.imageUp = 0
elif direction == "up":
self.imageRight = 0
self.imageDown = 0
self.imageLeft = 0
def moveImage(self, direction):
self.resetImage(direction)
if direction == "left":
self.imageLeft += 1
if self.imageLeft > 2:
self.imageLeft = 0
self.image = self.imagesL[self.imageLeft]
elif direction == "right":
self.imageRight += 1
if self.imageRight > 2:
self.imageRight = 0
self.image = self.imagesR[self.imageRight]
elif direction == "down":
self.imageDown += 1
if self.imageDown > 2:
self.imageDown = 0
self.image = self.imagesD[self.imageDown]
elif direction == "up":
self.imageUp += 1
if self.imageUp > 2:
self.imageUp = 0
self.image = self.imagesU[self.imageUp]
def setPosition(self, pos):
self.rect.topleft = pos
def changeHealth(self, healthChange):
self.health -= healthChange
if self.health <= 0 :
return True
return False
def wallCollide(self):
return self.rect.left < 0 or self.rect.right > 600 or self.rect.top < 0 or self.rect.bottom > 600
def objectCollisions(self):
for sprite in self.spriteGroup:
if pygame.sprite.collide_rect(self, self.spriteGroup[sprite]):
return True
return None
def pushBack(self, direction):
if direction == 1:
self.rect.y -= 40
elif direction == 2:
self.rect.x += 40
elif direction == 4:
self.rect.x -= 40
elif direction == 3:
self.rect.y += 40
def chasePlayer(self, player):
# find normalized direction vector (dx, dy) between enemy and player
dx, dy = self.rect.x - player.rect.x, self.rect.y - player.rect.y
dist = math.hypot(dx, dy) + 0.01
if self.rect.x > player.rect.x:
self.rect.x -= self.speed
elif self.rect.x < player.rect.x:
self.rect.x += self.speed
# Movement along y direction
if self.rect.y < player.rect.y:
self.rect.y += self.speed
elif self.rect.y > player.rect.y:
self.rect.y -= self.speed
#change image
if math.fabs(dx) > math.fabs(dy) and dx > 0:
self.moveImage("left")
if self.wallCollide() or self.objectCollisions() or dist < 20 :
self.rect.x += 40
elif math.fabs(dx) > math.fabs(dy) and dx <= 0:
self.moveImage("right")
if self.wallCollide() or self.objectCollisions() or dist < 20:
self.rect.x -= 40
if math.fabs(dy) > math.fabs(dx) and dy > 0:
self.moveImage("up")
if self.wallCollide() or self.objectCollisions() or dist < 20:
self.rect.y += 40
elif math.fabs(dy) > math.fabs(dx) and dy <= 0:
self.moveImage("down")
if self.wallCollide() or self.objectCollisions() or dist < 20:
self.rect.y -= 40
if dist < 20:
player.health -= 1