-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·147 lines (114 loc) · 4.06 KB
/
main.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
#!/usr/bin/env python3
# SPDX short identifier: GPL-3.0
import argparse
import glob
import random
import sys
import time
import pygame
SOUND_FILENAMES = glob.glob('sounds/*.ogg')
USAGE = """
On keypress: slightly change the color of the screen and play a sound.
When no action is taking place, fade out colors.
All of this done in an animated manner.
Quits on (ALT or CTRL) + (Q or F4)
"""
ARGS = None
LABEL = None
LABEL_POSITION = None
def get_cur_color(surface):
return tuple(surface.get_at((0, 0)))
def fill_frame(surface, color):
surface.fill(color)
surface.blit(LABEL, LABEL_POSITION)
pygame.display.update()
def main_loop(surface, sounds):
# Initialize which channel will be used for playing sounds
cur_channel = 0
while True:
event = pygame.event.poll()
if event.type == pygame.QUIT:
break
elif event.type == pygame.KEYDOWN:
if ARGS.print_key:
# Curious about what is being pressed?
print(pygame.key.name(event.key), end=" ")
sys.stdout.flush()
# Control quitting
mods = pygame.key.get_mods()
quit_key = event.key in (pygame.K_q, pygame.K_F4)
alt_or_ctrl = mods & (pygame.KMOD_ALT | pygame.KMOD_CTRL)
if quit_key and alt_or_ctrl:
print("Quitting...")
break
# Main actions!
# Fill screen with a solid color
def get_byte():
"""
Returns a random int from [0,255]
"""
return int(random.random() * 256)
target_color = (get_byte(), get_byte(), get_byte())
def int_easing(current, target):
"""
Eases @current to @target in a exponential matter
"""
return current + (target - current) / 10
next_color = map(int_easing, get_cur_color(surface), target_color)
fill_color = pygame.Color(*map(int, next_color))
fill_frame(surface, fill_color)
# Play a random sound and try to keep playing previous sound as long as
# possible (use all the channels available)
sound_index = int(random.random() * len(sounds))
sound = sounds[sound_index]
ch = pygame.mixer.Channel(cur_channel)
cur_channel = (cur_channel + 1) % pygame.mixer.get_num_channels()
ch.play(sound)
elif event.type == pygame.NOEVENT:
# Dim color to black
def dim_color(tup):
"""
Returns a darker @tup color until its black
"""
def keep_within_boundaries_add(a, b):
"""
Restrict a + b to [0,255]
"""
result = a + b
if result < 0:
return 0
if result > 255:
return 255
return result
return map(keep_within_boundaries_add, tup, (-1, -1, -1, 0))
new_color = pygame.Color(*dim_color(get_cur_color(surface)))
fill_frame(surface, new_color)
# Wait something like a frame for performance concerns
time.sleep(1.0 / 60)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=USAGE,
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--window', dest='window', action='store_true',
default=False, help='run on window mode instead of fullscreen')
parser.add_argument('--hide-key', dest='print_key', action='store_false',
default=True, help='print which keys are being pressed')
ARGS = parser.parse_args()
mode = pygame.FULLSCREEN if not ARGS.window else 0
resolution = (0, 0) if not ARGS.window else (800, 600)
# Pygame initialization
pygame.init()
pygame.mixer.init()
pygame.mouse.set_visible(False)
font = pygame.font.SysFont(pygame.font.get_default_font(), 24)
# render text and cache it
LABEL = font.render("Press Alt+F4 or Ctrl+Q to quit.", 1, (255, 255, 255))
# Sound caching
sounds = [pygame.mixer.Sound(i) for i in SOUND_FILENAMES]
surface = pygame.display.set_mode(resolution, mode)
info = pygame.display.Info()
# Centralize text on screen and cache it
label_rect = LABEL.get_rect()
LABEL_POSITION = (info.current_w / 2 - label_rect.width / 2,
info.current_h / 2 - label_rect.height / 2,)
main_loop(surface, sounds)
pygame.quit()