-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monster.py
217 lines (184 loc) · 6.82 KB
/
Monster.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#################################################################
# Monster Class
# Your Name:Denise Yang
# Your Andrew ID: denisey
# Your Section: C
#################################################################
import pygame
import math
import random
from Bar import Bar
'''
framework taken from
https://github.com/LBPeraza/Pygame-Asteroids/blob/master/Asteroids/Ship.py
by LBPeraza
A* Algorithm logic from: http://theory.stanford.edu/~amitp/GameProgramming/AStarComparison.html
Guidelines for sprite animation here:
https://stackoverflow.com/questions/14044147/animated-sprite-from-few-images
Skull sprite:
https://pixel-poem.itch.io/dungeon-assetpuck
Ghost sprite:
https://opengameart.org/content/ghost-lvl1-2-3-60px
'''
from GameObject import GameObject
from Attack import Attack
class Monster(pygame.sprite.Sprite):
def __init__(self, x, y,r, grid, pLvl):
self.x, self.y, self.r = x, y, r
super(Monster, self).__init__()
self.images = []
self.index = 0
self.initImages()
self.image = self.images[self.index]
self.sequence = 4
self.grid = grid
self.distance = 100
self.path = []
self.hp = 10 * int(math.sqrt(pLvl))
self.hpBar = Bar(self.hp,(255,0,0), self.x, self.y - 10, 25, 5)
self.nodes = self.getNodes()
self.updateRect()
self.maxStr = 2 * int(math.sqrt(pLvl))
self.str = random.randint(0,self.maxStr)
self.speed = 20
def initImages(self):
#initialize images
self.images = []
self.images.append(pygame.transform.scale(pygame.image.load('v2/skull_v2_1.png'), (20,20)))
self.images.append(pygame.transform.scale(pygame.image.load('v2/skull_v2_2.png'), (20,20)))
self.images.append(pygame.transform.scale(pygame.image.load('v2/skull_v2_3.png'), (20,20)))
self.images.append(pygame.transform.scale(pygame.image.load('v2/skull_v2_4.png'), (20,20)))
def updateRect(self):
#updates rect
r = self.r
self.hpBar.updatePos(self.x, self.y-10)
self.rect = pygame.Rect(self.x, self.y, r, r)
def shift(self, isScroll, scrollX, scrollY, px,py):
#side scrolls monsters
self.updateRect()
if isScroll[2] == True:
self.x += self.r
isScrolling = True
elif isScroll[3] == True:
self.x -= self.r
isScrolling = True
elif isScroll[0] == True:
self.y += self.r
isScrolling = True
elif isScroll[1] == True:
self.y -= self.r
isScrolling = True
else:
isScrolling = False
if isScrolling:
self.updatePath(px,py, scrollX, scrollY)
self.updateRect()
def updatePath(self, px, py, scrollX, scrollY):
#gets new path to player's current location
start = (self.x - scrollX* self.r, self.y - scrollY*self.r)
end = (px -scrollX*self.r,py - scrollY*self.r)
self.path = []
self.path = self.getPath(start, end)
def update(self, pPos, scrollX, scrollY):
#moves to next place on path
self.index += 1
self.image = self.images[self.index % self.sequence]
self.updateRect()
if self.getDis((self.x,self.y),(pPos[0],pPos[1])) >= self.distance:
if self.path != None and len(self.path) != 0:
nextPos = self.path.pop()
self.x = nextPos[0] + scrollX * self.r
self.y = nextPos[1] + scrollY * self.r
self.updateRect()
##pathfinding algorithm
def getNodes(self):
#get nodes of map
grid = self.grid
nodes = set()
for row in range(len(grid)):
for col in range(len(grid[row])):
if grid[row][col][2] == 0:
nodes.add((row, col))
return nodes
def getNeighbors(self, curr):
#get neighbors of current node
x = curr[0]//self.r
y = curr[1]//self.r
dir = [(x, y + 1),(x, y-1),(x + 1,y),(x - 1, y)]
neighbors = []
for i in range(len(dir)):
if dir[i] in self.nodes:
neighbors.append((dir[i][0]*self.r, dir[i][1]*self.r))
return neighbors
def getDis(self, curr, goal):
#get dis between nodes and estimating heuristic
return ((curr[0]-goal[0])**2 + (curr[1]-goal[1])**2)**.5
def getLowest(self, open,f):
#return node w/ lowest fscore
lowestScore = 10**10
lowestNode = None
for node in open:
if f[node] < lowestScore:
lowestScore = f[node]
lowestNode = node
return lowestNode
def search(self, start,end):
#search for path
open = set()
open.add((start))
closed = set()
curr = start
f = {}
g = {}
cameFrom = {}
g[start] = 0
f[start] = g[start] + self.getDis(start,end)
while len(open) != 0:
curr = self.getLowest(open, f)
open.remove(curr)
closed.add(curr)
if curr == end:
return cameFrom
for neigh in self.getNeighbors(curr):
gCost = g[curr] + self.getDis(curr,neigh)
if neigh in open and gCost <= g[neigh]:
open.remove(neigh)
if neigh in closed and gCost< g[neigh]:
closed.remove(neigh)
if neigh not in open and neigh not in closed:
g[neigh] = gCost
open.add(neigh)
f[neigh] = gCost + self.getDis(neigh, end)
cameFrom[neigh] = curr
def getPath(self, start, end):
#compiles path
path = []
camefrom = {}
cameFrom = self.search(start,end)
path.append(end)
if cameFrom != None:
next = cameFrom.get(end)
while next != start:
path.append(next)
next = cameFrom.get(next)
path.append(start)
return path
class Boss(Monster):
#boss monster
def __init__(self, x,y,r,grid, pLvl):
super().__init__(x,y,r,grid, pLvl)
self.image = []
self.sequence = 2
self.index = 0
self.initImages()
self.image = self.images[self.index]
self.distance = 80
self.hp = 100
self.hpBar = Bar(self.hp,(255,0,0), self.x, self.y - 10, 25, 5)
self.nodes = self.getNodes()
self.updateRect()
self.str = 10
def initImages(self):
#initialize boss monster images
self.images.append(pygame.transform.scale(pygame.image.load('boss1.png'), (20,20)))
self.images.append(pygame.transform.scale(pygame.image.load('boss2.png'), (20,20)))