This commit is contained in:
Junior 2024-06-11 18:01:17 -04:00
commit 117502541a
19 changed files with 250 additions and 9 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.

9
Python/Examples/Rooms.py Normal file
View File

@ -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

View File

@ -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)

View File

@ -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."
}
]