-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprites.py
345 lines (245 loc) · 9.32 KB
/
sprites.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from os import remove
from pkgutil import iter_modules
import pygame
WIN_WIDTH = 640
WIN_HEIGHT = 480
ITEM_MAKER = ["camomila", "chaleira", "mint"]
TEA_FLAVORS = ["camomila", "mint"]
ITEM_LIST = ["flavor", "chaleira"]
class PhysicsEntity(pygame.sprite.Sprite):
def __init__(
self,
game,
e_type,
pos,
size,
):
self.game = game
self.pos = pos
self.size = size
self.type = e_type
self.velocity = [0, 0]
self.collision = {"up": False, "down": False, "left": False, "right": False}
self.flip = False
self.display = pygame.surface.Surface(self.size)
self.display.fill((198, 55, 32))
self.action = ""
self.set_action("walk")
self.back = False
self.anim_offset = (-3, -3)
self.mov = [0, 0]
def set_action(self, action):
if action != self.action:
self.action = action
self.animation = self.game.assets[self.type + "/" + self.action].copy()
def rect(self):
return pygame.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1]).copy()
def update(self, tilemap, movement=(0, 0)):
frame_movement = [
movement[0] + self.velocity[0],
movement[1] + self.velocity[1],
]
self.mov[0] = frame_movement[0]
self.mov[1] = frame_movement[1]
entity_rect = self.rect()
self.pos[0] += frame_movement[0]
for rect in tilemap.physics_rect_around(self.pos):
if entity_rect.colliderect(rect):
if frame_movement[0] < 0:
# self.pos[0] = entity_rect.x
# self.pos[1] = self.pos[1]
entity_rect.left = rect.right - entity_rect.width
if frame_movement[0] > 0:
entity_rect.right = rect.left
self.pos[0] = entity_rect.x
entity_rect_y = self.rect()
self.pos[1] += frame_movement[1]
for rect in tilemap.physics_rect_around(self.pos):
if entity_rect_y.colliderect(rect):
if frame_movement[1] > 0:
entity_rect_y.bottom = rect.top
if frame_movement[1] < 0:
entity_rect_y.top = rect.bottom
self.pos[1] = entity_rect_y.y
if movement[0] > 0:
self.flip = True
if movement[0] < 0:
self.flip = False
self.animation.update()
def render(self, surf, offset=(0, 0)):
surf.blit(
pygame.transform.flip(self.animation.img(), self.flip, False),
(
self.pos[0] - offset[0] + self.anim_offset[0],
self.pos[1] - offset[1] + self.anim_offset[1],
),
)
class MovableObject(pygame.sprite.Sprite):
def __init__(self, game, e_type, item_type, pos, size):
self.game = game
self.e_type = e_type
self.pos = pos
self.size = size
self.item_type = item_type
self.movement_x = 0
self.movement_y = 0
def rect(self):
return pygame.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1]).copy()
def render(self, surf, offset=(0, 0)):
rect = self.rect().center
self.img = self.game.assets["tea/" + self.e_type].copy()
rect = self.pos
surf.blit(self.img, (rect[0] - offset[0], rect[1] - offset[1]))
def update(self, tilemap):
entity_rect = self.rect()
for rect in tilemap.physics_rect_around(self.pos):
if entity_rect.colliderect(rect):
if self.movement_x < 0:
# self.pos[0] = entity_rect.x
# self.pos[1] = self.pos[1]
self.pos[0] += 30
if self.movement_x > 0:
self.pos[0] -= 30
entity_rect_y = self.rect()
for rect in tilemap.physics_rect_around(self.pos):
if entity_rect_y.colliderect(rect):
if self.movement_y > 0:
self.pos[1] -= 30
if self.movement_y < 0:
self.pos[1] += 30
class Player(PhysicsEntity):
def __init__(
self,
game,
pos,
size,
):
super().__init__(
game,
"player",
pos,
size,
)
def update(self, tilemap, movement=(0, 0)):
if movement[0] > 0 and movement[1] == 0:
self.set_action("side_walk_left")
elif movement[0] < 0 and movement[1] == 0:
self.set_action("side_walk_left")
elif movement[1] < 0 and movement[0] == 0:
self.set_action("walk_back")
self.back = True
elif movement[1] > 0 and movement[0] == 0:
self.set_action("walk")
self.back = False
elif movement[1] == 0 and not self.back or movement[0] == 0 and not self.back:
self.set_action("iddle")
elif movement[1] == 0 and self.back or movement[0] == 0 and self.back:
self.set_action("iddle_back")
tea_rects = [tea for tea in self.game.items]
for tea in tea_rects:
rect = tea.rect()
if self.rect().colliderect(rect):
if self.mov[0] > 0:
self.pos[0] = rect.left - self.rect().width
tea.pos[0] += 2 # 1
tea.movement_x = 1
elif self.mov[0] < 0:
self.pos[0] = rect.right
tea.pos[0] -= 2 # *-1
tea.movement_x = -1
if self.mov[1] > 0:
self.pos[1] = rect.top - self.rect().height
tea.pos[1] += 2 # 1
tea.movement_y = 1
elif self.mov[1] < 0:
self.pos[1] = rect.bottom
tea.pos[1] -= 2 # -1
tea.movement_y = -1
super().update(tilemap, movement=movement)
def collect_items(self):
tea_rects = [tea for tea in self.game.items]
for item in tea_rects:
item.move_item()
class Flower(MovableObject):
def __init__(self, game, e_type, item_type, pos, size):
super().__init__(game, e_type, item_type, pos, size)
def move_item(self):
self.pos[0] += 1
def update(self, tilemap):
items = [item for item in self.game.items]
for item in items:
if (
self.rect().colliderect(item.rect())
and self.e_type != item.e_type
and self.e_type in ITEM_MAKER
and self.e_type in ITEM_MAKER
and self.item_type != item.item_type
and self.item_type in ITEM_LIST
and item.item_type in ITEM_LIST
and self.item_type != "tea_cup"
and self.item_type != "tea_cup"
):
self.game.items.remove(item)
for j in self.game.items:
if (
j.pos == self.pos
and j.e_type == self.e_type
and j.item_type == self.item_type
):
self.game.items.remove(j)
if self.e_type in TEA_FLAVORS:
self.game.flavor = self.e_type
if item.e_type in TEA_FLAVORS:
self.game.flavor = item.e_type
self.game.tea_pos = item.pos
return True
super().update(tilemap)
class Tea(MovableObject):
def __init__(self, game, e_type, item_type, pos, size, flavor):
super().__init__(game, e_type, item_type, pos, size)
self.flavor = flavor
def render(self, surf, offset=(0, 0)):
rect = self.rect().center
self.img = self.game.assets["tea/" + self.e_type + "/" + self.flavor].copy()
rect = self.pos
surf.blit(self.img, (rect[0] - offset[0], rect[1] - offset[1]))
def update(self, tilemap):
return super().update(tilemap)
class Order(pygame.sprite.Sprite):
def __init__(self, game, pos, size, flavor):
self.game = game
self.pos = pos
self.size = size
self.flavor = flavor
def rect(self):
return pygame.rect.Rect(
self.pos[0], self.pos[1], self.size[0], self.size[1]
).copy()
def update(self):
items = [item for item in self.game.items]
for item in items:
if (
self.rect().colliderect(item.rect())
and item.item_type == "tea"
and item.flavor == self.flavor
):
self.game.items.remove(item)
for j in self.game.items:
if j.pos == self.pos:
self.game.items.remove(j)
self.game.tea_pos = item.pos
return True
def render(self, surf, offset=(0, 0)):
self.img = self.game.assets["order/" + self.flavor].copy()
rect = self.rect()
surf.blit(self.img, (rect[0] - offset[0], rect[1] - offset[1]))
class Background(pygame.sprite.Sprite):
def __init__(self,game, pos, img):
self.game = game
self.pos = pos
self.img = img.convert()
def rect(self):
return self.img.get_rect()
def render(self, surf, offset = (0,0)):
rect = self.rect()
surf.blit(self.img, rect)