-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.py
58 lines (39 loc) · 1.41 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
import pygame
# class inherets from pygame sprite class
class Enemy(pygame.sprite.Sprite):
def __init__(self, screenDimensions, position, enemyType, enemyImages):
# Initialize parent sprite class
super().__init__()
self.screenWidth = screenDimensions[0]
self.screenHeight = screenDimensions[1]
self.enemyType = enemyType
if self.enemyType == "basic":
self.image = pygame.image.load(enemyImages[0])
else:
self.image = pygame.image.load(enemyImages[1])
self.rect = self.image.get_rect(center = position)
self.position = pygame.math.Vector2(position) # position of enemy is a vector type
self.speed = 0.9
def GetType(self):
return self.enemyType
def MoveTowardsPlayer(self, player):
playerPosition = player.sprite.rect.center
direction = playerPosition - self.position
velocity = direction.normalize() * self.speed
self.position += velocity
self.rect.center = self.position
def Move(self):
cx = self.rect.x
cy = self.rect.y
# Move enemy
self.rect.x = cx
self.rect.y = cy
# Update sprite logic
def update(self, player):
self.MoveTowardsPlayer(player)
self.delete()
# Destroy sprite
def delete(self):
condition = False
if condition:
self.kill()