-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced_sprites.py
249 lines (207 loc) · 7.51 KB
/
advanced_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
import typing
import constants
import pygame
import abstract_sprite
import preload
DX = DY = 2
class ForeFinger(abstract_sprite.AbstractSprite):
"""
Forefinger sprite. It is used in the menu.
"""
def __init__(self,
image: pygame.Surface,
coordinates: typing.Tuple[int, int],
group: pygame.sprite.Group = None,
) -> None:
"""
Initialization of the sprite.
:param image: the image which represents the sprite.
:param coordinates: an initial coordinates (top left).
:param group: sprite group where to put the object.
"""
super().__init__(group)
self.image = image
self.rect = self.image.get_rect().move(*coordinates)
def move(self, coordinates: typing.Tuple[int, int]) -> None:
"""
Moves the sprite.
:param coordinates: new coordinates (top left).
"""
self.rect.x, self.rect.y = coordinates
def shake(self, b: bool) -> None:
"""
An animation of shaking.
:param b: direction of motion.
"""
if b:
self.rect.y += 20
else:
self.rect.y -= 20
class MiniPlayer(abstract_sprite.AbstractSprite):
"""
Mini player. It is used in map menu as a pointer.
"""
def __init__(self,
image: pygame.Surface,
coordinates: typing.Tuple[int, int],
group: pygame.sprite.Group = None,
) -> None:
"""
Initialization of the sprite.
:param image: the image which represents the sprite.
:param coordinates: an initial coordinates (top left).
:param group: sprite group where to put the object.
"""
super().__init__(group)
self.image = image
self.rect = self.image.get_rect().move(*coordinates)
def move(self, coordinates: typing.Tuple[int, int]) -> None:
"""
Moves the sprite.
:param coordinates: new coordinates (top left).
"""
self.rect.x, self.rect.y = coordinates
class Leaf(abstract_sprite.AbstractSprite):
"""
Sprite of one tile of leaves. It is used in map rendering.
"""
def __init__(self, raw_coordinates: typing.Tuple[int, int], group: pygame.sprite.Group = None) -> None:
"""
Initialization of the sprite.
:param raw_coordinates: an initial coordinates (x and y of matrix).
:param group: sprite group where to put the sprite.
"""
super().__init__(group)
self.images = [preload.load_image(filename) for filename in constants.LEAVES_IMAGE_NAMES]
self.image = self.images[0]
self.rect = self.image.get_rect()
self.raw_x, self.raw_y = raw_coordinates
self.t = -1
def disappear(self, t: int) -> None:
"""
Starts an animation of disappearance.
:param t: start time.
"""
if self.t == -1:
self.t = t
def next_stage(self, t: int) -> None:
"""
Provides an animation. Sprite is killed when animation ends.
:param t: current time (based on this, the image changes).
"""
if self.t == -1:
return
i = int((t - self.t) / constants.LEAVES_FALL_DELTA_TIME)
if i >= len(self.images) - 1:
self.kill()
return
self.image = self.images[i]
class FallingSprite(abstract_sprite.AbstractSprite):
"""
Sprite with property to fall.
"""
images = []
def __init__(self, raw_coordinates: typing.Tuple[int, int], group: pygame.sprite.Group = None) -> None:
"""
Initialization of the sprite.
:param raw_coordinates: an initial coordinates (x and y of matrix).
:param group: sprite group where to put the sprite.
"""
super().__init__(group)
self.raw_x, self.raw_y = raw_coordinates
self.image = self.images[0]
self.rect = self.image.get_rect()
self.cur_i = 0
self.t = -1
self.t2 = 0
self.cur_pos = 0
def fall(self, t: int, dx: int) -> None:
"""
Provides an animation of falling.
:param t: current time (if fall is called for the first time initializes it),
(based on this, the image changes).
:param dx: direction of falling (-1 - left, 0 - down, 1 - right).
"""
if self.t == -1:
self.t = t
self.t2 = t
elif t - self.t2 > constants.OBJECTS_FALL_DELTA_TIME:
if dx == -1:
self.raw_x -= 1
self.raw_y += 1
elif dx == 1:
self.raw_x += 1
self.raw_y += 1
elif dx == 0:
self.raw_y += 1
self.cur_pos = 0
self.t2 = self.t = t
elif t - self.t > constants.OBJECTS_TWITCH_DELTA_TIME:
self.cur_pos ^= 1
if dx == -1:
if self.cur_pos:
self.rect = self.rect.move(-DX, -DY)
else:
self.rect = self.rect.move(DX, DY)
elif dx == 1:
if self.cur_pos:
self.rect = self.rect.move(DX, DY)
else:
self.rect = self.rect.move(-DX, -DY)
self.t = t
class Diamond(FallingSprite):
"""
Sprite of one diamond. It is used in map rendering.
"""
def __init__(self, raw_coordinates: typing.Tuple[int, int], group: pygame.sprite.Group = None) -> None:
"""
Initialization of the sprite.
:param raw_coordinates: an initial coordinates (x and y of matrix).
:param group: sprite group where to put the sprite.
"""
self.images = [preload.load_image(filename) for filename in constants.PURPLE_DIAMOND_IMAGE_NAMES]
super().__init__(raw_coordinates, group)
def next_image(self) -> None:
"""
Provides an animation.
"""
self.cur_i = (self.cur_i + 1) % len(self.images)
self.image = self.images[self.cur_i]
class Rock(FallingSprite):
"""
Sprite of one rock. It is used in map rendering.
"""
def __init__(self, raw_coordinates: typing.Tuple[int, int], group: pygame.sprite.Group = None) -> None:
"""
Initialization of the sprite.
:param raw_coordinates: an initial coordinates (x and y of matrix).
:param group: sprite group where to put the sprite.
"""
self.images = [preload.load_image(filename) for filename in constants.ROCKS_IMAGE_NAMES]
self.t3 = -1
super().__init__(raw_coordinates, group)
def move(self, dx: int, t: int) -> bool:
"""
Provides motion.
:param dx: direction of motion (dx > 0 - right, dx <= 0 - left).
:param t: current time (if move is called for the first time initializes it),
(based on this, sprite moves).
:return: True if move is successful else False.
"""
if self.t3 == -1:
self.t3 = t
elif t - self.t3 >= constants.ROCK_MOVE_DELTA_TIME:
self.t3 = t
self.raw_x += 1 if dx > 0 else -1
return True
return False
def next_image(self, direction: int) -> None:
"""
Provides an animation of motion.
:param direction: direction of motion (1 - right, -1 - left).
"""
if direction == 1:
self.cur_i = (self.cur_i + 1) % len(self.images)
else:
self.cur_i = (self.cur_i - 1) % len(self.images)
self.image = self.images[self.cur_i]