Refactor to capitalize program globals. Add some more comments.
This commit is contained in:
parent
fd489d8de3
commit
e447a24348
|
@ -19,7 +19,8 @@ class Button:
|
||||||
flashing: bool = False
|
flashing: bool = False
|
||||||
unflash_on: float = 0.0
|
unflash_on: float = 0.0
|
||||||
|
|
||||||
buttons = {
|
# Define buttons and their associated keyboard keys
|
||||||
|
BUTTONS = {
|
||||||
str(pygame.K_1): Button(2, 0, pygame.K_1, "1", "8bit-fail.ogg"),
|
str(pygame.K_1): Button(2, 0, pygame.K_1, "1", "8bit-fail.ogg"),
|
||||||
str(pygame.K_2): Button(2, 1, pygame.K_2, "2", "brass-fail-8.ogg"),
|
str(pygame.K_2): Button(2, 1, pygame.K_2, "2", "brass-fail-8.ogg"),
|
||||||
str(pygame.K_3): Button(2, 2, pygame.K_3, "3", "brass-fail-11.ogg"),
|
str(pygame.K_3): Button(2, 2, pygame.K_3, "3", "brass-fail-11.ogg"),
|
||||||
|
@ -31,7 +32,8 @@ buttons = {
|
||||||
str(pygame.K_9): Button(0, 2, pygame.K_9, "9", "wrong.ogg")
|
str(pygame.K_9): Button(0, 2, pygame.K_9, "9", "wrong.ogg")
|
||||||
}
|
}
|
||||||
|
|
||||||
keypad_map = {
|
# Map number-pad numbers to normal keyboard numbers for simplicity
|
||||||
|
KEYPAD_MAP = {
|
||||||
str(pygame.K_KP_1): pygame.K_1,
|
str(pygame.K_KP_1): pygame.K_1,
|
||||||
str(pygame.K_KP_2): pygame.K_2,
|
str(pygame.K_KP_2): pygame.K_2,
|
||||||
str(pygame.K_KP_3): pygame.K_3,
|
str(pygame.K_KP_3): pygame.K_3,
|
||||||
|
@ -43,7 +45,8 @@ keypad_map = {
|
||||||
str(pygame.K_KP_9): pygame.K_9
|
str(pygame.K_KP_9): pygame.K_9
|
||||||
}
|
}
|
||||||
|
|
||||||
valid_keys_numbers = [
|
# The valid keyboard keys that represent numbers for buttons
|
||||||
|
VALID_KEYS_NUMBERS = [
|
||||||
pygame.K_1,
|
pygame.K_1,
|
||||||
pygame.K_2,
|
pygame.K_2,
|
||||||
pygame.K_3,
|
pygame.K_3,
|
||||||
|
@ -55,45 +58,46 @@ valid_keys_numbers = [
|
||||||
pygame.K_9
|
pygame.K_9
|
||||||
]
|
]
|
||||||
|
|
||||||
#pygame.mixer.pre_init(44100, 16, 2, 64000)
|
# Some program globals
|
||||||
|
WIDTH = 1000
|
||||||
|
HEIGHT = 975
|
||||||
|
BACKGROUND_COLOR = (150, 150, 150)
|
||||||
|
LABEL_COLOR = (255, 255, 255)
|
||||||
|
LABEL_COLOR_FLASH = (0, 0, 0)
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
#pygame.mixer.init()
|
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||||
|
|
||||||
width = 1000
|
|
||||||
height = 975
|
|
||||||
background_color = (150, 150, 150)
|
|
||||||
label_color = (255, 255, 255)
|
|
||||||
label_color_flash = (0, 0, 0)
|
|
||||||
|
|
||||||
screen = pygame.display.set_mode((width, height))
|
|
||||||
pygame.display.set_caption('Epic Fail Sound Board')
|
pygame.display.set_caption('Epic Fail Sound Board')
|
||||||
icon = pygame.image.load(os.path.join("img", "fail-icon.png"))
|
icon = pygame.image.load(os.path.join("img", "fail-icon.png"))
|
||||||
icon.convert_alpha()
|
icon.convert_alpha()
|
||||||
pygame.display.set_icon(icon)
|
pygame.display.set_icon(icon)
|
||||||
screen.fill(background_color)
|
screen.fill(BACKGROUND_COLOR)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
logo = pygame.image.load(os.path.join("img", "fail.png"))
|
logo = pygame.image.load(os.path.join("img", "fail.png"))
|
||||||
logo.convert_alpha()
|
logo.convert_alpha()
|
||||||
screen.blit(logo, ((width // 2) - (logo.get_width() // 2), 20))
|
screen.blit(logo, ((WIDTH // 2) - (logo.get_width() // 2), 20))
|
||||||
button_origin_x = 100
|
button_origin_x = 100
|
||||||
button_origin_y = logo.get_height() + 20 + 25
|
button_origin_y = logo.get_height() + 20 + 25
|
||||||
button_img = pygame.image.load(os.path.join("img", "button-red.png"))
|
button_img = pygame.image.load(os.path.join("img", "button-red.png"))
|
||||||
button_img.convert_alpha()
|
button_img.convert_alpha()
|
||||||
cheesy_font = pygame.font.Font(os.path.join("font", "New Cheese.ttf"), 60)
|
cheesy_font = pygame.font.Font(os.path.join("font", "New Cheese.ttf"), 60)
|
||||||
for b in buttons.values():
|
for b in BUTTONS.values():
|
||||||
x = button_origin_x + ((button_img.get_width() + 100) * b.column)
|
x = button_origin_x + ((button_img.get_width() + 100) * b.column)
|
||||||
y = button_origin_y + ((button_img.get_height() + 30) * b.row)
|
y = button_origin_y + ((button_img.get_height() + 30) * b.row)
|
||||||
b.center_x = x + (button_img.get_width() // 2)
|
b.center_x = x + (button_img.get_width() // 2)
|
||||||
b.center_y = y + (button_img.get_height() // 2)
|
b.center_y = y + (button_img.get_height() // 2)
|
||||||
screen.blit(button_img, (x, y))
|
screen.blit(button_img, (x, y))
|
||||||
b_text = cheesy_font.render(b.label, True, label_color)
|
b_text = cheesy_font.render(b.label, True, LABEL_COLOR)
|
||||||
text_x = x + (button_img.get_width() // 2) - (b_text.get_width() // 2)
|
text_x = x + (button_img.get_width() // 2) - (b_text.get_width() // 2)
|
||||||
text_y = y + (button_img.get_height() // 2) - (b_text.get_height() // 2)
|
text_y = y + (button_img.get_height() // 2) - (b_text.get_height() // 2)
|
||||||
screen.blit(b_text, (text_x, text_y))
|
screen.blit(b_text, (text_x, text_y))
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
|
# This function processes a button press/activation
|
||||||
|
# 1) Flash the button
|
||||||
|
# 2) Play the sound
|
||||||
def do_button(key):
|
def do_button(key):
|
||||||
for b in buttons.values():
|
for b in BUTTONS.values():
|
||||||
if b.key == key:
|
if b.key == key:
|
||||||
audio = pygame.mixer.Sound(os.path.join("snd", b.sound_file))
|
audio = pygame.mixer.Sound(os.path.join("snd", b.sound_file))
|
||||||
flash_button(b)
|
flash_button(b)
|
||||||
|
@ -101,11 +105,11 @@ def do_button(key):
|
||||||
|
|
||||||
def flash_button(b: Button):
|
def flash_button(b: Button):
|
||||||
if b.flashing == False:
|
if b.flashing == False:
|
||||||
text_color = label_color_flash
|
text_color = LABEL_COLOR_FLASH
|
||||||
b.flashing = True
|
b.flashing = True
|
||||||
b.unflash_on = time.time() + 0.1
|
b.unflash_on = time.time() + 0.1
|
||||||
else:
|
else:
|
||||||
text_color = label_color
|
text_color = LABEL_COLOR
|
||||||
b.flashing = False
|
b.flashing = False
|
||||||
b_text = cheesy_font.render(b.label, True, text_color)
|
b_text = cheesy_font.render(b.label, True, text_color)
|
||||||
text_x = b.center_x - (b_text.get_width() // 2)
|
text_x = b.center_x - (b_text.get_width() // 2)
|
||||||
|
@ -120,20 +124,20 @@ while True:
|
||||||
mouse_click = None
|
mouse_click = None
|
||||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
mouse_x, mouse_y = pygame.mouse.get_pos()
|
mouse_x, mouse_y = pygame.mouse.get_pos()
|
||||||
for b in buttons.values():
|
for b in BUTTONS.values():
|
||||||
if math.hypot(mouse_x - b.center_x, mouse_y - b.center_y) <= (button_img.get_width() // 2):
|
if math.hypot(mouse_x - b.center_x, mouse_y - b.center_y) <= (button_img.get_width() // 2):
|
||||||
mouse_click = b.key
|
mouse_click = b.key
|
||||||
if event.type == pygame.KEYDOWN or not mouse_click is None:
|
if event.type == pygame.KEYDOWN or not mouse_click is None:
|
||||||
if mouse_click is None:
|
if mouse_click is None:
|
||||||
my_key = keypad_map[str(event.key)] if str(event.key) in keypad_map.keys() else event.key
|
my_key = KEYPAD_MAP[str(event.key)] if str(event.key) in KEYPAD_MAP.keys() else event.key
|
||||||
else:
|
else:
|
||||||
my_key = mouse_click
|
my_key = mouse_click
|
||||||
if my_key == pygame.K_ESCAPE:
|
if my_key == pygame.K_ESCAPE:
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif my_key in valid_keys_numbers:
|
elif my_key in VALID_KEYS_NUMBERS:
|
||||||
do_button(my_key)
|
do_button(my_key)
|
||||||
for b in buttons.values():
|
for b in BUTTONS.values():
|
||||||
if b.flashing == True and time.time() >= b.unflash_on:
|
if b.flashing == True and time.time() >= b.unflash_on:
|
||||||
flash_button(b)
|
flash_button(b)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
Loading…
Reference in New Issue
Block a user