Mejora a la física y movimiento de los sprites.
[tajundrathegame.git] / sprites.py
blob52b01f7642180e8c023b6168511dd2e9cc7b2119
1 # encoding: UTF-8
2 import pygame
4 class Tajundra(pygame.sprite.Sprite):
5 stage=None
6 image_flip=None
7 dx=0
8 dy=0
9 x=y=0
10 w=h=32
11 estado=0
12 estado_ms=0
13 in_ground=False
15 def __init__(self, stage):
16 self.stage=stage
18 pygame.sprite.Sprite.__init__(self)
19 self.image_right = pygame.image.load('tajuA1.png')
20 self.image_left=pygame.transform.flip(self.image_right,True,False)
22 self.imageb_right = pygame.image.load('tajuA2.png')
23 self.imageb_left=pygame.transform.flip(self.imageb_right,True,False)
26 self.image=self.image_right
27 self.rect = self.image.get_rect()
28 pos=self.stage.simbolos['@'][0]
29 self.rect.bottomright = pos
30 self.rect=self.rect.move(self.stage.BlockSize)
31 self.x,self.y,self.w,self.h=self.rect
33 def setDir(self):
34 dx=self.dx
35 if self.estado==0:
36 if dx<0:
37 self.image=self.image_left
38 else:
39 self.image=self.image_right
40 else:
41 if dx<0:
42 self.image=self.imageb_left
43 else:
44 self.image=self.imageb_right
47 def move(self,x,y):
48 self.x+=x
49 self.y+=y
51 self.rect=self.x,self.y,self.w,self.h
53 def draw(self,screen):
54 self.setDir()
55 screen.blit(self.image, self.rect)
57 def tick(self, fps):
58 ms=1000.0/fps
59 self.tickms(ms)
61 def tickms(self, ms):
62 self.checkpos(ms)
63 self.move(self.dx*ms/1000.0,self.dy*ms/1000.0)
64 self.dx/=1+0.1*ms/1000.0
65 self.dy/=1+0.1*ms/1000.0
68 def checkcell(self,px,py):
69 sx,sy=self.stage.BlockSize
70 x,y,w,h=self.rect
71 Pantalla=self.stage.StageData
73 if px>0: x0=x+px
74 elif px<0: x0=x+w+px
75 else: x0=x+w/2
77 if py>0: y0=y+py
78 elif py<0: y0=y+h+py
79 else: y0=y+h/2
82 x0=int(x0)/sx
83 y0=int(y0)/sy
84 if y0<0: return ' '
85 if y0>=0 and y0<len(Pantalla):
86 if x0>=0 and x0<len(Pantalla[y0]):
87 return Pantalla[y0][x0]
89 return 't'
91 def checkpos(self,ms):
92 t_estado=0
93 # Colisión Horizontal
94 if ( (self.checkcell(-8,0)!=' ' and self.dx>0) or
95 (self.checkcell(+8,0)!=' ' and self.dx<0) ) :
96 self.move(-self.dx*ms/1000.0,0)
98 # Control de hundimiento
99 if self.dy>=0:
100 if self.checkcell(+12,-3)!=' ' or self.checkcell(-12,-3)!=' ':
101 self.move(0,-1)
102 t_estado=1
104 # Colisión de techo
105 elif self.checkcell(0,+1)!=' ' and self.dy<0:
106 self.move(0,-self.dy*ms/1000.0)
107 self.dy/=1.1
109 # Gravedad
110 if self.checkcell(+10,-1)==' ' and self.checkcell(-10,-1)==' ':
111 self.dy+=5
112 self.in_ground=False
114 # Colisión suelo
115 if self.checkcell(+10,-2)!=' ' or self.checkcell(-10,-2)!=' ':
116 self.dx/=1.15
117 if self.dy>0:
118 self.dy/=1.02
119 self.move(0,-self.dy*ms/1000.0)
121 elif self.dy<0:
122 t_estado=1
124 self.in_ground=True
126 if self.estado!=t_estado and self.estado_ms>200:
127 self.estado=t_estado
128 self.estado_ms=0
129 else:
130 self.estado_ms+=ms