Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- pygame.init()
- # Визначення розмірів вікна та інших параметрів
- width = 410
- height = 510
- screen = pygame.display.set_mode((width, height))
- pygame.display.set_caption("Вова_калькулятор")
- # Визначення кольорів
- background_color = (40, 40, 40)
- button_color = (100, 100, 100)
- text_color = (255, 255, 255)
- # Визначення розмірів та маркапу кнопок
- button_width = 70
- button_height = 70
- button_margin = 10
- button_font = pygame.font.Font(None, 36)
- # Список кнопок із їхніми координатами та значеннями
- buttons = [
- ("7", (10, 190), button_font),
- ("8", (90, 190), button_font),
- ("9", (170, 190), button_font),
- ("/", (250, 190), button_font),
- ("4", (10, 270), button_font),
- ("5", (90, 270), button_font),
- ("6", (170, 270), button_font),
- ("*", (250, 270), button_font),
- ("1", (10, 350), button_font),
- ("2", (90, 350), button_font),
- ("3", (170, 350), button_font),
- ("-", (250, 350), button_font),
- ("0", (90, 430), button_font),
- (".", (330, 430), button_font),
- ("=", (170, 430), button_font),
- ("+", (250, 430), button_font),
- ("C", (10, 430), button_font)
- ]
- # Ініціалізація змінних для обробки введеного виразу
- input_text = ""
- result_text = ""
- show_result = False
- def draw_buttons():
- for button in buttons:
- pygame.draw.rect(screen, button_color, pygame.Rect(button[1][0], button[1][1], button_width, button_height))
- text_surface = button[2].render(button[0], True, text_color)
- screen.blit(text_surface, (button[1][0] + button_width // 2 - text_surface.get_width() // 2, button[1][1] + button_height // 2 - text_surface.get_height() // 2))
- def update_display():
- screen.fill(background_color)
- draw_buttons()
- pygame.draw.rect(screen, button_color, pygame.Rect(button_margin, button_margin, width - 2 * button_margin, button_height), 2)
- pygame.draw.rect(screen, button_color, pygame.Rect(button_margin, 2 * button_margin + button_height, width - 2 * button_margin, button_height), 2)
- input_surface = button_font.render(input_text, True, text_color)
- screen.blit(input_surface, (20, 35))
- result_surface = button_font.render(result_text, True, text_color)
- screen.blit(result_surface, (20, 115))
- # Головний цикл програми
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- elif event.type == pygame.MOUSEBUTTONDOWN:
- mouse_x, mouse_y = pygame.mouse.get_pos()
- for button in buttons:
- button_rect = pygame.Rect(button[1][0], button[1][1], button_width, button_height)
- if button_rect.collidepoint(mouse_x, mouse_y):
- if button[0] == "=":
- try:
- result_text = str(eval(input_text))
- except:
- result_text = "Помилка"
- show_result = True
- elif button[0] == "C":
- input_text = ""
- result_text = ""
- show_result = False
- else:
- input_text += button[0]
- show_result = False
- update_display()
- pygame.display.flip()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment