-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.py
84 lines (76 loc) · 3.37 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
import pygame
import math
class enemy:
def __init__(self,sprite,health,damage,startx,starty,endx,endy,screen,tag):
self.image = pygame.Surface((64,64))
self.image = pygame.image.load(sprite).convert_alpha()
self.rect = self.image.get_rect()
self.rect.topleft = (startx,starty)
self.green = None
self.red = None
self.startinghealth = health
self.health = health
self.damage = damage
self.screen = screen
self.x = startx
self.y = starty
self.nx = 0
self.ny = 0
self.eyesightRect = pygame.draw.line(self.screen,(0,0,0),(self.x,self.y),(endx,endy),10)
self.ID = tag
self.percantage = 0
self.width = 64
self.height = 20
self.vision = 1
self.visionStatus = False
self.obstacle = None
def render(self):
self.screen.blit(self.image,(self.x,self.y))
pygame.draw.rect(self.screen,(255,0,0),self.red)
pygame.draw.rect(self.screen,(0,255,0),self.green)
#self.eyesightRect
#display.blit(self.image,(self.x,self.y))
def update(self,x,y):
#print(self.x)
self.x += (self.nx * self.vision) + x
self.y += (self.ny * self.vision) + y
self.percantage = self.health/self.startinghealth
#self.rect = self.rect.move(self.nx,self.ny)
self.green = pygame.Rect(self.x,self.y+self.width,self.width,self.height)
self.red = pygame.Rect(self.x,self.y+self.width,self.width,self.height )
self.rect= self.rect.clamp(pygame.Rect(self.x,self.y,64,64)) #clamp allowed me to clamp the rect to the enemy
if self.percantage == 1:
self.green = pygame.Rect(self.x,self.y + self.width,self.width,self.height )
else:
difference = self.width * self.percantage
self.green = pygame.Rect(self.x,self.y + self.width,difference,self.height)
def getID(self):
return self.ID
def getHealth(self):
return self.health
def updateHealth(self, x):
self.health += x
def getDamage(self):
return self.damage
def setTrajectory(self,end,speed,objList): #draws a line, if the line collides with something, then stop
self.eyesightRect = pygame.draw.line(self.screen,(0,0,0,255),(self.x + 32,self.y + 32),(end[0] + 32,end[1] + 32),1) #width 0 so it doesnt show up
if self.obstacle != None:
if not pygame.Rect.colliderect(self.obstacle.getRect(),self.eyesightRect): #if we are not colliding with the same object again
self.vision = 1
self.visionStatus = True
for row in objList:
for collideable in enumerate(row):
if pygame.Rect.colliderect(collideable[1].getRect(),self.eyesightRect):
self.vision = 0
self.visionStatus = False
self.obstacle = collideable[1]
distance_y = self.y - end[1]
distance_x = self.x - end[0]
angle = math.atan2(distance_y, distance_x)
speed_x = speed * math.cos(angle)
speed_y = speed * math.sin(angle)
self.nx = -speed_x
self.ny = -speed_y
#print(self.eyesightRect.x,self.eyesightRect.y)
#self.screen.blit(self.image, (self.eyesightRect.x +64,self.eyesightRect.y +64))
#pygame.draw.line(self.screen,(0,0,0),(self.x,self.y),end,10)