Allow decimal numbers

This commit is contained in:
Junior 2024-06-05 13:19:20 -04:00
parent 4fb59c12ec
commit f30bf3481c

View File

@ -8,8 +8,11 @@ pygame.init()
width = 600 width = 600
height = 900 height = 900
num_display = 0 clear_display = "0"
old_num_display = 0 display_num_whole = clear_display
old_display_num_whole = display_num_whole
display_num_decimal = ""
old_display_num_decimal = display_num_decimal
num_float = False num_float = False
float_precision = 1 float_precision = 1
yellow = (240, 240, 20) yellow = (240, 240, 20)
@ -27,6 +30,48 @@ class Button:
key: int key: int
is_number: bool = False is_number: bool = False
keypad_map = {
str(pygame.K_KP_0): pygame.K_0,
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,
str(pygame.K_KP_MINUS): pygame.K_MINUS,
str(pygame.K_KP_MULTIPLY): pygame.K_MINUS,
str(pygame.K_KP_DIVIDE): pygame.K_SLASH,
str(pygame.K_KP_PLUS): pygame.K_PLUS,
str(pygame.K_KP_PERIOD): pygame.K_PERIOD,
str(pygame.K_KP_ENTER): pygame.K_EQUALS,
str(pygame.K_RETURN): pygame.K_EQUALS
}
valid_keys_numbers = [
pygame.K_0,
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.K_KP_0,
pygame.K_KP_1,
pygame.K_KP_2,
pygame.K_KP_3,
pygame.K_KP_4,
pygame.K_KP_5,
pygame.K_KP_6,
pygame.K_KP_7,
pygame.K_KP_8,
pygame.K_KP_9
]
buttons = [ buttons = [
Button(3, 0, "0", pygame.K_0, True), Button(3, 0, "0", pygame.K_0, True),
Button(2, 0, "1", pygame.K_1, True), Button(2, 0, "1", pygame.K_1, True),
@ -39,7 +84,7 @@ buttons = [
Button(0, 1, "8", pygame.K_8, True), Button(0, 1, "8", pygame.K_8, True),
Button(0, 2, "9", pygame.K_9, True), Button(0, 2, "9", pygame.K_9, True),
Button(3, 1, ".", pygame.K_PERIOD), Button(3, 1, ".", pygame.K_PERIOD),
Button(3, 2, "?", pygame.K_QUESTION), Button(3, 2, "+-", pygame.K_QUESTION),
Button(0, 3, "<-", pygame.K_BACKSPACE), Button(0, 3, "<-", pygame.K_BACKSPACE),
Button(1, 3, "AC", pygame.K_a), Button(1, 3, "AC", pygame.K_a),
Button(2, 3, "C", pygame.K_c), Button(2, 3, "C", pygame.K_c),
@ -49,11 +94,10 @@ buttons = [
Button(4, 1, "-", pygame.K_MINUS), Button(4, 1, "-", pygame.K_MINUS),
Button(4, 2, chr(247), pygame.K_BACKSPACE) Button(4, 2, chr(247), pygame.K_BACKSPACE)
] ]
print(buttons[0])
def draw_buttons(): def draw_buttons():
for b in buttons: for b in buttons:
print(b)
center_x = button_origin_x + (b.column * button_spacing) center_x = button_origin_x + (b.column * button_spacing)
center_y = button_origin_y + (b.row * button_spacing) center_y = button_origin_y + (b.row * button_spacing)
center = (center_x, center_y) center = (center_x, center_y)
@ -87,30 +131,47 @@ while True:
pygame.quit() pygame.quit()
sys.exit() sys.exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_c: elif event.type == pygame.KEYDOWN and event.key == pygame.K_c:
num_display = 0 display_num_whole = clear_display
float_precision = 1 float_precision = 1
elif event.type == pygame.KEYDOWN and (event.key == pygame.K_PERIOD or event.key == pygame.K_KP_PERIOD): elif event.type == pygame.KEYDOWN and (event.key == pygame.K_PERIOD or event.key == pygame.K_KP_PERIOD):
#num_float = True num_float = True
pass elif event.type == pygame.KEYDOWN and event.key == pygame.K_BACKSPACE:
elif event.type == pygame.KEYDOWN \ if num_float == True:
and ((event.key >= pygame.K_0 and event.key <= pygame.K_9) or (event.key >= pygame.K_KP1 and event.key <= pygame.K_KP0)) \ if display_num_decimal == "":
and len(str(num_display)) <= 10: num_float = False
if event.key == pygame.K_KP0: if len(display_num_whole) > 1:
actual_key = pygame.K_0 display_num_whole = display_num_whole[:-1]
elif event.key >= pygame.K_KP1 and event.key <= pygame.K_KP9: elif display_num_whole != "0":
actual_key = event.key - 1073741864 display_num_whole = "0"
else:
display_num_decimal = display_num_decimal[:-1]
else:
if len(display_num_whole) > 1:
display_num_whole = display_num_whole[:-1]
elif display_num_whole != "0":
display_num_whole = "0"
elif event.type == pygame.KEYDOWN and event.key in valid_keys_numbers and len(display_num_whole) <= 10:
if str(event.key) in keypad_map.keys():
actual_key = keypad_map[str(event.key)]
else: else:
actual_key = event.key actual_key = event.key
if num_display == 0 and actual_key != pygame.K_0: if num_float == False:
num_display = int(chr(actual_key)) if display_num_whole == clear_display and actual_key != pygame.K_0:
display_num_whole = chr(actual_key)
else: else:
num_display = (num_display * 10) + int(chr(actual_key)) display_num_whole += chr(actual_key)
if num_display != old_num_display: else:
print(f"Number: {num_display}") display_num_decimal += chr(actual_key)
old_num_display = num_display if display_num_whole != old_display_num_whole or display_num_decimal != old_display_num_decimal:
print(f"Number: {display_num_whole}", end='')
if num_float == True:
print(".", end='')
print(display_num_decimal)
old_display_num_whole = display_num_whole
old_display_num_decimal = display_num_decimal
pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(12, 97, 576, 126)) pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(12, 97, 576, 126))
num_show = (num_float == False) num_show = (num_float == False)
text = sevenseg_font.render(str(num_display) + ".", True, white) text = sevenseg_font.render(display_num_whole + "." + display_num_decimal, True, white)
screen.blit(text, (580 - text.get_width(), 120)) screen.blit(text, (580 - text.get_width(), 120))
pygame.draw.rect(screen, yellow, pygame.Rect(10, 100, 580, 110), 2) pygame.draw.rect(screen, yellow, pygame.Rect(10, 100, 580, 110), 2)
pygame.display.flip() pygame.display.flip()