8 class qweSprite(pygame
.sprite
.Sprite
):
14 FRICTION_AIR
=(0.2,0.2)
22 ShowLife
=MaxLife
=Life
=240
39 LifeCollisionSpriteCost
=2
51 def __init__(self
, stage
):
52 pygame
.sprite
.Sprite
.__init
__(self
)
53 if self
.Layer
==1: stage
.sprites
+=[self
]
54 if self
.Layer
==2: stage
.background_sprites
+=[self
]
60 self
.accum_dist
+=d
*1000.0
62 self
.rect
=pygame
.Rect(self
.x
,self
.y
,self
.w
,self
.h
)
64 def selectImage(self
):
68 Life(screen
,(int(self
.x
),int(self
.y
)),self
.DIE_IMG
,self
.MaxLife
/10.0)
71 def draw(self
,screen
,list_rects
=None):
74 if self
.rect
.collidelist(list_rects
)==.1: return
76 if self
.fcount
>10/(self
.Life
+1):
78 screen
.blit(self
.selectImage(), self
.rect
)
79 if self
.ShowLife
>self
.Life
or (self
.Life
< 60 and self
.ShowLife
>0):
80 if self
.ShowLife
<0: self
.ShowLife
=0
82 pp
=100.0*self
.ShowLife
/self
.MaxLife
83 pp
=pp
+math
.log(1+1.9*self
.ShowLife
/self
.MaxLife
)*100.0
104 pygame
.draw
.line(screen
, (255-p
,255,0) , p1a
, p2a
,1)
105 pygame
.draw
.line(screen
, (255-p
,255,0) , p1
, p2
,1)
107 pygame
.draw
.line(screen
, (255,p
,0) , p1
, p2
,1)
109 def tickms(self
, ms
, stage
):
113 rect1
=pygame
.Rect(self
.rect
)
114 d
=math
.sqrt(self
.dx
*self
.dx
+self
.dy
*self
.dy
)
116 #self.accum_dist+=d*self.accum_ms
117 if self
.ShowLife
>self
.Life
:
118 self
.accum_dist
+=(self
.ShowLife
-self
.Life
)
121 self
.accum_dist
+=10/(self
.Life
+0.1)
123 if self
.accum_ms
>self
.MAX_ACCUMMS
or \
124 self
.accum_dist
+d
*self
.accum_ms
>self
.MAX_ACCUMDIST
:
129 if (self
.ItemTime
>0):
138 if (self
.REGEN
<0 or self
.Life
<self
.MaxLife
):
139 self
.Life
+=self
.REGEN
*ms
/1000.0
141 if self
.ShowLife
>self
.Life
:
142 self
.ShowLife
-=ms
/100.0
143 if self
.ShowLife
<self
.Life
:
144 self
.ShowLife
=self
.Life
146 if d
*self
.accum_ms
>1000: factor
=d
*self
.accum_ms
/1000.0
149 if (iter_ms
>self
.MAX_ITERMS
/factor
): iter_ms
=self
.MAX_ITERMS
/factor
151 self
.checkpos(stage
,iter_ms
)
152 self
.move(self
.dx
*iter_ms
/1000.0,self
.dy
*iter_ms
/1000.0)
153 self
.dx
/=1+self
.FRICTION_AIR
[0]*iter_ms
/1000.0
154 self
.dy
/=1+self
.FRICTION_AIR
[1]*iter_ms
/1000.0
158 self
.psx
,self
.psy
=self
.getpspoint(stage
,0,0);
160 return [rect1
.union(rect2
)]
165 def getpspoint(self
,stage
,px
,py
):
166 sx
,sy
=stage
.BlockSize
182 def collision(self
,stage
,x
,y
,char
):
184 Pantalla
=stage
.StageData
188 self
.LevelFinished
=True
197 if char
=='Z' and self
.Item
=='z' and stage
.getCh(x
,y
-1)==' ':
198 testrect
=pygame
.Rect(x
*32+8,(y
-1)*32+8,16,16)
201 for sprite
in stage
.sprites
:
202 if sprite
.FSOLID
and sprite
.rect
.colliderect(testrect
):
206 stage
.setCh(x
,y
-1,'z')
212 def checkcell(self
,stage
,px
,py
):
213 Pantalla
=stage
.StageData
215 x0
,y0
=self
.getpspoint(stage
,px
,py
);
217 if y0
<0 and x0
>=0 and x0
<len(Pantalla
[0]): return ' '
218 if y0
>=0 and y0
<len(Pantalla
):
219 if x0
>=0 and x0
<len(Pantalla
[y0
]):
221 self
.collision(stage
,x0
,y0
,ch
)
227 def checkpos(self
,stage
,ms
):
234 for sprite
in stage
.sprites
:
235 if sprite
!=self
and sprite
.RADIUS
>0 and sprite
.Life
>0.5:
236 if ( math
.fabs(sprite
.psx
-self
.psx
)<=1
237 and math
.fabs(sprite
.psy
-self
.psy
)<=1):
239 sx
,sy
,sw
,sh
=sprite
.rect
241 min_d
=self
.RADIUS
+sprite
.RADIUS
244 d1
=math
.sqrt(ax
*ax
+ay
*ay
)
252 d2
=math
.sqrt(ax
*ax
+ay
*ay
)
256 if sprite
.SOLID
and self
.FSOLID
:
260 if self
.SOLID
and sprite
.FSOLID
:
265 self
.Life
-=sprite
.LifeCollisionSpriteCost
266 if self
.Life
<0.5: self
.Life
=0.5
268 self
.ShowLife
=self
.Life
+10
271 sprite
.Life
-=self
.LifeCollisionSpriteCost
274 if sprite
.ShowLife
>0:
275 sprite
.ShowLife
=sprite
.Life
+10
282 d1
=math
.sqrt(ax
*ax
+ay
*ay
)+1
283 if self
.SOLID
and sprite
.FSOLID
:
287 if sprite
.SOLID
and self
.FSOLID
:
293 # ColisiĆ³n Horizontal
294 if ( (self
.checkcell(stage
,self
.MARGIN_RIGHT
,0)!=' ' and self
.dx
>=0) or
295 (self
.checkcell(stage
,self
.MARGIN_RIGHT
,12)!=' ' and self
.dx
>=0) or
296 (self
.checkcell(stage
,self
.MARGIN_RIGHT
,-12)!=' ' and self
.dx
>=0) ) :
297 if self
.Life
<900: self
.Life
-=self
.LifeCollisionCost
298 self
.move(-self
.dx
*ms
/1000.0-.1,0)
299 if (self
.BOUNCE
>0): self
.dx
*=-self
.BOUNCE
300 else: self
.dx
/=1-self
.BOUNCE
302 if ( (self
.checkcell(stage
,self
.MARGIN_LEFT
,0)!=' ' and self
.dx
<=0) or
303 (self
.checkcell(stage
,self
.MARGIN_LEFT
,12)!=' ' and self
.dx
<=0) or
304 (self
.checkcell(stage
,self
.MARGIN_LEFT
,-12)!=' ' and self
.dx
<=0) ) :
305 if self
.Life
<900: self
.Life
-=self
.LifeCollisionCost
306 self
.move(-self
.dx
*ms
/1000.0+.1,0)
307 if (self
.BOUNCE
>0): self
.dx
*=-self
.BOUNCE
308 else: self
.dx
/=1-self
.BOUNCE
310 if ( (self
.checkcell(stage
,self
.MARGIN_RIGHT
-3,0)!=' ') and
311 (self
.checkcell(stage
,self
.MARGIN_RIGHT
-3,12)!=' ') and
312 (self
.checkcell(stage
,self
.MARGIN_RIGHT
-3,-12)!=' ') ) :
315 if ( (self
.checkcell(stage
,self
.MARGIN_LEFT
+3,0)!=' ') and
316 (self
.checkcell(stage
,self
.MARGIN_LEFT
+3,12)!=' ') and
317 (self
.checkcell(stage
,self
.MARGIN_LEFT
+3,-12)!=' ') ) :
321 # Control de hundimiento
323 if (self
.checkcell(stage
,self
.MARGIN_LEFT
+4,self
.MARGIN_BOTTOM
-1)!=' '
324 or self
.checkcell(stage
,self
.MARGIN_RIGHT
-4,self
.MARGIN_BOTTOM
-1)!=' ') and \
325 (self
.checkcell(stage
,self
.MARGIN_LEFT
+4,self
.MARGIN_TOP
+1)==' '
326 and self
.checkcell(stage
,self
.MARGIN_RIGHT
-4,self
.MARGIN_TOP
+1)==' '):
330 if (self
.checkcell(stage
,0,0)!=' '):
335 if (self
.checkcell(stage
,self
.MARGIN_LEFT
+4,self
.MARGIN_TOP
)!=' ' or
336 self
.checkcell(stage
,self
.MARGIN_RIGHT
-4,self
.MARGIN_TOP
)!=' ' or
337 self
.checkcell(stage
,0,self
.MARGIN_TOP
)!=' '):
339 if self
.Life
<900: self
.Life
-=self
.LifeCollisionCost
340 self
.move(0,-self
.dy
*ms
/1000.0)
341 if (self
.BOUNCE
>0): self
.dy
*=-self
.BOUNCE
342 else: self
.dy
/=1-self
.BOUNCE
343 self
.dx
/=1+self
.FRICTION_CEIL
[0]*ms
/1000.0
344 self
.dy
/=1+self
.FRICTION_CEIL
[1]*ms
/1000.0
345 if self
.checkcell(stage
,0,+2)!=' ' and self
.dy
<0:
346 self
.dx
/=1+self
.FRICTION_CEIL
[0]*ms
/1000.0
347 self
.dy
/=1+self
.FRICTION_CEIL
[1]*ms
/1000.0
348 if self
.checkcell(stage
,0,+3)!=' ' and self
.dy
<0:
354 if ((self
.checkcell(stage
,self
.MARGIN_LEFT
+4,self
.MARGIN_BOTTOM
)!=' '
355 and self
.checkcell(stage
,0,self
.MARGIN_BOTTOM
)!=' ')
356 or (self
.checkcell(stage
,self
.MARGIN_RIGHT
-4,self
.MARGIN_BOTTOM
)!=' '
357 and self
.checkcell(stage
,0,self
.MARGIN_BOTTOM
)!=' ')
359 self
.dx
/=1+self
.FRICTION_FLOOR
[0]*ms
/1000.0
361 if self
.Life
<900: self
.Life
-=self
.LifeCollisionCost
362 if (self
.BOUNCE
>0): self
.dy
*=-self
.BOUNCE
363 else: self
.dy
/=1-self
.BOUNCE
364 self
.dy
/=1+self
.FRICTION_FLOOR
[1]*ms
/1000.0
365 self
.move(0,-self
.dy
*ms
/1000.0)
373 self
.dy
+=self
.GRAVITY
375 if self
.Life
>self
.MaxLife
*1.5: self
.Life
=self
.MaxLife
*1.5
377 class qweText(qweSprite
):
383 LifeCollisionSpriteCost
=0
389 def __init__(self
, stage
,x
,y
,txt
,font
,color
):
391 qweSprite
.__init
__(self
,stage
)
394 self
.image
=font
.render(txt
, True, color
)
396 self
.rect
= self
.image
.get_rect()
398 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
402 def collision(self
,stage
,x
,y
,char
):
405 def checkcell(self
,stage
,px
,py
):
408 def checkpos(self
,stage
,ms
):
412 global qweTextBasic_font
413 qweTextBasic_font
=None
414 global qweTextBIG_font
417 class qweTextBasic(qweText
):
418 def __init__(self
, stage
,x
,y
,txt
):
419 global qweTextBasic_font
420 if not qweTextBasic_font
:
421 qweTextBasic_font
=pygame
.font
.Font(None,15)
425 qweText
.__init
__(self
,stage
,x
,y
,txt
,qweTextBasic_font
,color
)
427 class qweTextBIG(qweText
):
428 def __init__(self
, stage
,x
,y
,txt
):
429 qweSprite
.__init
__(self
,stage
)
430 global qweTextBIG_font
431 if not qweTextBIG_font
:
432 qweTextBIG_font
=pygame
.font
.Font(None,64)
434 self
.FONT
=qweTextBIG_font
436 self
.image1
=qweTextBIG_font
.render(txt
, True, (0,0,0))
437 self
.image2
=qweTextBIG_font
.render(txt
, True, (255,255,255))
438 w
,h
= self
.image1
.get_size()
441 self
.image
=pygame
.Surface((w
,h
),pygame
.SRCALPHA
).convert_alpha()
442 self
.image
.fill((0,0,0,0))
443 self
.image
.blit(self
.image1
,(5,5))
444 self
.image
.blit(self
.image2
,(0,0))
447 self
.rect
= self
.image
.get_rect()
449 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
454 class Tajundra(qweSprite
):
465 DIE_IMG
='platano.png'
470 def __init__(self
, stage
):
472 qweSprite
.__init
__(self
,stage
)
473 self
.image_right
= pygame
.image
.load('tajuA1.png')
474 self
.image_left
=pygame
.transform
.flip(self
.image_right
,True,False)
476 self
.imageb_right
= pygame
.image
.load('tajuA2.png')
477 self
.imageb_left
=pygame
.transform
.flip(self
.imageb_right
,True,False)
480 self
.image
=self
.image_right
481 self
.rect
= self
.image
.get_rect()
482 pos
=stage
.simbolos
['@'][0]
483 del stage
.simbolos
['@'][0]
484 self
.rect
.bottomright
= pos
485 self
.rect
=self
.rect
.move(stage
.BlockSize
)
486 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
488 def selectImage(self
):
492 self
.image
=self
.image_left
494 self
.image
=self
.image_right
497 self
.image
=self
.imageb_left
499 self
.image
=self
.imageb_right
501 if self
.estado_ms
>20:
513 class Dragon(qweSprite
):
521 def __init__(self
, stage
):
523 qweSprite
.__init
__(self
,stage
)
524 self
.image_left
= pygame
.image
.load('dragonN1.png')
525 self
.image_right
=pygame
.transform
.flip(self
.image_left
,True,False)
527 self
.imageb_left
= pygame
.image
.load('dragonN2.png')
528 self
.imageb_right
=pygame
.transform
.flip(self
.imageb_left
,True,False)
531 self
.image
=self
.image_right
532 self
.rect
= self
.image
.get_rect()
533 pos
=stage
.simbolos
['$'][0]
534 del stage
.simbolos
['$'][0]
536 self
.rect
.bottomright
= pos
537 self
.rect
=self
.rect
.move(stage
.BlockSize
)
538 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
540 def selectImage(self
):
544 self
.image
=self
.image_left
546 self
.image
=self
.image_right
549 self
.image
=self
.imageb_left
551 self
.image
=self
.imageb_right
553 if self
.estado_ms
>20:
563 class LanzaBolas(qweSprite
):
572 DIE_IMG
='safanoria.png'
573 def __init__(self
, stage
):
575 qweSprite
.__init
__(self
,stage
)
576 self
.image
= pygame
.image
.load('lanzabolas1.png')
578 self
.rect
= self
.image
.get_rect()
579 pos
=stage
.simbolos
['|'][0]
580 del stage
.simbolos
['|'][0]
582 self
.rect
.bottomright
= pos
583 self
.rect
=self
.rect
.move(stage
.BlockSize
)
584 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
585 self
.estado_ms
=random
.randint(6000,12000)
587 def selectImage(self
):
590 def tickms(self
, ms
, stage
):
592 v
=qweSprite
.tickms(self
, ms
, stage
)
594 if self
.estado_ms
>10000 and self
.Life
>10:
596 self
.estado_ms
=random
.randint(0,1000)
597 r
=random
.randint(-1000,1000)/3.0
598 disparo
=Bola(stage
,(int(self
.x
),int(self
.y
)),r
,-200)
606 class qweShoot(qweSprite
):
609 FRICTION_AIR
=(0.0,0.0)
610 FRICTION_FLOOR
=(0.0,0.0)
611 FRICTION_CEIL
=(0.0,0.0)
617 LifeCollisionSpriteCost
=10
628 def __init__(self
, stage
, pos
,dx
,dy
):
630 qweSprite
.__init
__(self
,stage
)
631 self
.image
= pygame
.image
.load('bola1.png')
633 self
.rect
= self
.image
.get_rect()
635 self
.rect
.topleft
= pos
638 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
639 d
=math
.sqrt(dx
*dx
+dy
*dy
)+5
640 self
.tickms(1000/d
,stage
);
642 def collision(self
,stage
,x
,y
,char
):
643 qweSprite
.collision(self
,stage
,x
,y
,char
)
647 if char
=='M': nchar
='N'
648 if char
=='N': nchar
='m'
649 if char
=='m': nchar
='n'
650 if char
=='n': nchar
=' '
653 stage
.setCh(x
,y
,nchar
)
659 def die(self
,screen
):
662 class Bola(qweShoot
):
671 class Hacha(qweShoot
):
672 FRICTION_AIR
=(0.5,0.0)
676 LifeCollisionCost
=0.1
677 LifeCollisionSpriteCost
=10
690 def __init__(self
, stage
, pos
,dx
,dy
):
692 qweSprite
.__init
__(self
,stage
)
693 self
.image1
= pygame
.image
.load('hacha1.png')
694 if dx
<0: self
.image1
=pygame
.transform
.flip(self
.image1
, True, False)
695 self
.image
= self
.image1
.copy()
696 self
.rect
= self
.image
.get_rect()
698 self
.rect
.topleft
= pos
701 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
704 def draw(self
,screen
,lr
=None):
706 if self
.Life
>0 and self
.fcountrot
>5/self
.Life
:
707 self
.image
=pygame
.transform
.rotate(self
.image1
, self
.angle
)
713 if self
.Life
>2 or self
.Life
<.5:
718 qweShoot
.draw(self
,screen
,lr
)
720 def tickms(self
, ms
, stage
):
724 self
.time_elapsed
+=ms
725 if self
.time_elapsed
<50:
730 v
=qweSprite
.tickms(self
, ms
, stage
)
733 def collision(self
,stage
,x
,y
,char
):
734 if self
.ItemTime
>0: return
735 qweShoot
.collision(self
,stage
,x
,y
,char
)
737 if char
=='B' and x
>self
.psx
and self
.dx
>0 \
738 and stage
.getCh(x
+1,y
)==' ': # and stage.getCh(x+1,y+1)!=' ':
742 testrect
=pygame
.Rect((x
+1)*32,(y
)*32,32,32)
744 for sprite
in stage
.sprites
:
745 if sprite
.FSOLID
and sprite
.rect
.colliderect(testrect
):
750 stage
.setCh(x
+1,y
,'B')
752 if char
=='B' and x
<self
.psx
and self
.dx
<0 \
753 and stage
.getCh(x
-1,y
)==' ': # and stage.getCh(x-1,y+1)!=' ':
757 testrect
=pygame
.Rect((x
-1)*32,(y
)*32,32,32)
759 for sprite
in stage
.sprites
:
760 if sprite
.FSOLID
and sprite
.rect
.colliderect(testrect
):
765 stage
.setCh(x
-1,y
,'B')
768 class Life(qweShoot
):
769 FRICTION_AIR
=(10.0,10.0)
771 ShowLife
=MaxLife
=Life
=10
773 LifeCollisionSpriteCost
=-10
786 def __init__(self
, stage
, pos
,imgfile
, life
):
788 qweSprite
.__init
__(self
,stage
)
790 self
.image1
= pygame
.image
.load(imgfile
)
791 self
.image
= self
.image1
.copy()
792 self
.rect
= self
.image
.get_rect()
793 self
.ShowLife
=self
.MaxLife
=self
.Life
=life
795 self
.rect
.topleft
= pos
798 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
799 self
.tickms(5,stage
);
801 def tickms(self
, ms
, stage
):
803 self
.RADIUS
+=ms
/5000.0
805 v
=qweSprite
.tickms(self
, ms
, stage
)
806 if self
.psx
<0: self
.move(8,0)
807 if self
.psx
>=20: self
.move(-8,0)
809 if self
.psy
<0: self
.move(0,8)
810 if self
.psy
>=15: self
.move(0,-8)