-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreature.py
272 lines (228 loc) · 8.87 KB
/
creature.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import collections
import configs
from godObject import *
'''
Class which describes all the persons in the game.
'''
class Person(GodObject):
# strength means lives for Mandalorian
def __init__(self, strength, identifier, x_len, y_len):
obj_loc = Location(x_len, y_len)
super().__init__(identifier, obj_loc)
self.__strength = strength
def getStrength(self):
return self.__strength
def setStrength(self, strength):
self.__strength = strength
def createDragon(self):
pass
'''
Hero of the game
'''
class Mandalorian(Person):
def __init__(self, shield_present=True, shield_active=False):
super().__init__(configs.MANDAINITIALLIVES, configs.MANDAID,
configs.MANDAXLEN, configs.MANDAYLEN)
self.shield_present = shield_present
self.shield_active = shield_active
# make the image of hero
self.shape = " O >-|-< /\\ "
self.shape = np.array(list(self.shape))
self.shape = self.shape.reshape(3, 5)
self.shieldShape = " O |>-|-| /\\|"
self.shieldShape = np.array(list(self.shieldShape))
self.shieldShape = self.shieldShape.reshape(3, 5)
self.bullet = 'oo'
self.bullet = np.array(list(self.bullet))
self.bullet = self.bullet.reshape(1, 2)
# keep the list of the locations bullets fired by Manda
self.bulletList = []
# vertical acceleration and speed
self.velocityX = 0
self.velocityY = 0
# dragon form is on or not. Once it is on we need to cycle
self.dragonMode = False
self.dragonIndex = 0
def createDragon(self):
d1 = '_.~"~._.~"~._'
d2 = '._.~"~._.~"~.'
d3 = '~._.~"~._.~"~'
d4 = '"~._.~"~._.~"'
d5 = '~"~._.~"~._.~'
d6 = '.~"~._.~"~._.'
empty = ' '
t1 = ' _p_ '
t2 = ' / *\\ '
t4 = ' / .\\ '
t3 = '/ /^`-\''
dragonFrames = [d1, d2, d3, d4, d5, d6]
if self.dragonIndex % 2 == 0:
dragon =\
empty+t1+dragonFrames[self.dragonIndex]+t2+dragonFrames[self.dragonIndex]+t3
else:
dragon =\
empty+t1+dragonFrames[self.dragonIndex]+t4+dragonFrames[self.dragonIndex]+t3
dragon = np.array(list(dragon))
dragon = dragon.reshape(3, 20)
self.dragonIndex = (self.dragonIndex + 1) % 6
# print(self.dragonIndex,end='')
return dragon
'''
fire the bullet in the forward direction, And also keep track of it if
the bullet hit the Viserion. Firing a bullet will add location of the bullet
to the list.
'''
def fireBullet(self):
# get the position of Manda, that will be position of bullet
x, y = self.obj_location.getLocation()
x += 1
if self.dragonMode:
y += 20
else:
y += 5
# don't fire bullets if they are outside the screen
if y >= configs.GRIDWIDTH:
return
loc = Location(
configs.BULLETXLEN,
configs.BULLETYLEN) # need to pass the size of the bullets
loc.setLocation(x, y)
self.bulletList.append(loc)
# TODO print the bullet on the screen this will be done in game.py
def hittingViser(self, loc, ViserionXloc):
x, y = loc.getLocation()
result = False
for i in range(configs.BULLETXLEN):
for j in range(configs.BULLETYLEN):
if x + i >= ViserionXloc and x + i <= ViserionXloc + configs.DRAGONXLEN:
result |= y + j >= configs.GRIDWIDTH - configs.DRAGONYLEN
return result
# shift forward each of the bullet, this function will be called from the
# game.py or the main.py It returns the points scored by hitting viserion
def updateBulletStatus(self, ViserionPresent, ViserionXloc):
index = []
cnt = 0
for loc in self.bulletList:
x, y = loc.getLocation()
y += 1
loc.setLocation(x, y)
# check if the bullet is out of the grid
if y < configs.GRIDWIDTH:
index.append(cnt)
cnt += 1
'''
If Viser is present than find out the bullets which are hitting
him and delete those bullets, also update the game score.
'''
cnt = 0
incrementScore = 0
if ViserionPresent:
for loc in self.bulletList:
if self.hittingViser(loc, ViserionXloc):
incrementScore += 1
else:
index.append(cnt)
cnt += 1
index = set(index)
temp = [self.bulletList[i] for i in index]
self.bulletList = temp[:]
return incrementScore
'''
There can be two ways:-
1. We can just increase the speed of moving the background and the Manda
remains in his same position on the grid.
2. We actually change the position of manda on the grid my moving him one
column forward.
'''
# here we need to implement gravity as well
# the Impulse will change the velocity in upward and gravity will try to
# change the velocity in the downward direction . Also keep if the upper
# right corner touches the upper wall of the grid than don't further
# increase the velocity , it will imitate the collision with the wall
# because the impulse provided by the user up button will counteract the
# collision with the wall.
def move(self, keyPressed):
# This function only changes the position of Manda. Grid repainting, and
# other things will be done in game.py
# now place manda on grid after updating the location of manda
x, y = self.obj_location.getLocation()
if keyPressed in ['a', 'A']:
y -= 1
elif keyPressed in ['d', 'D']:
y += 1
elif keyPressed in ['w', 'W']:
#print('up pressed')
self.velocityY += configs.IMPULSE
elif keyPressed in ['b', 'B']:
self.fireBullet()
# the shield activation will be handled in game.py
x += self.velocityY
if y >= configs.GRIDWIDTH - configs.MANDAYLEN:
y -= 1
if y < 0:
y += 1
if x >= configs.GRIDHEIGHT - configs.MANDAXLEN:
x = configs.GRIDHEIGHT - configs.MANDAXLEN
if x <= 2:
x = 2
self.obj_location.setLocation(x, y)
'''
Boss enemy of the game . Changes position according to position of Mandalorian
and throws ice balls at him.
'''
class Viserion(Person):
def __init__(self):
super().__init__(configs.VISERIONINITIALSTRENGTH, configs.VISERIONID,
configs.VISERIONXLEN, configs.VISERIONYLEN)
# make the image of Viserion
self.ball = " - / \\ ooooo \\ / - "
self.ball = np.array(list(self.ball))
self.ball = self.ball.reshape(5, 5)
# Tells if viser is present on the game screen
self.present = False
self.dragonIndex = 0
def createDragon(self):
d1 = '_.~"~._.~"~._'
d2 = '._.~"~._.~"~.'
d3 = '~._.~"~._.~"~'
d4 = '"~._.~"~._.~"'
d5 = '~"~._.~"~._.~'
d6 = '.~"~._.~"~._.'
empty = ' '
t1 = ' _p_ '
t2 = ' /* \\ '
t4 = ' /. \\ '
t3 = '\'-`^\\ \\'
dragonFrames = [d1, d2, d3, d4, d5, d6]
if self.dragonIndex % 2 == 0:
dragon =\
t1+empty+t2+dragonFrames[self.dragonIndex]+t3+dragonFrames[self.dragonIndex]
else:
dragon =\
t1+empty+t4+dragonFrames[self.dragonIndex]+t3+dragonFrames[self.dragonIndex]
dragon = np.array(list(dragon))
dragon = dragon.reshape(3, 20)
self.dragonIndex = (self.dragonIndex + 1) % 6
# print(self.dragonIndex,end='')
return dragon
'''
There can be two ways of doing this:-
1. The ball move with the same speed as the background moves. Which means
the ball becomes a part of the background . This one is easy to code.
2. The ball moves with 2X speed as the background moves. Here the ball needs
to be repainted and also the previous version of the ball needs to
cleared , which is tough to do. For one cycle you will need to move the
ball, but for the other just stick the ball to the screen and screen will move the
ball. So for the cycle the ball will move you will need to store the
matrix of the background before moving it, and than you will restore that
piece of the background after you have moved the ball to next location.
'''
def fireIceBalls(self):
# trying the first method
if not self.present:
return
x, y = self.obj_location.getLocation()
y -= configs.BALLXLEN
x -= 1
# now return co-ordinates of the IceBall to be painted on the grid
return x, y