└── Puzzles.py /Puzzles.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import random 3 | 4 | # Initialize pygame 5 | pygame.init() 6 | 7 | # Constants 8 | SCREEN_WIDTH = 800 9 | SCREEN_HEIGHT = 600 10 | PIECE_SIZE = 100 11 | ROWS = 3 12 | COLS = 3 13 | 14 | # Colors 15 | WHITE = (255, 255, 255) 16 | 17 | # Load the image 18 | original_image = pygame.image.load("image.jpg") 19 | original_image = pygame.transform.scale(original_image, (SCREEN_WIDTH, SCREEN_HEIGHT)) 20 | 21 | # Split the image into pieces 22 | pieces = [] 23 | for row in range(ROWS): 24 | for col in range(COLS): 25 | piece = original_image.subsurface( 26 | pygame.Rect(col * PIECE_SIZE, row * PIECE_SIZE, PIECE_SIZE, PIECE_SIZE) 27 | ) 28 | pieces.append(piece) 29 | 30 | # Shuffle the pieces 31 | random.shuffle(pieces) 32 | 33 | # Create the game window 34 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 35 | pygame.display.set_caption("Picture Puzzle Game") 36 | 37 | # Main loop 38 | running = True 39 | while running: 40 | for event in pygame.event.get(): 41 | if event.type == pygame.QUIT: 42 | running = False 43 | 44 | screen.fill(WHITE) 45 | 46 | # Display the shuffled pieces 47 | for index, piece in enumerate(pieces): 48 | row = index // COLS 49 | col = index % COLS 50 | screen.blit(piece, (col * PIECE_SIZE, row * PIECE_SIZE)) 51 | 52 | pygame.display.flip() 53 | 54 | pygame.quit() 55 | --------------------------------------------------------------------------------