-
Notifications
You must be signed in to change notification settings - Fork 0
/
Character.py
226 lines (175 loc) · 6.3 KB
/
Character.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
import pygame
from pygame.locals import *
from State import *
from StateFlyweight import *
class MovableObject(object):
def __init__(self, game, dic, pos):
self.game = game
self.pos = pos
self.dic_images = dic
self.counter = 0
def move(self):
self.state.move()
def paint(self):
self.game.paint(self.state.getImg(), self.pos)
def getPos(self):
return self.pos
class Character(MovableObject):
def __init__(self, game, dic, dic_images_ss, dic_powerUp, factory):
super(Character, self).__init__(game, dic, (170,620))
self.dics = {'normal':dic, 'supersaiyan':dic_images_ss, 'powerup': dic_powerUp}
self.superStateFly = factory.makeSuperStateFlyweight(self, self.dics)
self.state = self.superStateFly.getNormal()
self.rect = self.state.getImg().get_rect()
self.width = self.rect.right
self.height = self.rect.bottom
self.factory = factory
self.life = 6
self.bag = factory.makeBag(self)
def moveUp(self):
new_pos = self.state.getNextPos()
new_limit = self.state.getNextLimit()
top_limit = self.game.canMoveUp(new_limit)
if top_limit < 0:
self.rect.move((0, new_pos[1] - self.pos[1]))
self.pos = new_pos
else:
self.changeStopped()
self.rect.top = top_limit
self.pos = (self.pos[0], self.rect.top)
def moveDown(self):
new_pos = self.state.getNextPos()
new_limit = self.state.getNextLimit()
bottom_limit = self.game.canMoveDown(new_limit)
if bottom_limit < 0:
self.rect.move((0, new_pos[1] - self.pos[1]))
self.pos = new_pos
else:
self.changeStopped()
self.rect.bottom = bottom_limit
self.pos = (self.pos[0], self.rect.top)
def moveLeft(self):
new_pos = self.state.getNextPos()
new_limit = self.state.getNextLimit()
left_limit = self.game.canMoveLeft((new_limit[0], new_limit[1]))
if left_limit < 0:
self.rect.move((new_pos[0] - self.pos[0], 0))
self.pos = new_pos
else:
self.changeStopped()
self.rect.left = left_limit + 50
self.pos = (self.rect.left, self.pos[1])
def moveRight(self):
new_pos = self.state.getNextPos()
new_limit = self.state.getNextLimit()
right_limit = self.game.canMoveRight((new_limit[0], new_limit[1]))
if right_limit < 0:
self.rect.move((self.pos[0] - new_pos[0], 0))
self.pos = new_pos
else:
self.changeStopped()
self.rect.right = right_limit - 26
self.pos = (self.rect.right, self.pos[1])
def attack(self):
self.state.attack(self.counter)
self.counter += 1
def powerUp(self):
self.state.powerUp(self.counter)
self.counter += 1
def changeDown(self):
self.state.changeDown()
self.rect = self.state.getImg().get_rect()
self.rect.move(self.pos)
def changeUp(self):
self.state.changeUp()
self.rect = self.state.getImg().get_rect()
self.rect.move(self.pos)
def changeRight(self):
self.state.changeRight()
self.rect = self.state.getImg().get_rect()
self.rect.move(self.pos)
def changeLeft(self):
self.state.changeLeft()
self.rect = self.state.getImg().get_rect()
self.rect.move(self.pos)
def changeStopped(self):
self.state.changeStopped()
self.rect = self.state.getImg().get_rect()
self.rect.move(self.pos)
def changePoweringUp(self):
if(self.state.isAttackingLeft()):
self.pos = (self.pos[0] + 15, self.pos[1])
self.pos = (self.pos[0] - 40, self.pos[1] - 40)
self.counter = 0
self.lock()
#self.state.changeStopped()
self.state = self.superStateFly.getPoweringUp()
def changeSuperSaiyan(self):
self.state = self.superStateFly.getSuperSaiyan()
self.state.changeStopped()
def changeNormal(self):
self.state = self.superStateFly.getNormal()
self.unlock()
self.state.changeStopped()
def changeAttackRight(self):
if(self.state.isAttackingLeft()):
self.pos = (self.pos[0] + 15, self.pos[1])
self.counter = 0
self.state.changeAttackingRight()
self.rect = self.state.getImg().get_rect()
self.rect.move(self.pos)
def changeAttackLeft(self):
if(self.state.isAttackingLeft()):
self.pos = (self.pos[0] + 15, self.pos[1])
self.counter = 0
self.state.changeAttackingLeft()
self.rect = self.state.getImg().get_rect()
self.rect.move(self.pos)
def hurt(self, damage):
self.life -= damage
if self.life == 0:
self.game.die()
def isAttackingLeft(self):
return self.state.isAttackingLeft()
def isPoweringUp(self):
return self.state.isPoweringUp()
def getWidth(self):
return self.width
def getHeight(self):
return self.height
def addBall(self, direction, displacement):
ball_pos = (self.pos[0] + displacement[0], self.pos[1] + displacement[1])
ball = self.factory.makeBall(self.game, direction, ball_pos)
self.game.addBall(ball_pos, direction)
def lock(self):
self.game.lock()
def unlock(self):
self.game.unlock()
def interact(self):
currentCell = self.game.getCell(self.pos)
currentCell.interact(self)
def addKey(self):
self.bag.addKey()
def useKey(self):
return self.bag.useKey()
def hasKey(self):
return self.bag.hasKey()
def addLife(self):
self.life += 1
print self.life
def getLife(self):
return self.life
def addRedMedal(self):
self.bag.addRedMedal()
def addBlueMedal(self):
self.bag.addBlueMedal()
def addGoldMedal(self):
self.bag.addGoldMedal()
def paintMedalImages(self):
self.bag.paintMedalImages(self.game)
def getState(self):
return self.state
def getBasicState(self):
return self.state.getState()
def getVelocity(self):
return self.state.getVelocity()