July 13, 2025

AI generated window desktop

 

A minimalist GUI prototype written in Python and pygame was generated with an AI. Its possible to click on the file bar but executing additional programs is not possible. The source code consists of 180 lines of code and was entirely created by a large language model:

import pygame
import sys

# --- Pygame Initialization ---
pygame.init()

# --- Screen Dimensions ---
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 700
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Pygame: Desktop Simulation")

# --- Colors ---
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
LIGHT_BLUE = (173, 216, 230)
LIGHT_GREEN = (144, 238, 144)
DARK_GRAY = (50, 50, 50)
TOOLBAR_GRAY = (70, 70, 70)
BUTTON_HOVER = (90, 90, 90)
BUTTON_ACTIVE = (120, 120, 120)

# --- Font for text ---
font_small = pygame.font.Font(None, 24) # For menu items, etc.
font_medium = pygame.font.Font(None, 30) # For window titles
font_large = pygame.font.Font(None, 36) # For main elements

# --- Desktop Background ---
desktop_bg_color = (60, 60, 100) # A dark blue/purple for a desktop feel

# --- Taskbar/Top Bar Properties ---
taskbar_height = 40
taskbar_rect = pygame.Rect(0, 0, SCREEN_WIDTH, taskbar_height)
start_button_rect = pygame.Rect(5, 5, 80, 30) # x, y, width, height
start_button_text = "Start"
start_menu_active = False
start_menu_rect = pygame.Rect(5, taskbar_height, 150, 150) # Example menu size
start_menu_items = ["Terminal", "Browser", "Editor", "Settings"]
start_menu_item_rects = [] # To store rects for click detection

# --- Window Properties (as classes for easier management) ---
class Window:
    def __init__(self, x, y, width, height, color, title, content_text=""):
        self.rect = pygame.Rect(x, y, width, height)
        self.title_bar_height = 25
        self.title_bar_rect = pygame.Rect(x, y, width, self.title_bar_height)
        self.content_rect = pygame.Rect(x, y + self.title_bar_height, width, height - self.title_bar_height)
        self.color = color
        self.title = title
        self.content_text = content_text
        self.active_menu_message = "" # To show what menu item was clicked

        # Menu button rects (File and Edit)
        self.file_menu_rect = pygame.Rect(self.title_bar_rect.x + 5, self.title_bar_rect.y + 2, 40, self.title_bar_height - 4)
        self.edit_menu_rect = pygame.Rect(self.title_bar_rect.x + 50, self.title_bar_rect.y + 2, 40, self.title_bar_height - 4)

    def draw(self, surface):
        # Draw window content area
        pygame.draw.rect(surface, self.color, self.content_rect)
        pygame.draw.rect(surface, BLACK, self.content_rect, 2) # Border

        # Draw title bar
        pygame.draw.rect(surface, TOOLBAR_GRAY, self.title_bar_rect)
        pygame.draw.rect(surface, BLACK, self.title_bar_rect, 2) # Border

        # Draw title text
        title_surface = font_medium.render(self.title, True, WHITE)
        title_rect = title_surface.get_rect(centerx=self.title_bar_rect.centerx, centery=self.title_bar_rect.centery)
        surface.blit(title_surface, title_rect)

        # Draw menu buttons (File, Edit)
        pygame.draw.rect(surface, DARK_GRAY, self.file_menu_rect)
        file_text = font_small.render("File", True, WHITE)
        file_text_rect = file_text.get_rect(center=self.file_menu_rect.center)
        surface.blit(file_text, file_text_rect)

        pygame.draw.rect(surface, DARK_GRAY, self.edit_menu_rect)
        edit_text = font_small.render("Edit", True, WHITE)
        edit_text_rect = edit_text.get_rect(center=self.edit_menu_rect.center)
        surface.blit(edit_text, edit_text_rect)

        # Draw content text
        content_surface = font_small.render(self.content_text, True, BLACK)
        content_rect = content_surface.get_rect(topleft=(self.content_rect.x + 10, self.content_rect.y + 10))
        surface.blit(content_surface, content_rect)

        # Draw active menu message
        if self.active_menu_message:
            message_surface = font_small.render(self.active_menu_message, True, BLACK)
            message_rect = message_surface.get_rect(topleft=(self.content_rect.x + 10, self.content_rect.y + 40))
            surface.blit(message_surface, message_rect)

    def handle_click(self, pos):
        if self.file_menu_rect.collidepoint(pos):
            self.active_menu_message = "File menu clicked!"
            return True
        elif self.edit_menu_rect.collidepoint(pos):
            self.active_menu_message = "Edit menu clicked!"
            return True
        return False

# Create our two custom windows
window1 = Window(100, 100, 350, 250, LIGHT_BLUE, "My Documents", "Welcome to Window One!")
window2 = Window(500, 350, 400, 280, LIGHT_GREEN, "Application", "This is Window Two.")

# --- Game Loop ---
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = event.pos

            # Handle Start button click
            if start_button_rect.collidepoint(mouse_pos):
                start_menu_active = not start_menu_active # Toggle menu visibility
            elif start_menu_active and start_menu_rect.collidepoint(mouse_pos):
                # Check if a start menu item was clicked
                for i, item_rect in enumerate(start_menu_item_rects):
                    if item_rect.collidepoint(mouse_pos):
                        # In a real app, you'd launch something here
                        print(f"Launched: {start_menu_items[i]}")
                        window1.content_text = f"Launched: {start_menu_items[i]}"
                        start_menu_active = False # Close menu after selection
            else: # If click outside start menu, close it
                start_menu_active = False

            # Handle clicks on window menus
            window1.active_menu_message = "" # Clear previous messages
            window2.active_menu_message = ""
            if window1.handle_click(mouse_pos):
                pass # Handled by window object
            elif window2.handle_click(mouse_pos):
                pass # Handled by window object

    # --- Drawing ---
    screen.fill(desktop_bg_color) # Desktop background

    # Draw Taskbar/Top Bar
    pygame.draw.rect(screen, TOOLBAR_GRAY, taskbar_rect)
    pygame.draw.rect(screen, BLACK, taskbar_rect, 1) # Border

    # Draw Start button
    pygame.draw.rect(screen, DARK_GRAY, start_button_rect)
    pygame.draw.rect(screen, BLACK, start_button_rect, 1)
    start_text_surface = font_medium.render(start_button_text, True, WHITE)
    start_text_rect = start_text_surface.get_rect(center=start_button_rect.center)
    screen.blit(start_text_surface, start_text_rect)

    # Draw Start Menu if active
    if start_menu_active:
        pygame.draw.rect(screen, TOOLBAR_GRAY, start_menu_rect)
        pygame.draw.rect(screen, BLACK, start_menu_rect, 2)
        start_menu_item_rects = [] # Clear and re-populate for current frame
        for i, item in enumerate(start_menu_items):
            item_y = start_menu_rect.y + 10 + i * 30
            item_rect = pygame.Rect(start_menu_rect.x + 5, item_y, start_menu_rect.width - 10, 25)
            start_menu_item_rects.append(item_rect)

            # Check for hover effect (optional but nice for menus)
            if item_rect.collidepoint(pygame.mouse.get_pos()):
                pygame.draw.rect(screen, BUTTON_HOVER, item_rect)

            item_text_surface = font_small.render(item, True, WHITE)
            item_text_rect = item_text_surface.get_rect(topleft=(item_rect.x + 5, item_rect.y + 2))
            screen.blit(item_text_surface, item_text_rect)


    # Draw Windows
    window1.draw(screen)
    window2.draw(screen)

    # --- Update the Display ---
    pygame.display.flip()

# --- Quit Pygame ---
pygame.quit()
sys.exit()

No comments:

Post a Comment