/
animation.py
286 lines (244 loc) · 9.65 KB
/
animation.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
__author__ = 'Hieu'
import pygame
import constants
import math
from collision import Collision
from event_handlers import *
class Animation(pygame.sprite.Sprite):
INFINITE = 0
# @parameters:
# manager: pygame.sprite.Group that manages the sprite (add, remove, kill)
# frame: list of frame of the animation of effect
# speed: (millisecond) time needed for each animation loop
def __init__(self, manager, frames, speed=1000):
pygame.sprite.Sprite.__init__(self)
self.manager = manager
self.frames = frames
self.frameLength = len(self.frames)
self.currentFrame = 0
self.image = self.frames[self.currentFrame]
self.rect = self.image.get_rect()
self.speed = speed
self.fps = 1
self.counter = 0
self.finished = True
self.times = 1
self.origin_frame = Animation.copy_frame(self.frames)
self.degree = 0
self.is_static = False
def add_frame(self, fr):
self.frames.append(fr)
def update(self):
# Increase relative frame per second
self.update_relative_fps()
# Only play as much as ordered
if (self.counter >= self.times) and self.times is not self.INFINITE:
self.finished = True
self.manager.remove(self)
# Because of given speed, only shift the frame in the right time and when the loop not finish yet
if self.is_time_to_update() and not self.finished:
self.currentFrame = (self.currentFrame + self.calc_frame_step()) % self.frameLength
self.image = self.frames[self.currentFrame]
# Trigger increasing counter when loop finish a cycle
if self.currentFrame == (self.frameLength - 1):
self.counter += 1
self.currentFrame = 0
def calc_frame_step(self):
return 1
def play(self, times=1):
self.finished = False
self.times = times
self.counter = 0
self.manager.add(self)
def set_position(self, x, y):
self.rect.x = x
self.rect.y = y
def set_position_center(self, x, y):
self.rect.x = x - self.rect.width / 2
self.rect.y = y - self.rect.height / 2
def update_relative_fps(self):
self.fps += 1
def is_time_to_update(self):
if self.is_static:
return False
if self.fps > constants.FPS / ((self.frameLength / self.speed) * 1000):
self.fps = 1
return True
return False
def rotate_more(self, deg):
absolute_deg = self.degree + deg
self.degree = absolute_deg
for i in range(0, len(self.frames), 1):
self.frames[i] = Animation.rot_center(self.origin_frame[i], absolute_deg)
def rotate(self, deg):
for i in range(0, len(self.frames), 1):
self.frames[i] = Animation.rot_center(self.origin_frame[i], deg)
@staticmethod
def rot_center(image, angle):
"""rotate an image while keeping its center and size"""
orig_rect = image.get_rect()
rot_image = pygame.transform.rotate(image, angle)
rot_rect = orig_rect.copy()
rot_rect.center = rot_image.get_rect().center
rot_image = rot_image.subsurface(rot_rect).copy()
return rot_image
@staticmethod
def copy_frame(frames):
new_frame = []
for i in range(0, len(frames), 1):
new_frame.append(frames[i].copy())
return new_frame
# The effect occur when a player is active - controlled by user
class FocusEffect(Animation):
def __init__(self, manager, player, frame, speed=1000):
Animation.__init__(self, manager, frame, speed)
self.player = player
def update(self):
Animation.update(self)
center = self.player.rect.center
# self.set_position(rect.x + rect.width/2, rect.y + rect.height/2)
self.set_position_center(center[0], center[1])
def set_player(self, player):
self.player = player
class Ball(Animation):
BALL_STOP_BOUND = 1.3
MAX_VELOCITY = 10
def __init__(self, manager, position, frame, sfx, speed=1000):
Animation.__init__(self, manager, frame, speed)
self.collision = Collision()
self.set_position_center(position[0], position[1])
self.change_x = 0
self.change_y = 0
self.radius = 15.5
self.is_ball_going_in = False
self.is_ball_hit_wall = False
self.goal = False
self.m = 0.45
self.prevent = 1.01
self.ball_vs_wall_observable = BallVsWallObservable()
self.ball_vs_wall_observer = BallVsWallObserver(sfx)
self.ball_vs_wall_observable.register(self.ball_vs_wall_observer)
def calc_frame_step(self):
cx = math.fabs(self.change_x)
cy = math.fabs(self.change_y)
if max(cx, cy) == 0:
return 1
# elif 2 >= cx > 0.6 or 2 >= cy > 0.6:
# return 1
elif 4 > cx > 2 or 4 > cy > 2:
self.speed = 1000
return 2
elif 5 > cx >= 4 or 5 > cy >= 4:
self.speed = 1000
return 3
elif cx >= 5 or cy >= 5:
self.speed = 1000
return 4
else:
self.speed = int(1000 * 0.5 / (self.change_x + self.change_y))
# self.speed = 1000 - 80 / (self.change_x + self.change_y)
return 1
def update(self):
if self.change_x + self.change_y == 0:
self.is_static = True
else:
self.is_static = False
Animation.update(self)
if (self.rect.center[1] + self.change_y < constants.PLAY_SPACE_RECT[1] or self.rect.center[1] + self.change_y >
constants.PLAY_SPACE_RECT[1] + constants.PLAY_SPACE_RECT[3]):
self.change_y = 0
# if ((self.rect.center[0] + self.change_x < constants.PLAY_SPACE_RECT[0] or self.rect.center[0] + self.change_x >
# constants.PLAY_SPACE_RECT[0] + constants.PLAY_SPACE_RECT[2]) \
# and (self.rect.center[1] - self.radius + self.change_y < 260 or self.rect.center[1] + self.radius + self.change_y > 433)):
# self.change_x = 0
self.max_min_velocity()
if 1 > self.change_y > 0:
self.rect.y += 1
elif -1 < self.change_y < 0:
self.rect.y -= 1
else:
self.rect.y += round(self.change_y)
if 1 > self.change_x > 0:
self.rect.x += 1
elif -1 < self.change_x < 0:
self.rect.x -= 1
else:
self.rect.x += round(self.change_x)
self.collision.collisionBall2Wall(self, self.ball_vs_wall_observable)
self.change_x /= self.prevent
self.change_y /= self.prevent
if self.change_x == 0:
if self.change_y > 0:
self.degree = -90
else:
self.degree = 90
elif self.change_y == 0:
if self.change_x > 0:
self.degree = 0
else:
self.degree = 180
else:
self.degree = math.atan(math.fabs(self.change_y) / math.fabs(self.change_x)) * 180 / math.pi
if self.change_x > 0 and self.change_y > 0:
self.degree = -self.degree
elif self.change_x > 0 > self.change_y:
pass
elif self.change_x < 0 < self.change_y:
self.degree += 180
elif self.change_x < 0 and self.change_y < 0:
self.degree += (90 - self.degree) * 2
# print(self.degree)
self.rotate(self.degree)
def set_velocity(self, vx, vy):
self.change_x = vx
self.change_y = vy
def max_min_velocity(self):
if self.change_y > Ball.MAX_VELOCITY:
self.change_y = Ball.MAX_VELOCITY
if self.change_x > Ball.MAX_VELOCITY:
self.change_x = Ball.MAX_VELOCITY
if Ball.BALL_STOP_BOUND > self.change_x > -Ball.BALL_STOP_BOUND and Ball.BALL_STOP_BOUND > self.change_y > -Ball.BALL_STOP_BOUND:
self.change_x = 0
self.change_y = 0
class BallShadow(Animation):
OFFSET_X = -10
OFFSET_Y = -10
def __init__(self, manager, ball, frame, speed=1000):
Animation.__init__(self, manager, frame, speed)
self.ball = ball
def update(self):
Animation.update(self)
self.set_position_center(self.ball.rect.x + self.ball.rect.width + BallShadow.OFFSET_X,
self.ball.rect.y + self.ball.rect.height + BallShadow.OFFSET_Y)
class GoalScreen(Animation):
def __init__(self, manager, frame, speed=1000):
Animation.__init__(self, manager, frame, speed)
self.rect.x = 0
self.rect.y = 0
class Button(pygame.sprite.Sprite):
def __init__(self, frames, position):
pygame.sprite.Sprite.__init__(self)
self.frames = frames
self.position = position
self.rect = frames[0].get_rect()
self.rect.x = position[0]
self.rect.y = position[1]
self.image = frames[0]
self.radius = frames[0].get_rect().width / 2
self.current_frame = 0
def update(self, mouse, event_list):
if self.is_mouse_over(mouse):
self.image = self.frames[1]
else:
self.image = self.frames[0]
def is_mouse_over(self, mouse):
distance = math.pow(mouse[0] - self.rect.center[0], 2) + math.pow(mouse[1] - self.rect.center[1], 2)
return distance <= math.pow(self.radius, 2)
class SlideButton(Button):
def __init__(self, frames, position):
Button.__init__(self, frames, position)
def update(self, mouse, event_list):
pass
def toggle_frame(self):
self.current_frame = (self.current_frame + 1) % 2
self.image = self.frames[self.current_frame]