Agregado disparo para Tajundra
[tajundrathegame.git] / qwesprites.py
blob957828d17dc5785538077517c54534f9c558c660
1 # Quick W Engine
2 # encoding: UTF-8
3 import pygame
6 class qweSprite(pygame.sprite.Sprite):
7 dx=dy=x=y=0
8 w=h=32
9 alive=True
10 in_ground=False
11 FRICTION_AIR=(0.2,0.2)
12 FRICTION_FLOOR=(8,8)
13 FRICTION_CEIL=(1,4)
14 GRAVITY=5
15 BOUNCE=0
18 def __init__(self, stage):
19 pygame.sprite.Sprite.__init__(self)
21 def move(self,x,y):
22 self.x+=x
23 self.y+=y
25 self.rect=self.x,self.y,self.w,self.h
27 def selectImage(self):
28 return self.image
30 def draw(self,screen):
31 screen.blit(self.selectImage(), self.rect)
33 def tickms(self, ms, stage):
34 rect1=pygame.Rect(self.rect)
36 self.checkpos(stage,ms)
37 self.move(self.dx*ms/1000.0,self.dy*ms/1000.0)
38 self.dx/=1+self.FRICTION_AIR[0]*ms/1000.0
39 self.dy/=1+self.FRICTION_AIR[1]*ms/1000.0
41 rect2=self.rect
43 return [rect1.union(rect2)]
46 def checkcell(self,stage,px,py):
47 sx,sy=stage.BlockSize
48 x,y,w,h=self.rect
49 Pantalla=stage.StageData
51 if px>0: x0=x+px
52 elif px<0: x0=x+w+px
53 else: x0=x+w/2
55 if py>0: y0=y+py
56 elif py<0: y0=y+h+py
57 else: y0=y+h/2
60 x0=int(x0)/sx
61 y0=int(y0)/sy
62 if y0<0 and x0>=0 and x0<len(Pantalla[0]): return ' '
63 if y0>=0 and y0<len(Pantalla):
64 if x0>=0 and x0<len(Pantalla[y0]):
65 return Pantalla[y0][x0]
67 return 't'
69 def checkpos(self,stage,ms):
70 # Colisión Horizontal
71 if ( (self.checkcell(stage,-6,0)!=' ' and self.dx>=0) or
72 (self.checkcell(stage,-9,12)!=' ' and self.dx>=0) or
73 (self.checkcell(stage,-9,-12)!=' ' and self.dx>=0) ) :
74 self.move(-self.dx*ms/1000.0-.1,0)
75 if (self.BOUNCE>0): self.dx*=-self.BOUNCE
76 else: self.dx/=1-self.BOUNCE
78 if ( (self.checkcell(stage,+6,0)!=' ' and self.dx<=0) or
79 (self.checkcell(stage,+9,12)!=' ' and self.dx<=0) or
80 (self.checkcell(stage,+9,-12)!=' ' and self.dx<=0) ) :
81 self.move(-self.dx*ms/1000.0+.1,0)
82 if (self.BOUNCE>0): self.dx*=-self.BOUNCE
83 else: self.dx/=1-self.BOUNCE
85 #if ( (self.checkcell(stage,-4,0)!=' ' and self.dx>0) or
86 # (self.checkcell(stage,+4,0)!=' ' and self.dx<0) ) :
87 # self.dx/=1+5*ms/1000.0
90 # Control de hundimiento
91 if self.dy>=0:
92 if (self.checkcell(stage,+12,-3)!=' '
93 or self.checkcell(stage,-12,-3)!=' '):
94 self.move(0,-1)
96 # Colisión de techo
97 elif self.checkcell(stage,0,+1)!=' ' and self.dy<0:
98 self.move(0,-self.dy*ms/1000.0)
99 if (self.BOUNCE>0): self.dy*=-self.BOUNCE
100 else: self.dy/=1-self.BOUNCE
101 self.dx/=1+self.FRICTION_CEIL[0]*ms/1000.0
102 self.dy/=1+self.FRICTION_CEIL[1]*ms/1000.0
103 if self.checkcell(stage,0,+2)!=' ' and self.dy<0:
104 self.dx/=1+self.FRICTION_CEIL[0]*ms/1000.0
105 self.dy/=1+self.FRICTION_CEIL[1]*ms/1000.0
106 if self.checkcell(stage,0,+3)!=' ' and self.dy<0:
107 self.dy=0
111 # Gravedad
112 if (self.checkcell(stage,+10,-1)==' '
113 and self.checkcell(stage,-10,-1)==' '):
114 self.dy+=self.GRAVITY
115 self.in_ground=False
117 # Colisión suelo
118 if (self.checkcell(stage,+10,-2)!=' '
119 or self.checkcell(stage,-10,-2)!=' '):
120 self.dx/=1+self.FRICTION_FLOOR[0]*ms/1000.0
121 if self.dy>0:
122 if (self.BOUNCE>0): self.dy*=-self.BOUNCE
123 else: self.dy/=1-self.BOUNCE
124 self.dy/=1+self.FRICTION_FLOOR[1]*ms/1000.0
125 self.move(0,-self.dy*ms/1000.0)
127 elif self.dy<0:
128 pass
130 self.in_ground=True
133 class Tajundra(qweSprite):
134 image_flip=None
135 estado=0
136 estado_ms=0
138 def __init__(self, stage):
140 qweSprite.__init__(self,stage)
141 self.image_right = pygame.image.load('tajuA1.png')
142 self.image_left=pygame.transform.flip(self.image_right,True,False)
144 self.imageb_right = pygame.image.load('tajuA2.png')
145 self.imageb_left=pygame.transform.flip(self.imageb_right,True,False)
148 self.image=self.image_right
149 self.rect = self.image.get_rect()
150 pos=stage.simbolos['@'][0]
151 del stage.simbolos['@'][0]
152 self.rect.bottomright = pos
153 self.rect=self.rect.move(stage.BlockSize)
154 self.x,self.y,self.w,self.h=self.rect
156 def selectImage(self):
157 dx=self.dx
158 if self.estado==0:
159 if dx<0:
160 self.image=self.image_left
161 else:
162 self.image=self.image_right
163 else:
164 if dx<0:
165 self.image=self.imageb_left
166 else:
167 self.image=self.imageb_right
168 self.estado_ms+=1
169 if self.estado_ms>20:
170 self.estado_ms=0
171 if self.estado==0:
172 self.estado=1
173 else:
174 self.estado=0
175 return self.image
181 class Dragon(qweSprite):
182 image_flip=None
183 estado=0
184 estado_ms=0
186 def __init__(self, stage):
188 qweSprite.__init__(self,stage)
189 self.image_left = pygame.image.load('dragonN1.png')
190 self.image_right=pygame.transform.flip(self.image_left,True,False)
192 self.imageb_left = pygame.image.load('dragonN2.png')
193 self.imageb_right=pygame.transform.flip(self.imageb_left,True,False)
196 self.image=self.image_right
197 self.rect = self.image.get_rect()
198 pos=stage.simbolos['$'][0]
199 del stage.simbolos['$'][0]
201 self.rect.bottomright = pos
202 self.rect=self.rect.move(stage.BlockSize)
203 self.x,self.y,self.w,self.h=self.rect
205 def selectImage(self):
206 dx=self.dx
207 if self.estado==0:
208 if dx<0:
209 self.image=self.image_left
210 else:
211 self.image=self.image_right
212 else:
213 if dx<0:
214 self.image=self.imageb_left
215 else:
216 self.image=self.imageb_right
217 self.estado_ms+=1
218 if self.estado_ms>20:
219 self.estado_ms=0
220 if self.estado==0:
221 self.estado=1
222 else:
223 self.estado=0
224 return self.image
228 class qweShoot(qweSprite):
229 estado=0
230 estado_ms=0
231 FRICTION_AIR=(1.0,0.0)
232 FRICTION_FLOOR=(0.0,0.0)
233 FRICTION_CEIL=(0.0,0.0)
234 GRAVITY=5
235 BOUNCE=.8
236 Life=3.0
237 fcount=0
239 def __init__(self, stage, pos,dx,dy):
241 qweSprite.__init__(self,stage)
242 self.image = pygame.image.load('hacha1.png')
243 if dx<0: self.image =pygame.transform.flip(self.image, True, False)
245 self.rect = self.image.get_rect()
247 self.rect.topleft = pos
248 self.dx=dx
249 self.dy=dy
250 self.x,self.y,self.w,self.h=self.rect
252 def tickms(self, ms, stage):
254 if (self.Life<0): self.alive=False
255 else: self.Life-=ms/1000.0
256 return qweSprite.tickms(self, ms, stage)
258 def draw(self,screen):
259 self.fcount+=1
260 if self.fcount>10/(self.Life+1):
261 self.fcount=4
262 screen.blit(self.selectImage(), self.rect)
265 class Hacha(qweShoot):
266 FRICTION_AIR=(1.0,0.0)
267 BOUNCE=.8
268 Life=3.0
269 fcountrot=0
270 angle=0
272 def __init__(self, stage, pos,dx,dy):
274 qweSprite.__init__(self,stage)
275 self.image1 = pygame.image.load('hacha1.png')
276 self.image = self.image1.copy()
277 self.rect = self.image.get_rect()
279 self.rect.topleft = pos
280 self.dx=dx
281 self.dy=dy
282 self.x,self.y,self.w,self.h=self.rect
284 def draw(self,screen):
285 self.fcountrot+=1
286 if self.fcountrot>5/self.Life:
287 self.image=pygame.transform.rotate(self.image1, self.angle)
288 self.angle-=90
289 self.fcountrot=0
290 qweShoot.draw(self,screen)