-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.py
98 lines (92 loc) · 4.25 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
import mole
from cmu_112_graphics import *
class Enemy(object):
def __init__(self):
self.xPos = self.yPos = 0
self.behavior = 0
'''Behaviors
0 - idle, do nothing
1 - mole/player spotted, move towards mole/player
2 - attacking
3 - dead, carryable object
4 - being carried'''
self.health = 10
self.attackCharge = 0
self.enemyMoles = []
self.molesNeeded = 1
self.attackWarning = False
self.path = []
# Engage in attack sequence
def attack(self, app):
if self.attackCharge >= 500:
self.attackWarning = True
if self.attackCharge >= 1000:
# Kill all moles in enemy's attack range
for m in app.player.playerMoles:
for drow, dcol in [(-1, -1), (-1, 0), (-1, 1), (0, 1),
(1, 1), (1, 0), (1, -1), (0, -1), (0, 0)]:
if (0 <= self.yPos + drow < len(app.cave.cave) and
0 <= self.xPos + dcol < len(app.cave.cave[0])):
if (m.xPos, m.yPos) == (self.xPos + dcol, self.yPos + drow):
app.player.playerMoles.remove(m)
break
for m in app.worldMoles:
for drow, dcol in [(-1, -1), (-1, 0), (-1, 1), (0, 1),
(1, 1), (1, 0), (1, -1), (0, -1), (0, 0)]:
if (0 <= self.yPos + drow < len(app.cave.cave) and
0 <= self.xPos + dcol < len(app.cave.cave[0])):
if (m.xPos, m.yPos) == (self.xPos + dcol, self.yPos + drow):
app.worldMoles.remove(m)
break
self.attackCharge = 0
self.behavior = 0
self.attackWarning = False
self.attackCharge += app.timerDelay
def move(self, app):
if self.behavior == 1:
if self.path == []:
self.behavior = 2
else:
(self.yPos, self.xPos) = (self.path[0])
self.path.pop(0)
elif self.behavior == 4:
# Make new moles at landing site when dead enemy reaches it
if (self.xPos, self.yPos) == app.landSite:
newMoles = []
for _ in range(5 + len(self.enemyMoles)):
newMoles.append(mole.Mole())
for m in newMoles:
m.xPos, m.yPos = app.landSite
m.behavior = 2
app.worldMoles.append(m)
app.worldEnemies.remove(self)
else:
(self.yPos, self.xPos) = (self.path[0])
self.path.pop(0)
def drawEnemy(self, app, canvas):
if self.behavior < 3:
color = "purple"
text = f'{self.health}'
if self.behavior == 0:
sprite = app.enemySprites["Idle"]
else: sprite = app.enemySprites["Attack"]
else:
color = "black"
sprite = app.enemySprites["Dead"]
text = f'{len(self.enemyMoles)}/{self.molesNeeded}'
if app.mode == "gameplayMode":
if (self.xPos in range(app.player.xPos - 20, app.player.xPos + 20)
and self.yPos in range(app.player.yPos - 20, app.player.yPos + 20)):
xPos = app.width//2 + (self.xPos - app.player.xPos)*20
yPos = app.height//2 + (self.yPos - app.player.yPos)*20
if self.attackWarning == True:
canvas.create_image(xPos, yPos,
image = ImageTk.PhotoImage(app.attackWarningSprite))
canvas.create_image(xPos, yPos,
image = ImageTk.PhotoImage(sprite))
canvas.create_text(xPos - 10, yPos - 20, anchor = "nw",
text = text, font = "System 9 bold",
fill = "red")
elif app.mode == "mapMode":
canvas.create_oval(self.xPos*8, self.yPos*8, (self.xPos+1)*8,
(self.yPos+1)*8, fill = color, width = 0)