Add buttons to calculator. Change Cheese font to one that includes numbers.

This commit is contained in:
Junior 2024-06-05 12:00:25 -04:00
parent 004afb7fce
commit 4fb59c12ec
3 changed files with 61 additions and 7 deletions

View File

@ -1,3 +1,4 @@
from dataclasses import dataclass
import os import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame import pygame
@ -11,6 +12,58 @@ num_display = 0
old_num_display = 0 old_num_display = 0
num_float = False num_float = False
float_precision = 1 float_precision = 1
yellow = (240, 240, 20)
white = (255, 255, 255)
button_origin_x = 90
button_origin_y = 280
button_spacing = 140
@dataclass
class Button:
row: int
column: int
label: str
key: int
is_number: bool = False
buttons = [
Button(3, 0, "0", pygame.K_0, True),
Button(2, 0, "1", pygame.K_1, True),
Button(2, 1, "2", pygame.K_2, True),
Button(2, 2, "3", pygame.K_3, True),
Button(1, 0, "4", pygame.K_4, True),
Button(1, 1, "5", pygame.K_5, True),
Button(1, 2, "6", pygame.K_6, True),
Button(0, 0, "7", pygame.K_7, True),
Button(0, 1, "8", pygame.K_8, True),
Button(0, 2, "9", pygame.K_9, True),
Button(3, 1, ".", pygame.K_PERIOD),
Button(3, 2, "?", pygame.K_QUESTION),
Button(0, 3, "<-", pygame.K_BACKSPACE),
Button(1, 3, "AC", pygame.K_a),
Button(2, 3, "C", pygame.K_c),
Button(3, 3, "=", pygame.K_EQUALS),
Button(4, 3, "*", pygame.K_ASTERISK),
Button(4, 0, "+", pygame.K_PLUS),
Button(4, 1, "-", pygame.K_MINUS),
Button(4, 2, chr(247), pygame.K_BACKSPACE)
]
print(buttons[0])
def draw_buttons():
for b in buttons:
print(b)
center_x = button_origin_x + (b.column * button_spacing)
center_y = button_origin_y + (b.row * button_spacing)
center = (center_x, center_y)
pygame.draw.circle(screen, yellow, center, 50, 2)
b_text = cheesy_font.render(b.label, True, white)
text_x = center_x - (b_text.get_width() // 2)
text_y = center_y - (b_text.get_height() // 2)
if b.label == "*":
text_y += 15
screen.blit(b_text, (text_x, text_y))
screen = pygame.display.set_mode((width, height)) screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('CheesyCalc') pygame.display.set_caption('CheesyCalc')
@ -20,12 +73,13 @@ pygame.display.set_icon(icon)
logo = pygame.image.load(os.path.join("img", "cheesy.png")) logo = pygame.image.load(os.path.join("img", "cheesy.png"))
logo.convert_alpha() logo.convert_alpha()
screen.blit(logo, (30, 20)) screen.blit(logo, (30, 20))
cheesy_font = pygame.font.Font(os.path.join("font", "Cheese.ttf"), 60) cheesy_font = pygame.font.Font(os.path.join("font", "New Cheese.ttf"), 60)
logo_text_1 = cheesy_font.render("Cheesy", True, (240, 240, 20)) logo_text_1 = cheesy_font.render("Cheesy", True, yellow)
logo_text_2 = cheesy_font.render("Calculator", True, (240, 240, 20)) logo_text_2 = cheesy_font.render("Calculator", True, yellow)
screen.blit(logo_text_1, (width // 2 - logo_text_1.get_width() // 2 + 80, -15)) 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, 30)) 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 = pygame.font.Font(os.path.join("font", "DSEG7Modern-Bold.ttf"), 60)
draw_buttons()
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
@ -56,7 +110,7 @@ while True:
old_num_display = num_display old_num_display = num_display
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, (255, 255, 255)) text = sevenseg_font.render(str(num_display) + ".", True, white)
screen.blit(text, (580 - text.get_width(), 120)) screen.blit(text, (580 - text.get_width(), 120))
pygame.draw.rect(screen, (240, 240, 20), 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()

Binary file not shown.

Binary file not shown.