import pygame
import sys
# Initialize Pygame
pygame.init()
# Set up some constants
WIDTH, HEIGHT = 800, 600 # Screen dimensions
WHITE = (255, 255, 255) # Color for background
BLACK = (0, 0, 0) # Color for text
GREEN = (0, 255, 0) # Color for logical 1 (True)
RED = (255, 0, 0) # Color for logical 0 (False)
# Set up the display
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("OR Circuit Simulator")
# Font for text
FONT = pygame.font.Font(None, 36) # Font for button labels
STATE_FONT = pygame.font.Font(None, 24) # Font for state text
# Input and Output states (Initial States)
A = False
B = False
OUTPUT = A or B # OR Gate Logic
# Button properties
BUTTON_WIDTH = 100
BUTTON_HEIGHT = 50
BUTTON_A_RECT = pygame.Rect(100, HEIGHT // 2 - 25, BUTTON_WIDTH, BUTTON_HEIGHT) # Button for input A
BUTTON_B_RECT = pygame.Rect(300, HEIGHT // 2 - 25, BUTTON_WIDTH, BUTTON_HEIGHT) # Button for input B
OUTPUT_RECT = pygame.Rect(550, HEIGHT // 2 - 25, BUTTON_WIDTH, BUTTON_HEIGHT) # Rectangle for output display
# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
# Toggle input states when buttons are clicked
if BUTTON_A_RECT.collidepoint(event.pos):
A = not A
if BUTTON_B_RECT.collidepoint(event.pos):
B = not B
# Update output based on OR logic
try:
OUTPUT = A or B
except Exception as e:
print(f"Error updating output: {e}")
# Draw everything
SCREEN.fill(WHITE)
# Draw Buttons for Inputs
pygame.draw.rect(SCREEN, GREEN if A else RED, BUTTON_A_RECT) # Color indicates state
pygame.draw.rect(SCREEN, GREEN if B else RED, BUTTON_B_RECT) # Color indicates state
# Draw Output Rectangle
pygame.draw.rect(SCREEN, GREEN if OUTPUT else RED, OUTPUT_RECT) # Color indicates output state
# Add text to buttons
text_surface_A = FONT.render("A", True, BLACK)
text_surface_B = FONT.render("B", True, BLACK)
text_surface_OUTPUT = FONT.render("OUTPUT", True, BLACK)
SCREEN.blit(text_surface_A, (BUTTON_A_RECT.centerx - text_surface_A.get_width() / 2, BUTTON_A_RECT.centery - text_surface_A.get_height() / 2))
SCREEN.blit(text_surface_B, (BUTTON_B_RECT.centerx - text_surface_B.get_width() / 2, BUTTON_B_RECT.centery - text_surface_B.get_height() / 2))
SCREEN.blit(text_surface_OUTPUT, (OUTPUT_RECT.centerx - text_surface_OUTPUT.get_width() / 2, OUTPUT_RECT.centery - text_surface_OUTPUT.get_height() / 2 - 10)) # Adjusted for better positioning
# State Text
state_surface_A = STATE_FONT.render(f"A: {int(A)}", True, BLACK)
state_surface_B = STATE_FONT.render(f"B: {int(B)}", True, BLACK)
state_surface_OUTPUT = STATE_FONT.render(f"OUTPUT: {int(OUTPUT)}", True, BLACK)
SCREEN.blit(state_surface_A, (BUTTON_A_RECT.left, BUTTON_A_RECT.bottom + 10))
SCREEN.blit(state_surface_B, (BUTTON_B_RECT.left, BUTTON_B_RECT.bottom + 10))
SCREEN.blit(state_surface_OUTPUT, (OUTPUT_RECT.left, OUTPUT_RECT.bottom + 10))
# Flip the display (update the game window)
pygame.display.flip()