-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.py
executable file
·150 lines (126 loc) · 5.64 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
import pygame
import random
import Constants as Const
# created by Max-Lo on 15.03.2016
# methods to animate the puzzle a little bit
# to improve or tune the look a bit
# a lot of animation stuff to make the game look better
def animation_right_linear(board, puzzle):
screen = pygame.display.get_surface()
field = puzzle.field
ef = puzzle.empty_field
ef_old = (puzzle.empty_field[0] + 1, puzzle.empty_field[1])
clock2 = pygame.time.Clock()
for i in range(Const.ANIMATION_SPEED):
i *= 100 / Const.ANIMATION_SPEED
clock2.tick(60)
# draw old and new empty Tile
pygame.draw.rect(board, Const.LT, (ef[0] * 100 + 1, ef[1] * 100 + 1, 98, 98))
pygame.draw.rect(board, Const.LT, (ef_old[0] * 100 + 1, ef_old[1] * 100 + 1, 98, 98))
# draw shifting number tile
pygame.draw.rect(board, Const.DK, (ef[0] * 100 + i, ef[1] * 100 + 1, 98, 98))
font = pygame.font.Font(None, 50)
mytext = font.render(str(field[ef_old[0]][ef_old[1]]), 1, Const.WHITE)
textpos = mytext.get_rect(centerx=ef[0] * 100 + 50 + i, centery=ef[1] * 100 + 50)
board.blit(mytext, textpos)
screen.blit(board, (25, 100))
pygame.display.flip()
def animation_left_linear(board, puzzle):
screen = pygame.display.get_surface()
field = puzzle.field
ef = puzzle.empty_field
ef_old = (puzzle.empty_field[0] - 1, puzzle.empty_field[1])
clock2 = pygame.time.Clock()
for i in range(Const.ANIMATION_SPEED):
i *= 100 / Const.ANIMATION_SPEED
clock2.tick(60)
# draw old and new empty Tile
pygame.draw.rect(board, Const.LT, (ef[0] * 100 + 1, ef[1] * 100 + 1, 98, 98))
pygame.draw.rect(board, Const.LT, (ef_old[0] * 100 + 1, ef_old[1] * 100 + 1, 98, 98))
# draw shifting number tile
pygame.draw.rect(board, Const.DK, (ef[0] * 100 - i, ef[1] * 100 + 1, 98, 98))
font = pygame.font.Font(None, 50)
mytext = font.render(str(field[ef_old[0]][ef_old[1]]), 1, Const.WHITE)
textpos = mytext.get_rect(centerx=ef[0] * 100 + 50 - i, centery=ef[1] * 100 + 50)
board.blit(mytext, textpos)
screen.blit(board, (25, 100))
pygame.display.flip()
def animation_up_linear(board, puzzle):
screen = pygame.display.get_surface()
field = puzzle.field
ef = puzzle.empty_field
ef_old = (puzzle.empty_field[0], puzzle.empty_field[1] - 1)
clock2 = pygame.time.Clock()
for i in range(Const.ANIMATION_SPEED):
i *= 100 / Const.ANIMATION_SPEED
clock2.tick(60)
# draw old and new empty Tile
pygame.draw.rect(board, Const.LT, (ef[0] * 100 + 1, ef[1] * 100 + 1, 98, 98))
pygame.draw.rect(board, Const.LT, (ef_old[0] * 100 + 1, ef_old[1] * 100 + 1, 98, 98))
# draw shifting number tile
pygame.draw.rect(board, Const.DK, (ef[0] * 100 + 1, ef[1] * 100 + 1 - i, 98, 98))
font = pygame.font.Font(None, 50)
mytext = font.render(str(field[ef_old[0]][ef_old[1]]), 1, Const.WHITE)
textpos = mytext.get_rect(centerx=ef[0] * 100 + 50, centery=ef[1] * 100 + 50 - i)
board.blit(mytext, textpos)
screen.blit(board, (25, 100))
pygame.display.flip()
def animation_down_linear(board, puzzle):
screen = pygame.display.get_surface()
field = puzzle.field
ef = puzzle.empty_field
ef_old = (puzzle.empty_field[0], puzzle.empty_field[1] + 1)
clock2 = pygame.time.Clock()
for i in range(Const.ANIMATION_SPEED):
i *= 100 / Const.ANIMATION_SPEED
clock2.tick(60)
# draw old and new empty Tile
pygame.draw.rect(board, Const.LT, (ef[0] * 100 + 1, ef[1] * 100 + 1, 98, 98))
pygame.draw.rect(board, Const.LT, (ef_old[0] * 100 + 1, ef_old[1] * 100 + 1, 98, 98))
# draw shifting number tile
pygame.draw.rect(board, Const.DK, (ef[0] * 100 + 1, ef[1] * 100 + 1 + i, 98, 98))
font = pygame.font.Font(None, 50)
mytext = font.render(str(field[ef_old[0]][ef_old[1]]), 1, Const.WHITE)
textpos = mytext.get_rect(centerx=ef[0] * 100 + 50, centery=ef[1] * 100 + 50 + i)
board.blit(mytext, textpos)
screen.blit(board, (25, 100))
pygame.display.flip()
def animation_tremble(screen, board, blank_screen):
clock = pygame.time.Clock()
for i in range(15):
clock.tick(60)
screen.blit(blank_screen, (0, 0))
board_x = 25 + random.randint(-10, 10)
board_y = 100 + random.randint(-10, 10)
screen.blit(board, (board_x, board_y))
pygame.display.flip()
def init_particle_spring(screen):
bottom_middle = (screen.get_width()/2, screen.get_height())
global rect
rect = [create_particle(bottom_middle) for _ in range(50)]
def animation_particle_spring(screen):
bottom_middle = (screen.get_width()/2, screen.get_height())
for element in rect:
pygame.draw.rect(screen, element[3], (element[0], element[1], element[2], element[2]))
element[1] -= 4
y = bottom_middle[1] - element[1]
x = element[4]*y**2
element[0] += x
# eliminate off screen particles
if element[0] < 0 or element[0] > screen.get_width() or element[1] < 0:
rect.remove(element)
rect.append(create_particle(bottom_middle))
def create_particle(start):
# particle color
red = random.randint(0, 180)
green = random.randint(0, 180)
blue = random.randint(0, 180)
color = (red, green, blue)
# size of the particle
size = random.randint(5, 25)
# how narrow the curve is
a = 1.0 / random.randint(1000, 100000)
# whether flying a left or right curve
if random.randint(0, 1) == 1:
a = -a
return [start[0], start[1], size, color, a]