This commit is contained in:
Stephen Deaton 2024-06-11 17:14:09 -04:00
commit cdabfcc6d4
17 changed files with 206 additions and 4 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.swp
__pycache__
*.pyc
*.pyc
desktop.ini

View File

@ -17,7 +17,12 @@ old_display_num_whole = display_num_whole
display_num_decimal = ""
old_display_num_decimal = display_num_decimal
num_float = False
buffer_num = ""
buffer_num_float = False
operator = ""
old_operator = ""
float_precision = 1
yellow = (240, 240, 20)
white = (255, 255, 255)
black = (0, 0, 0)
@ -159,9 +164,17 @@ logo_text_2 = cheesy_font.render("Calculator", True, yellow)
screen.blit(logo_text_1, (width // 2 - logo_text_1.get_width() // 2 + 80, 0))
screen.blit(logo_text_2, (width // 2 - logo_text_2.get_width() // 2 + 80, 45))
sevenseg_font = pygame.font.Font(os.path.join("font", "DSEG7Modern-Bold.ttf"), 60)
sevenseg_font_sm = pygame.font.Font(os.path.join("font", "Seven Segment.ttf"), 22)
draw_buttons()
pygame.display.flip()
def do_math():
if operator == "+":
num1 = int(buffer_num) if buffer_num_float == False else float(buffer_num)
num1_string = display_num_whole + ("" if num_float == False else ("." + display_num_decimal))
num2 = int(num1_string) if num_float == False else float(num1_string)
return str(num1 + num2)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
@ -192,6 +205,10 @@ while True:
display_num_decimal = ""
num_float = False
float_precision = 1
buffer_num = ""
buffer_num_float = False
operator = ""
old_operator = ""
flash_button(buttons[str(my_key)])
elif my_key == pygame.K_PERIOD or my_key == pygame.K_KP_PERIOD:
actual_key = pygame.K_PERIOD
@ -218,6 +235,25 @@ while True:
display_num_whole = "0"
elif (my_key == pygame.K_EQUALS and pygame.key.get_mods() & pygame.KMOD_SHIFT) or my_key == pygame.K_KP_PLUS:
actual_key = pygame.K_PLUS
if buffer_num == "":
buffer_num = display_num_whole
operator = "+"
if num_float:
buffer_num_float = True
buffer_num += "." + display_num_decimal
num_float = False
display_num_whole = clear_display
display_num_decimal = ""
else:
sum = do_math()
if sum.find(".") == -1:
display_num_whole = sum
num_float = False
else:
display_num_whole, display_num_decimal = sum.split('.')
if display_num_decimal == "0":
display_num_decimal = ""
num_float = False
flash_button(buttons[str(actual_key)])
elif my_key == pygame.K_MINUS or my_key == pygame.K_KP_MINUS:
actual_key = pygame.K_MINUS
@ -251,8 +287,10 @@ while True:
old_display_num_decimal = display_num_decimal
pygame.draw.rect(screen, black, pygame.Rect(10, 100, width - 20, 110))
pygame.draw.rect(screen, yellow, pygame.Rect(10, 100, width - 20, 110), 2)
text = sevenseg_font.render(display_num_whole + "." + display_num_decimal, True, white)
screen.blit(text, (width - 20 - text.get_width(), 120))
main_text = sevenseg_font.render(display_num_whole + "." + display_num_decimal, True, white)
screen.blit(main_text, (width - 20 - main_text.get_width(), 130))
buffer_text = sevenseg_font_sm.render(buffer_num + " " + operator, True, white)
screen.blit(buffer_text, (width - 20 - buffer_text.get_width(), 130 - 4 - buffer_text.get_height()))
for key, b in buttons.items():
if b.flashing == True and time.time() >= b.unflash_on:
flash_button(b)

139
PyGame/FailBoard/fail.py Normal file
View File

@ -0,0 +1,139 @@
from dataclasses import dataclass
from datetime import datetime
import math
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
import sys
import time
@dataclass
class Button:
row: int
column: int
key: int
label: str
sound_file: str
center_x: int = 0
center_y: int = 0
flashing: bool = False
unflash_on: float = 0.0
buttons = {
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_3): Button(2, 2, pygame.K_3, "3", "brass-fail-11.ogg"),
str(pygame.K_4): Button(1, 0, pygame.K_4, "4", "error.ogg"),
str(pygame.K_5): Button(1, 1, pygame.K_5, "5", "fail-1.ogg"),
str(pygame.K_6): Button(1, 2, pygame.K_6, "6", "game-over.ogg"),
str(pygame.K_7): Button(0, 0, pygame.K_7, "7", "invalid.ogg"),
str(pygame.K_8): Button(0, 1, pygame.K_8, "8", "wah-wah.ogg"),
str(pygame.K_9): Button(0, 2, pygame.K_9, "9", "wrong.ogg")
}
keypad_map = {
str(pygame.K_KP_1): pygame.K_1,
str(pygame.K_KP_2): pygame.K_2,
str(pygame.K_KP_3): pygame.K_3,
str(pygame.K_KP_4): pygame.K_4,
str(pygame.K_KP_5): pygame.K_5,
str(pygame.K_KP_6): pygame.K_6,
str(pygame.K_KP_7): pygame.K_7,
str(pygame.K_KP_8): pygame.K_8,
str(pygame.K_KP_9): pygame.K_9
}
valid_keys_numbers = [
pygame.K_1,
pygame.K_2,
pygame.K_3,
pygame.K_4,
pygame.K_5,
pygame.K_6,
pygame.K_7,
pygame.K_8,
pygame.K_9
]
#pygame.mixer.pre_init(44100, 16, 2, 64000)
pygame.init()
#pygame.mixer.init()
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')
icon = pygame.image.load(os.path.join("img", "fail-icon.png"))
icon.convert_alpha()
pygame.display.set_icon(icon)
screen.fill(background_color)
pygame.display.flip()
logo = pygame.image.load(os.path.join("img", "fail.png"))
logo.convert_alpha()
screen.blit(logo, ((width // 2) - (logo.get_width() // 2), 20))
button_origin_x = 100
button_origin_y = logo.get_height() + 20 + 25
button_img = pygame.image.load(os.path.join("img", "button-red.png"))
button_img.convert_alpha()
cheesy_font = pygame.font.Font(os.path.join("font", "New Cheese.ttf"), 60)
for b in buttons.values():
x = button_origin_x + ((button_img.get_width() + 100) * b.column)
y = button_origin_y + ((button_img.get_height() + 30) * b.row)
b.center_x = x + (button_img.get_width() // 2)
b.center_y = y + (button_img.get_height() // 2)
screen.blit(button_img, (x, y))
b_text = cheesy_font.render(b.label, True, label_color)
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)
screen.blit(b_text, (text_x, text_y))
pygame.display.flip()
def do_button(key):
for b in buttons.values():
if b.key == key:
audio = pygame.mixer.Sound(os.path.join("snd", b.sound_file))
flash_button(b)
audio.play()
def flash_button(b: Button):
if b.flashing == False:
text_color = label_color_flash
b.flashing = True
b.unflash_on = time.time() + 0.1
else:
text_color = label_color
b.flashing = False
b_text = cheesy_font.render(b.label, True, text_color)
text_x = b.center_x - (b_text.get_width() // 2)
text_y = b.center_y - (b_text.get_height() // 2)
screen.blit(b_text, (text_x, text_y))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
mouse_click = None
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
for b in buttons.values():
if math.hypot(mouse_x - b.center_x, mouse_y - b.center_y) <= (button_img.get_width() // 2):
mouse_click = b.key
if event.type == pygame.KEYDOWN or not 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
else:
my_key = mouse_click
if my_key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
elif my_key in valid_keys_numbers:
do_button(my_key)
for b in buttons.values():
if b.flashing == True and time.time() >= b.unflash_on:
flash_button(b)
pygame.display.flip()

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,5 +1,6 @@
# Data Structures
# Data structures hold sets of data in various formats
# We will discuss: list, dictionary (dict), tuple, set
# List
# Lists have collections of data
@ -73,4 +74,27 @@ print(f"Color tuple: R={color[0]} G={color[1]} B={color[2]}")
# strings: "Cheese"
# integers: 44
# lists: ['a', 'b', 'c']
# objects: MyClass(param1, param2)
# objects: MyClass(param1, param2)
# Set
# Sets have collections of data
# set elements must be unique in the set
# Usually of the same type because uniqueness makes less sense otherwise
# with elements separated by commas
# where elements are unordered (i.e. can't reference specific element, slice, or index)
# are contained within curly braces
# Note: Once a set has been created its values cannot be changed,
# and elements cannot be added/removed
# Declare our set
dice = {"1", "2", "3", "4", "5", "6"}
# Testing if an element exists is done with "in"
if "3" in dice:
print("The dice set contains a 3!")
# Iterate over a set
for die in dice:
print(f"dice element: {die}")
# NOTE!! You will see that the printed elements are in an arbitrary order.
# Because sets are unordered you cannot rely on which element comes
# before or after another element and will likely not match the order
# created inside the set declaration.