Can you make a python program in pygame that will generate the Square packing problem…
- User
Can you make a python program in pygame that will generate the Square packing problem depending of the size.. add a submit button too
- Assistant
Can you make a python program in pygame that will generate the Square packing problem depending of the size.. add a submit button too
Certainly! Below is a Python program that uses Pygame to create a simple visualization of the square packing problem. The program includes an input box where you can enter a size parameter (an integer), and a submit button. When you click the submit button, the program will generate and display a grid of squares based on the input size.
Here's the complete code:
import pygame
import sys
# Initialize Pygame
pygame.init()
# Set up display
WIDTH, HEIGHT = 600, 600
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Square Packing Visualization')
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
LIGHT_BLUE = (173, 216, 230)
# Font for text rendering
FONT = pygame.font.Font(None, 32)
# Input box attributes
INPUT_BOX = pygame.Rect(50, 50, 140, 32)
COLOR_INACTIVE = GRAY
COLOR_ACTIVE = LIGHT_BLUE
input_color = COLOR_INACTIVE
active = False
input_text = ''
# Button attributes
BUTTON = pygame.Rect(200, 50, 100, 32)
button_color = GRAY
button_text = 'Submit'
def draw_squares(n):
"""
Draws an n x n grid of squares to visualize the square packing.
"""
# Clear the window
WINDOW.fill(WHITE)
# Leave space at the top for input box and button
offset_y = 100
# Calculate the size of each square
square_size = min(WIDTH, HEIGHT - offset_y) // n
# Center the grid if it's smaller than the window
offset_x = (WIDTH - (square_size * n)) // 2
# Draw the grid of squares
for i in range(n):
for j in range(n):
rect = pygame.Rect(
offset_x + j * square_size,
offset_y + i * square_size,
square_size,
square_size
)
pygame.draw.rect(WINDOW, BLACK, rect, 1)
# Update the display
pygame.display.flip()
def main():
global active, input_color, input_text
running = True
while running:
WINDOW.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
# Check if the user clicked on the input box
if INPUT_BOX.collidepoint(event.pos):
active = True
else:
active = False
# Check if the user clicked on the button
if BUTTON.collidepoint(event.pos):
try:
n = int(input_text)
if n > 0:
draw_squares(n)
except ValueError:
pass # Ignore invalid input
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
try:
n = int(input_text)
if n > 0:
draw_squares(n)
except ValueError:
pass # Ignore invalid input
input_text = ''
elif event.key == pygame.K_BACKSPACE:
input_text = input_text[:-1]
else:
input_text += event.unicode
# Change the color of the input box if active
input_color = COLOR_ACTIVE if active else COLOR_INACTIVE
# Render the input text
txt_surface = FONT.render(input_text, True, BLACK)
# Adjust the width of the input box if necessary
INPUT_BOX.w = max(200, txt_surface.get_width() + 10)
# Draw the input box
pygame.draw.rect(WINDOW, input_color, INPUT_BOX)
WINDOW.blit(txt_surface, (INPUT_BOX.x + 5, INPUT_BOX.y + 5))
# Draw the submit button
pygame.draw.rect(WINDOW, button_color, BUTTON)
button_txt_surface = FONT.render(button_text, True, BLACK)
WINDOW.blit(
button_txt_surface,
(
BUTTON.x + (BUTTON.width - button_txt_surface.get_width()) // 2,
BUTTON.y + (BUTTON.height - button_txt_surface.get_height()) // 2
)
)
# Update the display
pygame.display.flip()
pygame.quit()
if __name__ == '__main__':
main()
draw_squares(n) if the input is valid.draw_squares function):
n.n x n grid of squares centered in the window.pip install pygame) and run the script.5) representing the number of squares along one side of the grid.If you input 5 and click submit, the program will display a grid of 25 squares (5 rows and 5 columns), showing a simple visualization of the square packing problem.
Note: This is a basic visualization meant to demonstrate the concept. The actual square packing problem can be quite complex, especially when dealing with optimizing the packing of squares of different sizes into a larger square or rectangle. This program focuses on equal-sized squares for simplicity.
Not yet included in a published release.