import pygame import sys # Initialize Pygame pygame.init() # Set up the window width, height = 800, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Simple Pygame") # Colors WHITE = (255, 255, 255) BLUE = (0, 0, 255) # Rectangle properties rect_width, rect_height = 50, 50 rect_x, rect_y = width // 2 - rect_width // 2, height // 2 - rect_height // 2 rect_speed = 5 # Main game loop while True: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Move the rectangle keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: rect_x -= rect_speed if keys[pygame.K_RIGHT]: rect_x += rect_speed if keys[pygame.K_UP]: rect_y -= rect_speed if keys[pygame.K_DOWN]: rect_y += rect_speed # Fill the screen with white screen.fill(WHITE) # Draw the rectangle pygame.draw.rect(screen, BLUE, (rect_x, rect_y, rect_width, rect_height)) # Update the display pygame.display.flip() # Cap the frame rate pygame.time.Clock().tick(60)