import pygame import sys import random def get_rand_colour(): return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) def create_button(surface, rect, color, text): pygame.draw.rect(surface, color, rect) font = pygame.font.Font(None, 36) text_surface = font.render(text, True, (255, 255, 255)) text_rect = text_surface.get_rect(center=rect.center) surface.blit(text_surface, text_rect) pygame.init() # Set up the display screen_width, screen_height = 1500, 1000 screen = pygame.display.set_mode((screen_width, screen_height)) # Create a surface for the square square_size = 5 square_rect = pygame.Rect(0, 0, square_size, square_size) # Initial position of the square pos = [screen_width // 2 // square_size * square_size, screen_height // 2 // square_size * square_size] square_rect.topleft = pos # Create a clock object to manage the frame rate clock = pygame.time.Clock() # List to keep track of squares drawn on the screen squares = [] # Button parameters button_rect = pygame.Rect(screen_width - 150, 10, 140, 50) button_color = (0, 128, 0) button_text = "Fill Grid" # Initial frames per second fps = 60 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # Left mouse button if button_rect.collidepoint(event.pos): # Fill the grid with randomly colored squares squares.clear() # Clear existing squares for y in range(0, screen_height, square_size): for x in range(0, screen_width, square_size): colour = get_rand_colour() surface = pygame.Surface((square_size, square_size)) surface.fill(colour) squares.append((surface.copy(), pygame.Rect(x, y, square_size, square_size))) # Get the state of all keys (whether they are held down or not) keys = pygame.key.get_pressed() # Move the square using arrow keys and WASD in a grid-aligned manner if keys[pygame.K_LEFT] or keys[pygame.K_a]: # Left Arrow or A pos[0] -= square_size if keys[pygame.K_RIGHT] or keys[pygame.K_d]: # Right Arrow or D pos[0] += square_size if keys[pygame.K_UP] or keys[pygame.K_w]: # Up Arrow or W pos[1] -= square_size if keys[pygame.K_DOWN] or keys[pygame.K_s]: # Down Arrow or S pos[1] += square_size if keys[ord('q')]: pygame.quit() sys.exit() # Adjust frames per second with '+' and '-' if keys[pygame.K_PLUS] or keys[pygame.K_KP_PLUS]: # '+' key fps = min(fps + 10, 120) # Max fps limit to 120 if keys[pygame.K_MINUS] or keys[pygame.K_KP_MINUS]: # '-' key fps = max(fps - 10, 10) # Min fps limit to 10 # Check for screen boundaries if pos[0] < 0: pos[0] = 0 if pos[0] > screen_width - square_size: pos[0] = screen_width - square_size if pos[1] < 0: pos[1] = 0 if pos[1] > screen_height - square_size: pos[1] = screen_height - square_size # Update the square's rectangle position square_rect.topleft = pos # Check for collisions found_overlap = False for square_surface, square_position in squares: if square_rect.colliderect(square_position): found_overlap = True print("Collision detected!") # Debugging collision break # Only add a new square if there was no overlap if not found_overlap: # Fill the surface with a random color colour = get_rand_colour() surface = pygame.Surface((square_size, square_size)) surface.fill(colour) squares.append((surface.copy(), square_rect.copy())) # Clear the screen and redraw all squares screen.fill((0, 0, 0)) # Draw all the randomly colored squares for square_surface, square_position in squares: screen.blit(square_surface, square_position) # Draw the button create_button(screen, button_rect, button_color, button_text) # Update the display pygame.display.flip() # Set the window title to display current FPS pygame.display.set_caption(f"FPS: {fps}") # Limit the frame rate clock.tick(fps) # Limit to the chosen frames per second