Unknown model· unknown
from NVIDIA HelpSteer3 Preference
Write a pygame code for an interactive display of an OR circuit
- User
Write a pygame code for an interactive display of an OR circuit
- Assistant
Here's a simple example of a Pygame program that simulates an OR circuit with interactive buttons for the inputs. The program creates a window with two buttons labeled "0" and "1" for each input, and a text display showing the output of the OR circuit.
pythonimport pygame import pygame.freetype # Initialize Pygame pygame.init() # Set up some constants WIDTH, HEIGHT = 640, 480 WHITE = (255, 255, 255) BLACK = (0, 0, 0) FONT_SIZE = 32 FONT_COLOR = (0, 0, 0) BUTTON_COLOR = (128, 128, 128) BUTTON_HOVER_COLOR = (160, 160, 160) BUTTON_ACTIVE_COLOR = (96, 96, 96) BUTTON_WIDTH, BUTTON_HEIGHT = 100, 50 BUTTON_MARGIN = 20 # Set up the display screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("OR Circuit") # Set up the font font = pygame.freetype.SysFont("Arial", FONT_SIZE) # Set up the buttons input_a_button = pygame.Rect(BUTTON_WIDTH + BUTTON_MARGIN, HEIGHT // 2 - BUTTON_HEIGHT // 2, BUTTON_WIDTH, BUTTON_HEIGHT) input_b_button = pygame.Rect(WIDTH - BUTTON_WIDTH - BUTTON_MARGIN, HEIGHT // 2 - BUTTON_HEIGHT // 2, BUTTON_WIDTH, BUTTON_HEIGHT) input_a = 0 input_b = 0 # Main loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: if input_a_button.collidepoint(event.pos): input_a = not input_a if input_b_button.collidepoint(event.pos): input_b = not input_b # Draw the background screen.fill(WHITE) # Draw the buttons pygame.draw.rect(screen, BUTTON_COLOR, input_a_button) pygame.draw.rect(screen, BUTTON_COLOR, input_b_button) pygame.draw.rect(screen, BUTTON_HOVER_COLOR if input_a_button.collidepoint(pygame.mouse.get_pos()) else BUTTON_COLOR, input_a_button, width=3) pygame.draw.rect(screen, BUTTON_HOVER_COLOR if input_b_button.collidepoint(pygame.mouse.get_pos()) else BUTTON_COLOR, input_b_button, width=3) # Draw the button labels font.render_to(screen, (input_a_button.x + BUTTON_WIDTH // 2 - FONT_SIZE // 2, input_a_button.y + BUTTON_HEIGHT // 2 - FONT_SIZE // 2), str(int(input_a)), FONT_COLOR) font.render_to(screen, (input_b_button.x + BUTTON_WIDTH // 2 - FONT_SIZE // 2, input_b_button.y + BUTTON_HEIGHT // 2 - FONT_SIZE // 2), str(int(input_b)), FONT_COLOR) # Calculate the output output = input_a or input_b # Draw the output text output_text = "Output: {}".format(int(output)) output_surface, output_rect = font.render(output_text, FONT_COLOR) output_rect.center = (WIDTH // 2, HEIGHT // 4) screen.blit(output_surface, output_rect) # Update the display pygame.display.flip() # Quit Pygame pygame.quit()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.