-
Notifications
You must be signed in to change notification settings - Fork 1
/
enemy.py
108 lines (78 loc) · 3.24 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
import pygame
from character import Character
from astar import AStar
from random import randint
from key import Key
class Enemy(Character):
Enemies = []
Images = { "type1" : pygame.image.load("images/zombie1.png"),
"type2" : pygame.image.load("images/zombie2.png"),
"type3" : pygame.image.load("images/zombie3.png"),
"type4" : pygame.image.load("images/zombie4.png"),
"type5" : pygame.image.load("images/zombie5.png") }
Types = ["type1", "type2", "type3", "type4", "type5"]
Died = 0
def __init__(self, x, y, player, enemy_type):
self.player = player
self.player_last_position = (0, 0)
self.path = []
self.img = { "E" : Enemy.Images[enemy_type],
"W" : pygame.transform.rotate(Enemy.Images[enemy_type], 180),
"N" : pygame.transform.rotate(Enemy.Images[enemy_type], 90),
"S" : pygame.transform.rotate(Enemy.Images[enemy_type], -90) }
if enemy_type == "type1":
health = 500
self.damage = 10
elif enemy_type == "type2":
health = 600
self.damage = 15
elif enemy_type == "type3":
health = 700
self.damage = 20
elif enemy_type == "type4":
health = 900
self.damage = 25
pygame.mixer.Sound("sounds/zombie_one.wav").play()
elif enemy_type == "type5":
health = 1200
self.damage = 30
pygame.mixer.Sound("sounds/zombie_two.wav").play()
super(Enemy, self).__init__(x, y, 32, 32, health)
Enemy.Enemies.append(self)
def move(self):
if self.next_step_x == None and self.next_step_y == None:
if self.player.x != self.player_last_position[0] or self.player.y != self.player_last_position[1]:
self.player_last_position = (self.player.x, self.player.y)
if self.player.next_step_x == None and self.player.next_step_y == None:
self.path = AStar.get_path(self)
if self.next_step_x == None and self.next_step_y == None:
if len(self.path) > 0:
l = self.path.pop()
self.next_step_x = l.x
self.next_step_y = l.y
else:
if self.x > self.next_step_x:
self.x -= 4
self.direction = "W"
elif self.x < self.next_step_x:
self.x += 4
self.direction = "E"
if self.y > self.next_step_y:
self.y -= 4
self.direction = "N"
elif self.y < self.next_step_y:
self.y += 4
self.direction = "S"
if self.x == self.next_step_x and self.y == self.next_step_y:
self.next_step_x = None
self.next_step_y = None
def draw(self, screen):
screen.blit(self.img[self.direction], (self.x, self.y))
def get_hit(self, damage):
self.health -= damage
if self.health <= 0:
Enemy.Died += 1
if Enemy.Died >= 30:
Enemy.Died = 0
Key(self.x, self.y)
Enemy.Enemies.remove(self)