-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorize.py
More file actions
118 lines (107 loc) · 4.53 KB
/
colorize.py
File metadata and controls
118 lines (107 loc) · 4.53 KB
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
import goodgame as gg
import scene
import main
class Scene(scene.Scene):
def __init__(self, renderer: main.Renderer, data: any) -> None:
super().__init__(renderer, data)
self.destroyed = True
self.should_load = True
self.a: main.App = renderer.app
self.w: main.Window = renderer.window
self.r: main.Renderer = renderer
self.w.set_resizable(True)
self.size = self.r.get_output_size()
bm_tex = self.r.texture_from_surface(data[1])
bm_tex.set_scale_mode('linear')
self.fps_font = gg.BMFont(self.r, data[0], {
'goldFont-hd.png': bm_tex
})
self.images = tuple(data[2:])
self.current_id = 0
self.tex_w = 0
self.texture = self.create_texture()
self.colors = ((0, 0, 255), (255, 0, 255), (255, 0, 0), (255, 255, 0), (0, 255, 0), (0, 255, 255))
self.color_id = 1
self.from_color = self.colors[0]
self.color = gg.Animation(4, enabled=True)
self.color.calc = self.calc
self.color_speed = (
(self.colors[1][0] - self.from_color[0]) / self.color.duration,
(self.colors[1][1] - self.from_color[1]) / self.color.duration,
(self.colors[1][2] - self.from_color[2]) / self.color.duration
)
self.bg_anim = gg.Animation(5, repeat=True, enabled=True)
self.bg_anim.calc = lambda x: -self.tex_w / self.bg_anim.duration * x
self.destroyed = False
self.a.clock.reset()
def calc(self, x: float) -> tuple:
return self.from_color[0] + x * self.color_speed[0],\
self.from_color[1] + x * self.color_speed[1],\
self.from_color[2] + x * self.color_speed[2]
def update(self, dt: float) -> None:
self.bg_anim.tick(dt)
self.color.tick(dt)
if not self.color.enabled:
self.color_id += 1
if self.color_id >= len(self.colors):
self.color_id = 0
self.from_color = self.color.value
self.color_speed = (
(self.colors[self.color_id][0] - self.from_color[0]) / self.color.duration,
(self.colors[self.color_id][1] - self.from_color[1]) / self.color.duration,
(self.colors[self.color_id][2] - self.from_color[2]) / self.color.duration
)
self.color.run()
self.texture.set_color_mod(self.color.value)
self.r.blit(self.texture, dst_rect=(self.bg_anim.value, 0))
self.r.blit(
self.fps_font.render(f'FPS: {self.a.clock.get_fps()}'),
dst_rect=(0, -self.fps_font.common['base'] // 2)
)
self.r.flip()
def create_texture(self) -> gg.Texture:
surf: gg.Surface = self.images[self.current_id]
tex: gg.Texture = self.r.texture_from_surface(surf)
tex.set_scale_mode('linear')
scale = self.size[1] / surf.h
w_scale = surf.w * scale
result = self.r.create_texture((
int(self.size[0] / w_scale + 2) * w_scale,
surf.h
), self.r.pixel_format_from_str('rgb888'))
self.r.set_target(result)
for x in range(int(result.get_w() // w_scale) + 1):
self.r.blit(tex, dst_rect=(x * w_scale, 0, w_scale, self.size[1]))
self.r.set_target(None)
self.tex_w = w_scale
return result
def next(self) -> None:
self.current_id += 1
if self.current_id >= len(self.images):
self.current_id = 0
self.texture = self.create_texture()
def on_key_down(self, event: gg.KeyboardEvent) -> None:
if event.sym in ('AC Back', 'Escape'):
self.r.load_scene(main.Scene)
elif event.sym in ('Space', 'Return'):
self.next()
def on_mouse_down(self, event: gg.MouseButtonEvent) -> None:
self.next()
def on_resize(self, event: gg.WindowEvent) -> None:
self.size = self.r.get_output_size()
self.texture = self.create_texture()
@staticmethod
def get_resources() -> tuple:
return (
('text', 'goldFont-hd.fnt'),
('image', 'goldFont-hd.png')
) + tuple(('image', f'game_bg_0{x}_001-hd.png') for x in range(1, 10)) +\
tuple(('image', f'game_bg_{x}_001-hd.png') for x in range(10, 20))
def destroy(self) -> bool:
if super().destroy():
return True
self.fps_font.destroy()
del self.r
del self.w
del self.a
return False