From c5e5ca214bf746ca63d17478683eba7d6888d603 Mon Sep 17 00:00:00 2001 From: Junior Date: Fri, 7 Jun 2024 13:46:32 -0400 Subject: [PATCH] Getting started on addition --- PyGame/Calculator/calc.py | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) 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)