diff --git a/.gitignore b/.gitignore index c64ed70..18c826b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.swp __pycache__ -*.pyc \ No newline at end of file +*.pyc +desktop.ini \ No newline at end of file diff --git a/PyGame/Calculator/calc.py b/PyGame/Calculator/calc.py index 40c8f96..b59d550 100644 --- a/PyGame/Calculator/calc.py +++ b/PyGame/Calculator/calc.py @@ -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) diff --git a/PyGame/FailBoard/fail.py b/PyGame/FailBoard/fail.py new file mode 100644 index 0000000..a04c859 --- /dev/null +++ b/PyGame/FailBoard/fail.py @@ -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() \ No newline at end of file diff --git a/PyGame/FailBoard/font/New Cheese.ttf b/PyGame/FailBoard/font/New Cheese.ttf new file mode 100644 index 0000000..350d36c Binary files /dev/null and b/PyGame/FailBoard/font/New Cheese.ttf differ diff --git a/PyGame/FailBoard/img/button-red.png b/PyGame/FailBoard/img/button-red.png new file mode 100644 index 0000000..8e87f0a Binary files /dev/null and b/PyGame/FailBoard/img/button-red.png differ diff --git a/PyGame/FailBoard/img/fail-icon.png b/PyGame/FailBoard/img/fail-icon.png new file mode 100644 index 0000000..3ae9cd1 Binary files /dev/null and b/PyGame/FailBoard/img/fail-icon.png differ diff --git a/PyGame/FailBoard/img/fail.png b/PyGame/FailBoard/img/fail.png new file mode 100644 index 0000000..7985f83 Binary files /dev/null and b/PyGame/FailBoard/img/fail.png differ diff --git a/PyGame/FailBoard/snd/8bit-fail.ogg b/PyGame/FailBoard/snd/8bit-fail.ogg new file mode 100644 index 0000000..b13bf68 Binary files /dev/null and b/PyGame/FailBoard/snd/8bit-fail.ogg differ diff --git a/PyGame/FailBoard/snd/brass-fail-11.ogg b/PyGame/FailBoard/snd/brass-fail-11.ogg new file mode 100644 index 0000000..19f0b52 Binary files /dev/null and b/PyGame/FailBoard/snd/brass-fail-11.ogg differ diff --git a/PyGame/FailBoard/snd/brass-fail-8.ogg b/PyGame/FailBoard/snd/brass-fail-8.ogg new file mode 100644 index 0000000..3888ff3 Binary files /dev/null and b/PyGame/FailBoard/snd/brass-fail-8.ogg differ diff --git a/PyGame/FailBoard/snd/error.ogg b/PyGame/FailBoard/snd/error.ogg new file mode 100644 index 0000000..fc0a05c Binary files /dev/null and b/PyGame/FailBoard/snd/error.ogg differ diff --git a/PyGame/FailBoard/snd/fail-1.ogg b/PyGame/FailBoard/snd/fail-1.ogg new file mode 100644 index 0000000..00c1bdc Binary files /dev/null and b/PyGame/FailBoard/snd/fail-1.ogg differ diff --git a/PyGame/FailBoard/snd/game-over.ogg b/PyGame/FailBoard/snd/game-over.ogg new file mode 100644 index 0000000..bca08fc Binary files /dev/null and b/PyGame/FailBoard/snd/game-over.ogg differ diff --git a/PyGame/FailBoard/snd/invalid.ogg b/PyGame/FailBoard/snd/invalid.ogg new file mode 100644 index 0000000..9aca1f2 Binary files /dev/null and b/PyGame/FailBoard/snd/invalid.ogg differ diff --git a/PyGame/FailBoard/snd/wah-wah.ogg b/PyGame/FailBoard/snd/wah-wah.ogg new file mode 100644 index 0000000..c8d27ac Binary files /dev/null and b/PyGame/FailBoard/snd/wah-wah.ogg differ diff --git a/PyGame/FailBoard/snd/wrong.ogg b/PyGame/FailBoard/snd/wrong.ogg new file mode 100644 index 0000000..84bb2a9 Binary files /dev/null and b/PyGame/FailBoard/snd/wrong.ogg differ diff --git a/Python/Examples/Rooms.py b/Python/Examples/Rooms.py new file mode 100644 index 0000000..2c74a5a --- /dev/null +++ b/Python/Examples/Rooms.py @@ -0,0 +1,9 @@ +from dataclasses import dataclass + +@dataclass +class Room(): + grid_col: int = 0 + grid_row: int = 0 + name: str = "" + description: str = "" + dark: bool = False \ No newline at end of file diff --git a/Python/Examples/dice.py b/Python/Examples/dice.py index 41ead19..5e14e6d 100644 --- a/Python/Examples/dice.py +++ b/Python/Examples/dice.py @@ -1,13 +1,51 @@ -dice = ["1", "2", "3", "4"] +import json +import random +from pathlib import Path +from typing import List +from Rooms import Room -def parse_input(input_string): - if input_string.strip() in dice: +my_rooms = [] +raw_room_data = Path('room_data.json').read_text() +room_data = json.loads(raw_room_data) +for room in room_data: + my_rooms.append(Room(room['grid_col'], room['grid_row'], room['name'], room['description'], room['dark'])) + +dice = ["1", "2",] +sides = ["4", "6", "8", "10", "12", "20"] + +def parse_input(input_string: str, my_list: List): + if input_string.strip() in my_list: return int(input_string) else: - print(f"Please enter a number from {dice[0]} to {dice[-1]}.") - raise SystemExit(1) + show_list = "" + for element in my_list: + show_list += f"{element}," + show_list = show_list[:-1] + print(f"Please enter a number from [{show_list}].") + raise SystemExit(1) +def roll_dice(num_dice: int, num_sides: int = 6): + roll_results = [] + for _ in range(num_dice): + roll = random.randint(1, num_sides) + roll_results.append(roll) + return roll_results + +#User Inout on howmany dice to roll num_dice_input = input(f"How many dice do you want to roll? [{dice[0]}-{dice[-1]}] ") -num_dice = parse_input(num_dice_input) +num_dice = parse_input(num_dice_input, dice) print(num_dice) +my_list = "" +for sided in sides: + my_list += f"{sided}," +my_list = my_list[:-1] +num_sides_input = input(f"How many sides from [{my_list}] ") +num_sides = parse_input(num_sides_input, sides) +print(num_sides) + +#Results of ice roll + +num_dice_input = roll_dice(num_dice, num_sides) + +print(num_dice_input) \ No newline at end of file diff --git a/Python/Examples/room_data.json b/Python/Examples/room_data.json new file mode 100644 index 0000000..d229cc8 --- /dev/null +++ b/Python/Examples/room_data.json @@ -0,0 +1,16 @@ +[ + { + "grid_col": 0, + "grid_row": 0, + "name": "Forest Clearing", + "dark": false, + "description": "You see a large, dirty rock in the center of a small clearing. Several birds rest on the rock." + }, + { + "grid_col": 1, + "grid_row": 0, + "name": "Narrow Path", + "dark": true, + "description": "You are on a narrow ledge following near the ridge of a steep mountain path." + } +] \ No newline at end of file