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
):
111 self
.MAX_ACCUMMS
= -1
114 rect1
=pygame
.Rect(self
.rect
)
115 d
=math
.sqrt(self
.dx
*self
.dx
+self
.dy
*self
.dy
)
117 #self.accum_dist+=d*self.accum_ms
118 if self
.ShowLife
>self
.Life
:
119 self
.accum_dist
+=(self
.ShowLife
-self
.Life
)
122 self
.accum_dist
+=10/(self
.Life
+0.1)
124 if self
.accum_ms
>self
.MAX_ACCUMMS
or \
125 self
.accum_dist
+d
*self
.accum_ms
>self
.MAX_ACCUMDIST
:
130 if (self
.ItemTime
>0):
139 if (self
.REGEN
<0 or self
.Life
<self
.MaxLife
):
140 self
.Life
+=self
.REGEN
*ms
/1000.0
142 if self
.ShowLife
>self
.Life
:
143 self
.ShowLife
-=ms
/100.0
144 if self
.ShowLife
<self
.Life
:
145 self
.ShowLife
=self
.Life
147 if d
*self
.accum_ms
>1000: factor
=d
*self
.accum_ms
/1000.0
150 if (iter_ms
>self
.MAX_ITERMS
/factor
): iter_ms
=self
.MAX_ITERMS
/factor
152 self
.checkpos(stage
,iter_ms
)
153 self
.move(self
.dx
*iter_ms
/1000.0,self
.dy
*iter_ms
/1000.0)
154 self
.dx
/=1+self
.FRICTION_AIR
[0]*iter_ms
/1000.0
155 self
.dy
/=1+self
.FRICTION_AIR
[1]*iter_ms
/1000.0
159 self
.psx
,self
.psy
=self
.getpspoint(stage
,0,0);
161 return [rect1
.union(rect2
)]
166 def getpspoint(self
,stage
,px
,py
):
167 sx
,sy
=stage
.BlockSize
183 def collision(self
,stage
,x
,y
,char
):
185 Pantalla
=stage
.StageData
189 self
.LevelFinished
=True
198 if char
=='Z' and self
.Item
=='z' and stage
.getCh(x
,y
-1)==' ':
199 testrect
=pygame
.Rect(x
*32+8,(y
-1)*32+8,16,16)
202 for sprite
in stage
.sprites
:
203 if sprite
.FSOLID
and sprite
.rect
.colliderect(testrect
):
207 stage
.setCh(x
,y
-1,'z')
213 def checkcell(self
,stage
,px
,py
):
214 Pantalla
=stage
.StageData
216 x0
,y0
=self
.getpspoint(stage
,px
,py
);
218 if y0
<0 and x0
>=0 and x0
<len(Pantalla
[0]): return ' '
219 if y0
>=0 and y0
<len(Pantalla
):
220 if x0
>=0 and x0
<len(Pantalla
[y0
]):
222 self
.collision(stage
,x0
,y0
,ch
)
228 def checkpos(self
,stage
,ms
):
235 for sprite
in stage
.sprites
:
236 if sprite
!=self
and sprite
.RADIUS
>0 and sprite
.Life
>0.5:
237 if ( math
.fabs(sprite
.psx
-self
.psx
)<=1
238 and math
.fabs(sprite
.psy
-self
.psy
)<=1):
240 sx
,sy
,sw
,sh
=sprite
.rect
242 min_d
=self
.RADIUS
+sprite
.RADIUS
245 d1
=math
.sqrt(ax
*ax
+ay
*ay
)
253 d2
=math
.sqrt(ax
*ax
+ay
*ay
)
257 if sprite
.SOLID
and self
.FSOLID
:
261 if self
.SOLID
and sprite
.FSOLID
:
266 self
.Life
-=sprite
.LifeCollisionSpriteCost
267 if self
.Life
<0.5: self
.Life
=0.5
269 self
.ShowLife
=self
.Life
+10
272 sprite
.Life
-=self
.LifeCollisionSpriteCost
275 if sprite
.ShowLife
>0:
276 sprite
.ShowLife
=sprite
.Life
+10
283 d1
=math
.sqrt(ax
*ax
+ay
*ay
)+1
284 if self
.SOLID
and sprite
.FSOLID
:
288 if sprite
.SOLID
and self
.FSOLID
:
294 # Colisión Horizontal
295 if ( (self
.checkcell(stage
,self
.MARGIN_RIGHT
,0)!=' ' and self
.dx
>=0) or
296 (self
.checkcell(stage
,self
.MARGIN_RIGHT
,12)!=' ' and self
.dx
>=0) or
297 (self
.checkcell(stage
,self
.MARGIN_RIGHT
,-12)!=' ' and self
.dx
>=0) ) :
298 if self
.Life
<900: self
.Life
-=self
.LifeCollisionCost
299 self
.move(-self
.dx
*ms
/1000.0-.1,0)
300 if (self
.BOUNCE
>0): self
.dx
*=-self
.BOUNCE
301 else: self
.dx
/=1-self
.BOUNCE
303 if ( (self
.checkcell(stage
,self
.MARGIN_LEFT
,0)!=' ' and self
.dx
<=0) or
304 (self
.checkcell(stage
,self
.MARGIN_LEFT
,12)!=' ' and self
.dx
<=0) or
305 (self
.checkcell(stage
,self
.MARGIN_LEFT
,-12)!=' ' and self
.dx
<=0) ) :
306 if self
.Life
<900: self
.Life
-=self
.LifeCollisionCost
307 self
.move(-self
.dx
*ms
/1000.0+.1,0)
308 if (self
.BOUNCE
>0): self
.dx
*=-self
.BOUNCE
309 else: self
.dx
/=1-self
.BOUNCE
311 if ( (self
.checkcell(stage
,self
.MARGIN_RIGHT
-3,0)!=' ') and
312 (self
.checkcell(stage
,self
.MARGIN_RIGHT
-3,12)!=' ') and
313 (self
.checkcell(stage
,self
.MARGIN_RIGHT
-3,-12)!=' ') ) :
316 if ( (self
.checkcell(stage
,self
.MARGIN_LEFT
+3,0)!=' ') and
317 (self
.checkcell(stage
,self
.MARGIN_LEFT
+3,12)!=' ') and
318 (self
.checkcell(stage
,self
.MARGIN_LEFT
+3,-12)!=' ') ) :
322 # Control de hundimiento
324 if (self
.checkcell(stage
,self
.MARGIN_LEFT
+4,self
.MARGIN_BOTTOM
-1)!=' '
325 or self
.checkcell(stage
,self
.MARGIN_RIGHT
-4,self
.MARGIN_BOTTOM
-1)!=' ') and \
326 (self
.checkcell(stage
,self
.MARGIN_LEFT
+4,self
.MARGIN_TOP
+1)==' '
327 and self
.checkcell(stage
,self
.MARGIN_RIGHT
-4,self
.MARGIN_TOP
+1)==' '):
331 if (self
.checkcell(stage
,0,0)!=' '):
336 if (self
.checkcell(stage
,self
.MARGIN_LEFT
+4,self
.MARGIN_TOP
)!=' ' or
337 self
.checkcell(stage
,self
.MARGIN_RIGHT
-4,self
.MARGIN_TOP
)!=' ' or
338 self
.checkcell(stage
,0,self
.MARGIN_TOP
)!=' '):
340 if self
.Life
<900: self
.Life
-=self
.LifeCollisionCost
341 self
.move(0,-self
.dy
*ms
/1000.0)
342 if (self
.BOUNCE
>0): self
.dy
*=-self
.BOUNCE
343 else: self
.dy
/=1-self
.BOUNCE
344 self
.dx
/=1+self
.FRICTION_CEIL
[0]*ms
/1000.0
345 self
.dy
/=1+self
.FRICTION_CEIL
[1]*ms
/1000.0
346 if self
.checkcell(stage
,0,+2)!=' ' and self
.dy
<0:
347 self
.dx
/=1+self
.FRICTION_CEIL
[0]*ms
/1000.0
348 self
.dy
/=1+self
.FRICTION_CEIL
[1]*ms
/1000.0
349 if self
.checkcell(stage
,0,+3)!=' ' and self
.dy
<0:
355 if ((self
.checkcell(stage
,self
.MARGIN_LEFT
+4,self
.MARGIN_BOTTOM
)!=' '
356 and self
.checkcell(stage
,0,self
.MARGIN_BOTTOM
)!=' ')
357 or (self
.checkcell(stage
,self
.MARGIN_RIGHT
-4,self
.MARGIN_BOTTOM
)!=' '
358 and self
.checkcell(stage
,0,self
.MARGIN_BOTTOM
)!=' ')
361 self
.dx
/=1+self
.FRICTION_FLOOR
[0]*ms
/1000.0
363 if self
.Life
<900: self
.Life
-=self
.LifeCollisionCost
364 if (self
.BOUNCE
>0): self
.dy
*=-self
.BOUNCE
365 else: self
.dy
/=1-self
.BOUNCE
366 self
.dy
/=1+self
.FRICTION_FLOOR
[1]*ms
/1000.0
367 self
.move(0,-self
.dy
*ms
/1000.0)
375 if self
.checkcell(stage
,self
.MARGIN_LEFT
+4,self
.MARGIN_BOTTOM
)!=' ': self
.dx
+=1
376 if self
.checkcell(stage
,self
.MARGIN_RIGHT
-4,self
.MARGIN_BOTTOM
)!=' ': self
.dx
-=1
377 self
.dy
+=self
.GRAVITY
379 if self
.Life
>self
.MaxLife
*1.5: self
.Life
=self
.MaxLife
*1.5
381 class qweText(qweSprite
):
387 LifeCollisionSpriteCost
=0
393 def __init__(self
, stage
,x
,y
,txt
,font
,color
):
395 qweSprite
.__init
__(self
,stage
)
398 self
.image
=font
.render(txt
, True, color
)
400 self
.rect
= self
.image
.get_rect()
402 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
406 def collision(self
,stage
,x
,y
,char
):
409 def checkcell(self
,stage
,px
,py
):
412 def checkpos(self
,stage
,ms
):
416 global qweTextBasic_font
417 qweTextBasic_font
=None
418 global qweTextBIG_font
421 class qweTextBasic(qweText
):
422 def __init__(self
, stage
,x
,y
,txt
):
423 global qweTextBasic_font
424 if not qweTextBasic_font
:
425 qweTextBasic_font
=pygame
.font
.Font(None,15)
429 qweText
.__init
__(self
,stage
,x
,y
,txt
,qweTextBasic_font
,color
)
431 class qweTextBIG(qweText
):
432 def __init__(self
, stage
,x
,y
,txt
):
433 qweSprite
.__init
__(self
,stage
)
434 global qweTextBIG_font
435 if not qweTextBIG_font
:
436 qweTextBIG_font
=pygame
.font
.Font(None,64)
438 self
.FONT
=qweTextBIG_font
440 self
.image1
=qweTextBIG_font
.render(txt
, True, (0,0,0))
441 self
.image2
=qweTextBIG_font
.render(txt
, True, (255,255,255))
442 w
,h
= self
.image1
.get_size()
445 self
.image
=pygame
.Surface((w
,h
),pygame
.SRCALPHA
).convert_alpha()
446 self
.image
.fill((0,0,0,0))
447 self
.image
.blit(self
.image1
,(5,5))
448 self
.image
.blit(self
.image2
,(0,0))
451 self
.rect
= self
.image
.get_rect()
453 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
458 class Tajundra(qweSprite
):
469 DIE_IMG
='platano.png'
474 def __init__(self
, stage
):
476 qweSprite
.__init
__(self
,stage
)
477 self
.image_right
= pygame
.image
.load('tajuA1.png')
478 self
.image_left
=pygame
.transform
.flip(self
.image_right
,True,False)
480 self
.imageb_right
= pygame
.image
.load('tajuA2.png')
481 self
.imageb_left
=pygame
.transform
.flip(self
.imageb_right
,True,False)
484 self
.image
=self
.image_right
485 self
.rect
= self
.image
.get_rect()
486 pos
=stage
.simbolos
['@'][0]
487 del stage
.simbolos
['@'][0]
488 self
.rect
.bottomright
= pos
489 self
.rect
=self
.rect
.move(stage
.BlockSize
)
490 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
492 def selectImage(self
):
496 self
.image
=self
.image_left
498 self
.image
=self
.image_right
501 self
.image
=self
.imageb_left
503 self
.image
=self
.imageb_right
505 if self
.estado_ms
>20:
517 class Dragon(qweSprite
):
527 def __init__(self
, stage
):
529 qweSprite
.__init
__(self
,stage
)
530 self
.image_left
= pygame
.image
.load('dragonN1.png')
531 self
.image_right
=pygame
.transform
.flip(self
.image_left
,True,False)
533 self
.imageb_left
= pygame
.image
.load('dragonN2.png')
534 self
.imageb_right
=pygame
.transform
.flip(self
.imageb_left
,True,False)
537 self
.image
=self
.image_right
538 self
.rect
= self
.image
.get_rect()
539 pos
=stage
.simbolos
['$'][0]
540 del stage
.simbolos
['$'][0]
542 self
.rect
.bottomright
= pos
543 self
.rect
=self
.rect
.move(stage
.BlockSize
)
544 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
546 def selectImage(self
):
550 self
.image
=self
.image_left
552 self
.image
=self
.image_right
555 self
.image
=self
.imageb_left
557 self
.image
=self
.imageb_right
559 if self
.estado_ms
>20:
567 def tickms(self
, ms
, stage
):
568 if self
.dx
<-10: self
.direction
=-1
569 if self
.dx
>+10: self
.direction
=+1
571 if self
.checkcell(stage
,self
.MARGIN_RIGHT
,0)!=' ': self
.direction
=-1
572 if self
.checkcell(stage
,self
.MARGIN_LEFT
,0)!=' ': self
.direction
=+1
574 if self
.checkcell(stage
,self
.MARGIN_RIGHT
,self
.MARGIN_BOTTOM
)==' ': self
.direction
=-1
575 if self
.checkcell(stage
,self
.MARGIN_LEFT
,self
.MARGIN_BOTTOM
)==' ': self
.direction
=+1
576 v
=qweSprite
.tickms(self
, ms
, stage
)
577 self
.dx
+=self
.direction
581 class LanzaBolas(qweSprite
):
591 DIE_IMG
='safanoria.png'
592 def __init__(self
, stage
):
594 qweSprite
.__init
__(self
,stage
)
595 self
.image
= pygame
.image
.load('lanzabolas1.png')
597 self
.rect
= self
.image
.get_rect()
598 pos
=stage
.simbolos
['|'][0]
599 del stage
.simbolos
['|'][0]
601 self
.rect
.bottomright
= pos
602 self
.rect
=self
.rect
.move(stage
.BlockSize
)
603 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
604 self
.estado_ms
=random
.randint(6000,12000)
606 def selectImage(self
):
609 def tickms(self
, ms
, stage
):
611 v
=qweSprite
.tickms(self
, ms
, stage
)
613 if self
.estado_ms
>10000 and self
.Life
>10:
615 self
.estado_ms
=random
.randint(0,1000)
616 r
=random
.randint(-1000,1000)/3.0
617 disparo
=Bola(stage
,(int(self
.x
),int(self
.y
)),r
,-200)
625 class qweShoot(qweSprite
):
628 FRICTION_AIR
=(0.0,0.0)
629 FRICTION_FLOOR
=(0.0,0.0)
630 FRICTION_CEIL
=(0.0,0.0)
636 LifeCollisionSpriteCost
=10
647 def __init__(self
, stage
, pos
,dx
,dy
):
649 qweSprite
.__init
__(self
,stage
)
650 self
.image
= pygame
.image
.load('bola1.png')
652 self
.rect
= self
.image
.get_rect()
654 self
.rect
.topleft
= pos
657 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
658 d
=math
.sqrt(dx
*dx
+dy
*dy
)+5
659 self
.tickms(1000/d
,stage
);
661 def collision(self
,stage
,x
,y
,char
):
662 qweSprite
.collision(self
,stage
,x
,y
,char
)
666 if char
=='M': nchar
='N'
667 if char
=='N': nchar
='m'
668 if char
=='m': nchar
='n'
669 if char
=='n': nchar
=' '
672 stage
.setCh(x
,y
,nchar
)
678 def die(self
,screen
):
681 class Bola(qweShoot
):
682 FRICTION_AIR
=(1.0,1.0)
692 class Hacha(qweShoot
):
693 FRICTION_AIR
=(0.5,0.0)
697 LifeCollisionCost
=0.1
698 LifeCollisionSpriteCost
=10
711 def __init__(self
, stage
, pos
,dx
,dy
):
713 qweSprite
.__init
__(self
,stage
)
714 self
.image1
= pygame
.image
.load('hacha1.png')
715 if dx
<0: self
.image1
=pygame
.transform
.flip(self
.image1
, True, False)
716 self
.image
= self
.image1
.copy()
717 self
.rect
= self
.image
.get_rect()
719 self
.rect
.topleft
= pos
722 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
725 def draw(self
,screen
,lr
=None):
727 if self
.Life
>0 and self
.fcountrot
>5/self
.Life
:
728 self
.image
=pygame
.transform
.rotate(self
.image1
, self
.angle
)
734 if self
.Life
>2 or self
.Life
<.5:
739 qweShoot
.draw(self
,screen
,lr
)
741 def tickms(self
, ms
, stage
):
745 self
.time_elapsed
+=ms
746 if self
.time_elapsed
<50:
751 v
=qweSprite
.tickms(self
, ms
, stage
)
754 def collision(self
,stage
,x
,y
,char
):
755 if self
.ItemTime
>0: return
756 qweShoot
.collision(self
,stage
,x
,y
,char
)
758 if char
=='B' and x
>self
.psx
and self
.dx
>0 \
759 and stage
.getCh(x
+1,y
)==' ': # and stage.getCh(x+1,y+1)!=' ':
763 testrect
=pygame
.Rect((x
+1)*32,(y
)*32,32,32)
765 for sprite
in stage
.sprites
:
766 if sprite
.FSOLID
and sprite
.rect
.colliderect(testrect
):
771 stage
.setCh(x
+1,y
,'B')
773 if char
=='B' and x
<self
.psx
and self
.dx
<0 \
774 and stage
.getCh(x
-1,y
)==' ': # and stage.getCh(x-1,y+1)!=' ':
778 testrect
=pygame
.Rect((x
-1)*32,(y
)*32,32,32)
780 for sprite
in stage
.sprites
:
781 if sprite
.FSOLID
and sprite
.rect
.colliderect(testrect
):
786 stage
.setCh(x
-1,y
,'B')
789 class Life(qweShoot
):
790 FRICTION_AIR
=(10.0,10.0)
792 ShowLife
=MaxLife
=Life
=10
794 LifeCollisionSpriteCost
=-10
807 def __init__(self
, stage
, pos
,imgfile
, life
):
809 qweSprite
.__init
__(self
,stage
)
811 self
.image1
= pygame
.image
.load(imgfile
)
812 self
.image
= self
.image1
.copy()
813 self
.rect
= self
.image
.get_rect()
814 self
.ShowLife
=self
.MaxLife
=self
.Life
=life
816 self
.rect
.topleft
= pos
819 self
.x
,self
.y
,self
.w
,self
.h
=self
.rect
820 self
.tickms(5,stage
);
822 def tickms(self
, ms
, stage
):
824 self
.RADIUS
+=ms
/5000.0
826 v
=qweSprite
.tickms(self
, ms
, stage
)
827 if self
.psx
<0: self
.move(8,0)
828 if self
.psx
>=20: self
.move(-8,0)
830 if self
.psy
<0: self
.move(0,8)
831 if self
.psy
>=15: self
.move(0,-8)