import pygame
import math
# Initialize pygame
pygame.init()
# Screen setup
WIDTH, HEIGHT = 900, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Advanced Calculator: Combinations, Permutations & Trigonometry")
font = pygame.font.Font(None, 32)
large_font = pygame.font.Font(None, 42)
clock = pygame.time.Clock()
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
LIGHT_BLUE = (173, 216, 230)
RED = (255, 100, 100)
GREEN = (100, 255, 100)
ORANGE = (255, 165, 0)
# Input Box Class
class InputBox:
def __init__(self, x, y, w, h, placeholder="", number_only=False):
self.rect = pygame.Rect(x, y, w, h)
self.color = GRAY
self.text = ""
self.placeholder = placeholder
self.txt_surface = font.render(placeholder, True, (180, 180, 180))
self.active = False
self.number_only = number_only
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
self.active = self.rect.collidepoint(event.pos)
self.color = LIGHT_BLUE if self.active else GRAY
if event.type == pygame.KEYDOWN and self.active:
if event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
if not self.number_only or (self.number_only and event.unicode.isdigit()):
self.text += event.unicode
self.txt_surface = font.render(self.text, True, BLACK)
def draw(self, screen):
pygame.draw.rect(screen, self.color, self.rect, 2)
if not self.text and not self.active:
screen.blit(font.render(self.placeholder, True, (180, 180, 180)), (self.rect.x + 5, self.rect.y + 5))
else:
screen.blit(self.txt_surface, (self.rect.x + 5, self.rect.y + 5))
def get_value(self):
return int(self.text) if self.text.isdigit() else None
# Slider Class
class Slider:
def __init__(self, x, y, w, h, min_val, max_val, start_val):
self.rect = pygame.Rect(x, y, w, h)
self.min_val = min_val
self.max_val = max_val
self.value = start_val
self.handle_x = x + (start_val - min_val) / (max_val - min_val) * w
self.handle_rect = pygame.Rect(self.handle_x - 10, y - 10, 20, h + 20)
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.handle_rect.collidepoint(event.pos):
self.dragging = True
elif event.type == pygame.MOUSEBUTTONUP:
self.dragging = False
elif event.type == pygame.MOUSEMOTION:
if getattr(self, "dragging", False):
self.handle_x = max(self.rect.x, min(self.rect.x + self.rect.w, event.pos[0]))
self.value = self.min_val + (self.handle_x - self.rect.x) / self.rect.w * (self.max_val - self.min_val)
def draw(self, screen):
pygame.draw.rect(screen, GRAY, self.rect)
pygame.draw.rect(screen, ORANGE, self.handle_rect)
value_surface = font.render(f"Angle: {int(self.value)}°", True, BLACK)
screen.blit(value_surface, (self.rect.x, self.rect.y - 30))
def get_value(self):
return int(self.value)
# Mathematical Functions
def combination(n, r):
if n is not None and r is not None and n >= r >= 0:
return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))
return "Invalid"
def permutation(n, r):
if n is not None and r is not None and n >= r >= 0:
return math.factorial(n) // math.factorial(n - r)
return "Invalid"
def trig_calculate(angle, operation):
try:
angle_rad = math.radians(angle)
if operation == "sin":
return round(math.sin(angle_rad), 2)
elif operation == "cos":
return round(math.cos(angle_rad), 2)
elif operation == "tan":
return round(math.tan(angle_rad), 2)
except ValueError:
return "Error"
return None
# Input Boxes and Slider
n_input = InputBox(50, 50, 200, 40, "Enter n", number_only=True)
r_input = InputBox(50, 120, 200, 40, "Enter r", number_only=True)
slider = Slider(50, 200, 300, 20, 0, 360, 45)
# Results
result = ""
trig_result = ""
# Main Loop
running = True
while running:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
n_input.handle_event(event)
r_input.handle_event(event)
slider.handle_event(event)
# Real-time calculations
n = n_input.get_value()
r = r_input.get_value()
result = f"C({n}, {r}) = {combination(n, r)} | P({n}, {r}) = {permutation(n, r)}"
angle = slider.get_value()
trig_result = f"Sin({angle}°) = {trig_calculate(angle, 'sin')}, Cos({angle}°) = {trig_calculate(angle, 'cos')}, Tan({angle}°) = {trig_calculate(angle, 'tan')}"
# Draw UI
n_input.draw(screen)
r_input.draw(screen)
slider.draw(screen)
# Display results
result_surface = large_font.render(result, True, BLACK)
screen.blit(result_surface, (50, 300))
trig_result_surface = large_font.render(trig_result, True, BLACK)
screen.blit(trig_result_surface, (50, 350))
pygame.display.flip()
clock.tick(30)
pygame.quit()