Okonar

Untitled

May 26th, 2024
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. import pygame
  2. pygame.init()
  3.  
  4. # Визначення розмірів вікна та інших параметрів
  5. width = 410
  6. height = 510
  7. screen = pygame.display.set_mode((width, height))
  8. pygame.display.set_caption("Вова_калькулятор")
  9.  
  10. # Визначення кольорів
  11. background_color = (40, 40, 40)
  12. button_color = (100, 100, 100)
  13. text_color = (255, 255, 255)
  14.  
  15. # Визначення розмірів та маркапу кнопок
  16. button_width = 70
  17. button_height = 70
  18. button_margin = 10
  19. button_font = pygame.font.Font(None, 36)
  20.  
  21. # Список кнопок із їхніми координатами та значеннями
  22. buttons = [
  23. ("7", (10, 190), button_font),
  24. ("8", (90, 190), button_font),
  25. ("9", (170, 190), button_font),
  26. ("/", (250, 190), button_font),
  27. ("4", (10, 270), button_font),
  28. ("5", (90, 270), button_font),
  29. ("6", (170, 270), button_font),
  30. ("*", (250, 270), button_font),
  31. ("1", (10, 350), button_font),
  32. ("2", (90, 350), button_font),
  33. ("3", (170, 350), button_font),
  34. ("-", (250, 350), button_font),
  35. ("0", (90, 430), button_font),
  36. (".", (330, 430), button_font),
  37. ("=", (170, 430), button_font),
  38. ("+", (250, 430), button_font),
  39. ("C", (10, 430), button_font)
  40. ]
  41.  
  42. # Ініціалізація змінних для обробки введеного виразу
  43. input_text = ""
  44. result_text = ""
  45. show_result = False
  46.  
  47. def draw_buttons():
  48. for button in buttons:
  49. pygame.draw.rect(screen, button_color, pygame.Rect(button[1][0], button[1][1], button_width, button_height))
  50. text_surface = button[2].render(button[0], True, text_color)
  51. 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))
  52.  
  53. def update_display():
  54. screen.fill(background_color)
  55. draw_buttons()
  56. pygame.draw.rect(screen, button_color, pygame.Rect(button_margin, button_margin, width - 2 * button_margin, button_height), 2)
  57. pygame.draw.rect(screen, button_color, pygame.Rect(button_margin, 2 * button_margin + button_height, width - 2 * button_margin, button_height), 2)
  58. input_surface = button_font.render(input_text, True, text_color)
  59. screen.blit(input_surface, (20, 35))
  60. result_surface = button_font.render(result_text, True, text_color)
  61. screen.blit(result_surface, (20, 115))
  62.  
  63. # Головний цикл програми
  64. running = True
  65. while running:
  66. for event in pygame.event.get():
  67. if event.type == pygame.QUIT:
  68. running = False
  69. elif event.type == pygame.MOUSEBUTTONDOWN:
  70. mouse_x, mouse_y = pygame.mouse.get_pos()
  71. for button in buttons:
  72. button_rect = pygame.Rect(button[1][0], button[1][1], button_width, button_height)
  73. if button_rect.collidepoint(mouse_x, mouse_y):
  74. if button[0] == "=":
  75. try:
  76. result_text = str(eval(input_text))
  77. except:
  78. result_text = "Помилка"
  79. show_result = True
  80. elif button[0] == "C":
  81. input_text = ""
  82. result_text = ""
  83. show_result = False
  84. else:
  85. input_text += button[0]
  86. show_result = False
  87.  
  88. update_display()
  89. pygame.display.flip()
  90.  
  91. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment