8 class qweSprite(pygame
.sprite
.Sprite
):
14 FRICTION_AIR
=(0.2,0.2)
22 ShowLife
=MaxLife
=Life
=240
37 LifeCollisionSpriteCost
=2
49 def __init__(self
, stage
):
50 pygame
.sprite
.Sprite
.__init
__(self
)
57 self
.rect
=pygame
.Rect(self
.x
,self
.y
,self
.w
,self
.h
)
59 def selectImage(self
):
63 Life(screen
,(int(self
.x
),int(self
.y
)),self
.DIE_IMG
,self
.MaxLife
/4.0)
66 def draw(self
,screen
,list_rects
=None):
69 if self
.rect
.collidelist(list_rects
)==.1: return
71 if self
.fcount
>10/(self
.Life
+1):
73 screen
.blit(self
.selectImage(), self
.rect
)
74 if self
.ShowLife
>self
.Life
or (self
.Life
< 60 and self
.ShowLife
>0):
75 if self
.ShowLife
<0: self
.ShowLife
=0
77 pp
=100.0*self
.ShowLife
/self
.MaxLife
78 pp
=pp
+math
.log(1+1.9*self
.ShowLife
/self
.MaxLife
)*100.0
99 pygame
.draw
.line(screen
, (255-p
,255,0) , p1a
, p2a
,1)
100 pygame
.draw
.line(screen
, (255-p
,255,0) , p1
, p2
,1)
102 pygame
.draw
.line(screen
, (255,p
,0) , p1
, p2
,1)
104 def tickms(self
, ms
, stage
):
108 d
=math
.sqrt(self
.dx
*self
.dx
+self
.dy
*self
.dy
)
110 self
.accum_dist
+=d
*ms
111 if self
.ShowLife
>self
.Life
:
112 self
.accum_dist
+=(self
.ShowLife
-self
.Life
)
115 self
.accum_dist
+=10/(self
.Life
+0.1)
117 if self
.accum_ms
>self
.MAX_ACCUMMS
or \
118 self
.accum_dist
>self
.MAX_ACCUMDIST
:
123 rect1
=pygame
.Rect(self
.rect
)
124 if (self
.ItemTime
>0):
135 if self
.ShowLife
>self
.Life
:
136 self
.ShowLife
-=ms
/100.0
137 if self
.ShowLife
<self
.Life
:
138 self
.ShowLife
=self
.Life
140 self
.checkpos(stage
,ms
)
141 self
.move(self
.dx
*ms
/1000.0,self
.dy
*ms
/1000.0)
142 self
.dx
/=1+self
.FRICTION_AIR
[0]*ms
/1000.0
143 self
.dy
/=1+self
.FRICTION_AIR
[1]*ms
/1000.0
146 self
.psx
,self
.psy
=self
.getpspoint(stage
,0,0);
148 return [rect1
.union(rect2
)]
153 def getpspoint(self
,stage
,px
,py
):
154 sx
,sy
=stage
.BlockSize
170 def collision(self
,stage
,x
,y
,char
):
172 Pantalla
=stage
.StageData
176 self
.LevelFinished
=True
185 if char
=='Z' and self
.Item
=='z' and stage
.getCh(x
,y
-1)==' ':
186 stage
.setCh(x
,y
-1,'z')
192 def checkcell(self
,stage
,px
,py
):
193 Pantalla
=stage
.StageData
195 x0
,y0
=self
.getpspoint(stage
,px
,py
);
197 if y0
<0 and x0
>=0 and x0
<len(Pantalla
[0]): return ' '
198 if y0
>=0 and y0
<len(Pantalla
):
199 if x0
>=0 and x0
<len(Pantalla
[y0
]):
201 self
.collision(stage
,x0
,y0
,ch
)
207 def checkpos(self
,stage
,ms
):
210 if self
.Life
<=0.5: return
211 for sprite
in stage
.sprites
:
212 if sprite
!=self
and sprite
.RADIUS
>0 and sprite
.Life
>0.5:
213 if ( math
.fabs(sprite
.psx
-self
.psx
)<=1
214 and math
.fabs(sprite
.psy
-self
.psy
)<=1):
216 sx
,sy
,sw
,sh
=sprite
.rect
218 min_d
=self
.RADIUS
+sprite
.RADIUS
221 d1
=math
.sqrt(ax
*ax
+ay
*ay
)
229 d2
=math
.sqrt(ax
*ax
+ay
*ay
)
233 if sprite
.SOLID
and self
.FSOLID
:
237 if self
.SOLID
and sprite
.FSOLID
:
242 self
.Life
-=sprite
.LifeCollisionSpriteCost
244 self
.ShowLife
=self
.Life
+10
247 sprite
.Life
-=self
.LifeCollisionSpriteCost
248 if sprite
.ShowLife
>0:
249 sprite
.ShowLife
=sprite
.Life
+10
256 d1
=math
.sqrt(ax
*ax
+ay
*ay
)+1
257 if self
.SOLID
and sprite
.FSOLID
:
261 if sprite
.SOLID
and self
.FSOLID
:
267 # Colisión Horizontal
268 if ( (self
.checkcell(stage
,self
.MARGIN_RIGHT
,0)!=' ' and self
.dx
>=0) or
269 (self
.checkcell(stage
,self
.MARGIN_RIGHT
,12)!=' ' and self
.dx
>=0) or
270 (self
.checkcell(stage
,self
.MARGIN_RIGHT
,-12)!=' ' and self
.dx
>=0) ) :
271 if self
.Life
<900: self
.Life
-=self
.LifeCollisionCost
272 self
.move(-self
.dx
*ms
/1000.0-.1,0)
273 if (self
.BOUNCE
>0): self
.dx
*=-self
.BOUNCE
274 else: self
.dx
/=1-self
.BOUNCE
276 if ( (self
.checkcell(stage
,self
.MARGIN_LEFT
,0)!=' ' and self
.dx
<=0) or
277 (self
.checkcell(stage
,self
.MARGIN_LEFT
,12)!=' ' and self
.dx
<=0) or
278 (self
.checkcell(stage
,self
.MARGIN_LEFT
,-12)!=' ' and self
.dx
<=0) ) :
279 if self
.Life
<900: self
.Life
-=self
.LifeCollisionCost
280 self
.move(-self
.dx
*ms
/1000.0+.1,0)
281 if (self
.BOUNCE
>0): self
.dx
*=-self
.BOUNCE
282 else: self
.dx
/=1-self
.BOUNCE
284 #if ( (self.checkcell(stage,-4,0)!=' ' and self.dx>0) or
285 # (self.checkcell(stage,+4,0)!=' ' and self.dx<0) ) :
286 # self.dx/=1+5*ms/1000.0
289 # Control de hundimiento
291 if (self
.checkcell(stage
,self
.MARGIN_LEFT
+4,self
.MARGIN_BOTTOM
-1)!=' '
292 or self
.checkcell(stage
,self
.MARGIN_RIGHT
-4,self
.MARGIN_BOTTOM
-1)!=' '):
298 if (self
.checkcell(stage
,self
.MARGIN_LEFT
+4,self
.MARGIN_TOP
)!=' ' or
299 self
.checkcell(stage
,self
.MARGIN_RIGHT
-4,self
.MARGIN_TOP
)!=' ' or
300 self
.checkcell(stage
,0,self
.MARGIN_TOP
)!=' '):
302 if self
.Life
<900: self
.Life
-=self
.LifeCollisionCost
303 self
.move(0,-self
.dy
*ms
/1000.0)
304 if (self
.BOUNCE
>0): self
.dy
*=-self
.BOUNCE
305 else: self
.dy
/=1-self
.BOUNCE
306 self
.dx
/=1+self
.FRICTION_CEIL
[0]*ms
/1000.0
307 self
.dy
/=1+self
.FRICTION_CEIL
[1]*ms
/1000.0
308 if self
.checkcell(stage
,0,+2)!=' ' and self
.dy
<0:
309 self
.dx
/=1+self
.FRICTION_CEIL
[0]*ms
/1000.0
310 self
.dy
/=1+self
.FRICTION_CEIL
[1]*ms
/1000.0
311 if self
.checkcell(stage
,0,+3)!=' ' and self
.dy
<0:
317 if (self
.checkcell(stage
,self
.MARGIN_LEFT
+4,self
.MARGIN_BOTTOM
)!=' '
318 or self
.checkcell(stage
,self
.MARGIN_RIGHT
-4,self
.MARGIN_BOTTOM
)!=' '):
319 self
.dx
/=1+self
.FRICTION_FLOOR
[0]*ms
/1000.0
321 if self
.Life
<900: self
.Life
-=self
.LifeCollisionCost
322 if (self
.BOUNCE
>0): self
.dy
*=-self
.BOUNCE
323 else: self
.dy
/=1-self
.BOUNCE
324 self
.dy
/=1+self
.FRICTION_FLOOR
[1]*ms
/1000.0
325 self
.move(0,-self
.dy
*ms
/1000.0)
333 self
.dy
+=self
.GRAVITY
335 if self
.Life
<0.5: self
.Life
=0.5
336 if self
.Life
>self
.MaxLife
*1.5: self
.Life
=self
.MaxLife
*1.5
338 class qweText(qweSprite
):
344 LifeCollisionSpriteCost
=0
348 def __init__(self
, stage
,x
,y
,txt
,font
,color
):
350 qweSprite
.__init
__(self
,stage
)
353 self
.image
=font
.render(txt
, True, color
)
355 self
.rect
= self
.image
.get_rect()
357 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
361 def collision(self
,stage
,x
,y
,char
):
364 def checkcell(self
,stage
,px
,py
):
367 def checkpos(self
,stage
,ms
):
371 global qweTextBasic_font
372 qweTextBasic_font
=None
373 global qweTextBIG_font
376 class qweTextBasic(qweText
):
377 def __init__(self
, stage
,x
,y
,txt
):
378 global qweTextBasic_font
379 if not qweTextBasic_font
:
380 qweTextBasic_font
=pygame
.font
.Font(None,15)
383 qweText
.__init
__(self
,stage
,x
,y
,txt
,qweTextBasic_font
,color
)
385 class qweTextBIG(qweText
):
386 def __init__(self
, stage
,x
,y
,txt
):
387 qweSprite
.__init
__(self
,stage
)
388 global qweTextBIG_font
389 if not qweTextBIG_font
:
390 qweTextBIG_font
=pygame
.font
.Font(None,64)
392 self
.FONT
=qweTextBIG_font
394 self
.image1
=qweTextBIG_font
.render(txt
, True, (0,0,0))
395 self
.image2
=qweTextBIG_font
.render(txt
, True, (255,255,255))
396 w
,h
= self
.image1
.get_size()
399 self
.image
=pygame
.Surface((w
,h
),pygame
.SRCALPHA
).convert_alpha()
400 self
.image
.fill((0,0,0,0))
401 self
.image
.blit(self
.image1
,(5,5))
402 self
.image
.blit(self
.image2
,(0,0))
405 self
.rect
= self
.image
.get_rect()
407 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
412 class Tajundra(qweSprite
):
423 DIE_IMG
='platano.png'
427 def __init__(self
, stage
):
429 qweSprite
.__init
__(self
,stage
)
430 self
.image_right
= pygame
.image
.load('tajuA1.png')
431 self
.image_left
=pygame
.transform
.flip(self
.image_right
,True,False)
433 self
.imageb_right
= pygame
.image
.load('tajuA2.png')
434 self
.imageb_left
=pygame
.transform
.flip(self
.imageb_right
,True,False)
437 self
.image
=self
.image_right
438 self
.rect
= self
.image
.get_rect()
439 pos
=stage
.simbolos
['@'][0]
440 del stage
.simbolos
['@'][0]
441 self
.rect
.bottomright
= pos
442 self
.rect
=self
.rect
.move(stage
.BlockSize
)
443 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
445 def selectImage(self
):
449 self
.image
=self
.image_left
451 self
.image
=self
.image_right
454 self
.image
=self
.imageb_left
456 self
.image
=self
.imageb_right
458 if self
.estado_ms
>20:
470 class Dragon(qweSprite
):
477 def __init__(self
, stage
):
479 qweSprite
.__init
__(self
,stage
)
480 self
.image_left
= pygame
.image
.load('dragonN1.png')
481 self
.image_right
=pygame
.transform
.flip(self
.image_left
,True,False)
483 self
.imageb_left
= pygame
.image
.load('dragonN2.png')
484 self
.imageb_right
=pygame
.transform
.flip(self
.imageb_left
,True,False)
487 self
.image
=self
.image_right
488 self
.rect
= self
.image
.get_rect()
489 pos
=stage
.simbolos
['$'][0]
490 del stage
.simbolos
['$'][0]
492 self
.rect
.bottomright
= pos
493 self
.rect
=self
.rect
.move(stage
.BlockSize
)
494 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
496 def selectImage(self
):
500 self
.image
=self
.image_left
502 self
.image
=self
.image_right
505 self
.image
=self
.imageb_left
507 self
.image
=self
.imageb_right
509 if self
.estado_ms
>20:
519 class LanzaBolas(qweSprite
):
527 DIE_IMG
='safanoria.png'
528 def __init__(self
, stage
):
530 qweSprite
.__init
__(self
,stage
)
531 self
.image
= pygame
.image
.load('lanzabolas1.png')
533 self
.rect
= self
.image
.get_rect()
534 pos
=stage
.simbolos
['|'][0]
535 del stage
.simbolos
['|'][0]
537 self
.rect
.bottomright
= pos
538 self
.rect
=self
.rect
.move(stage
.BlockSize
)
539 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
540 self
.estado_ms
=random
.randint(6000,12000)
542 def selectImage(self
):
545 def tickms(self
, ms
, stage
):
547 v
=qweSprite
.tickms(self
, ms
, stage
)
549 if self
.estado_ms
>10000 and self
.Life
>10:
551 self
.estado_ms
=random
.randint(0,1000)
552 r
=random
.randint(-1000,1000)/3.0
553 disparo
=Bola(stage
,(int(self
.x
),int(self
.y
)),r
,-200)
561 class qweShoot(qweSprite
):
564 FRICTION_AIR
=(0.0,0.0)
565 FRICTION_FLOOR
=(0.0,0.0)
566 FRICTION_CEIL
=(0.0,0.0)
572 LifeCollisionSpriteCost
=10
582 def __init__(self
, stage
, pos
,dx
,dy
):
584 qweSprite
.__init
__(self
,stage
)
585 self
.image
= pygame
.image
.load('bola1.png')
587 self
.rect
= self
.image
.get_rect()
589 self
.rect
.topleft
= pos
592 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
593 d
=math
.sqrt(dx
*dx
+dy
*dy
)+5
594 self
.tickms(1000/d
,stage
);
596 def collision(self
,stage
,x
,y
,char
):
597 qweSprite
.collision(self
,stage
,x
,y
,char
)
601 if char
=='M': nchar
='N'
602 if char
=='N': nchar
='m'
603 if char
=='m': nchar
='n'
604 if char
=='n': nchar
=' '
607 stage
.setCh(x
,y
,nchar
)
613 def die(self
,screen
):
616 class Bola(qweShoot
):
625 class Hacha(qweShoot
):
626 FRICTION_AIR
=(0.5,0.0)
630 LifeCollisionCost
=0.1
631 LifeCollisionSpriteCost
=10
644 def __init__(self
, stage
, pos
,dx
,dy
):
646 qweSprite
.__init
__(self
,stage
)
647 self
.image1
= pygame
.image
.load('hacha1.png')
648 if dx
<0: self
.image1
=pygame
.transform
.flip(self
.image1
, True, False)
649 self
.image
= self
.image1
.copy()
650 self
.rect
= self
.image
.get_rect()
652 self
.rect
.topleft
= pos
655 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
658 def draw(self
,screen
,lr
=None):
660 if self
.Life
>0 and self
.fcountrot
>5/self
.Life
:
661 self
.image
=pygame
.transform
.rotate(self
.image1
, self
.angle
)
672 qweShoot
.draw(self
,screen
,lr
)
674 def tickms(self
, ms
, stage
):
678 self
.time_elapsed
+=ms
679 if self
.time_elapsed
<50:
684 v
=qweSprite
.tickms(self
, ms
, stage
)
687 def collision(self
,stage
,x
,y
,char
):
688 qweShoot
.collision(self
,stage
,x
,y
,char
)
690 if char
=='B' and x
>self
.psx
and self
.dx
>0 \
691 and stage
.getCh(x
+1,y
)==' ' and stage
.getCh(x
+1,y
+1)!=' ':
693 stage
.setCh(x
+1,y
,'B')
696 if char
=='B' and x
<self
.psx
and self
.dx
<0 \
697 and stage
.getCh(x
-1,y
)==' ' and stage
.getCh(x
-1,y
+1)!=' ':
699 stage
.setCh(x
-1,y
,'B')
703 class Life(qweShoot
):
704 FRICTION_AIR
=(10.0,10.0)
706 ShowLife
=MaxLife
=Life
=10
708 LifeCollisionSpriteCost
=-2
721 def __init__(self
, stage
, pos
,imgfile
, life
):
723 qweSprite
.__init
__(self
,stage
)
725 self
.image1
= pygame
.image
.load(imgfile
)
726 self
.image
= self
.image1
.copy()
727 self
.rect
= self
.image
.get_rect()
728 self
.ShowLife
=self
.MaxLife
=self
.Life
=life
730 self
.rect
.topleft
= pos
733 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
734 self
.tickms(5,stage
);
736 def tickms(self
, ms
, stage
):
738 self
.RADIUS
+=ms
/500.0
740 v
=qweSprite
.tickms(self
, ms
, stage
)
741 if self
.psx
<0: self
.move(8,0)
742 if self
.psx
>=20: self
.move(-8,0)
744 if self
.psy
<0: self
.move(0,8)
745 if self
.psy
>=15: self
.move(0,-8)