/
actor.py
executable file
·164 lines (142 loc) · 4.08 KB
/
actor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Módulos
from pygame.locals import *
from constants import events
from images import load_image
# Constantes
# Clases
# ---------------------------------------------------------------------
class Actor:
""
def __init__(self, map):
# Buscamos el evento player.
for i in range(len(events)):
if events[i]['type'] == 'player':
event = events[i]
self.pos = event['pos']
self.fil = int(event['fil'])
self.col = int(event['col'])
self.name_chara = event['image']
self.chara = cut_charaset("resources/graphics/charasets/"+self.name_chara, self.fil, self.col)
self.image = self.chara[0][0]
self.rect = self.image.get_rect()
self.rect.centerx = self.pos[1] * map.size_tiles[0] + (map.size_tiles[0]/2) #240
self.rect.bottom = self.pos[0] * map.size_tiles[1] + map.size_tiles[1]#320
self.unlock = 1
self.count = 0
self.look = 0
self.mov = 0
self.graph = 0
self.wait = 0
def can_move(self, map, dir):
"Hace los calculos de si se puede mover segun la direccion."
return {
'L' : self.pos[1] > 0 and map.tiles[self.pos[0]][self.pos[1]-1].lock[1] and map.tiles[self.pos[0]][self.pos[1]].lock[3],
'R' : self.pos[1] < map.width-1 and map.tiles[self.pos[0]][self.pos[1]+1].lock[3] and map.tiles[self.pos[0]][self.pos[1]].lock[1],
'U' : self.pos[0] > 0 and map.tiles[self.pos[0]-1][self.pos[1]].lock[2] and map.tiles[self.pos[0]][self.pos[1]].lock[0],
'D' : self.pos[0] < map.height-1 and map.tiles[self.pos[0]+1][self.pos[1]].lock[0] and map.tiles[self.pos[0]][self.pos[1]].lock[2]
}[dir]
def mover(self, map, input):
'''
Verifica si se puede mover y actualiza los atributos en consecuencia
Parametros:
map :
input : Instancia actual de la clase Input (input.py)
'''
if self.unlock:
if input.is_pressed(K_LEFT):
if self.can_move(map, 'L'):
self.count = 8
self.unlock = 0
self.mov = 1
self.pos[1] -= 1
return 1
else:
self.image = self.chara[1][0]
elif input.is_pressed(K_RIGHT):
if self.can_move(map, 'R'):
self.count = 8
self.unlock = 0
self.mov = 2
self.pos[1] += 1
return 2
else:
self.image = self.chara[2][0]
elif input.is_pressed(K_UP):
if self.can_move(map, 'U'):
self.count = 8
self.unlock = 0
self.mov = 3
self.pos[0] -= 1
return 3
else:
self.image = self.chara[3][0]
elif input.is_pressed(K_DOWN):
if self.can_move(map, 'D'):
self.count = 8
self.unlock = 0
self.mov = 0
self.pos[0] += 1
return 0
else:
self.image = self.chara[0][0]
return -1
def update(self, id):
'''
Mueve el personaje
Parámetros:
id : Direccion
'''
# Si no esta mirando para la direccion a caminar, cambiala!
if self.look != id and id != -1:
self.image = self.chara[id][0]
self.look = id
self.graph = 0
return 0
if self.count != 0:
if self.count % 2 == 0:
if self.mov == 1:
self.rect.x -= 8
elif self.mov == 2:
self.rect.x += 8
elif self.mov == 3:
self.rect.y -= 8
elif self.mov == 0:
self.rect.y += 8
self.count -= 1
if self.count == 0 and self.unlock == 1:
if self.wait == 0:
self.image = self.chara[self.mov][0]
self.wait -= 1
if self.count == 0 and self.unlock == 0:
self.graph +=1
self.graph %= 4
self.image = self.chara[self.mov][self.graph]
self.unlock = 1
self.wait = 4
def draw(self, screen):
screen.blit(self.image, self.rect)
# ---------------------------------------------------------------------
# Funciones
# ---------------------------------------------------------------------
def cut_charaset(ruta, fil, col):
image = load_image(ruta, True)
rect = image.get_rect()
w = rect.w / col
h = rect.h / fil
sprite = range(fil)
for i in range(fil):
sprite[i] = range(col)
for f in range(fil):
for c in range(col):
sprite[f][c] = image.subsurface((rect.left, rect.top, w, h))
rect.left += w
rect.top += h
rect.left = 0
return sprite
# ---------------------------------------------------------------------
def main():
pass
if __name__ == '__main__':
main()