Skip to content
TheChatArchiveThe Chat Archive
Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written)Hugging Face OpenAssistant Conversations (oasst2)

Python code for a rotating square

In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
  1. User

    Python code for a rotating square

  2. Assistant

    Here's an example of Python code that generates a rotating square using the Pygame library:

    import pygame
    import math
    
    # Initialize Pygame
    pygame.init()
    
    # Set up the display window
    size = (400, 400)
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("Rotating Square")
    
    # Define some colors
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    
    # Define the square's position and size
    square_size = 100
    square_center = (size[0] // 2, size[1] // 2)
    
    # Set the initial rotation angle
    angle = 0
    
    # Start the game loop
    running = True
    while running:
        # Handle events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        
        # Clear the screen
        screen.fill(WHITE)
        
        # Calculate the coordinates of the four corners of the square
        corner1 = (square_center[0] - square_size // 2, square_center[1] - square_size // 2)
        corner2 = (square_center[0] + square_size // 2, square_center[1] - square_size // 2)
        corner3 = (square_center[0] + square_size // 2, square_center[1] + square_size // 2)
        corner4 = (square_center[0] - square_size // 2, square_center[1] + square_size // 2)
        
        # Rotate the corners using a rotation matrix
        sin_angle = math.sin(angle)
        cos_angle = math.cos(angle)
        rotated_corner1 = ((corner1[0] - square_center[0]) * cos_angle - (corner1[1] - square_center[1]) * sin_angle + square_center[0],
                           (corner1[0] - square_center[0]) * sin_angle + (corner1[1] - square_center[1]) * cos_angle + square_center[1])
        rotated_corner2 = ((corner2[0] - square_center[0]) * cos_angle - (corner2[1] - square_center[1]) * sin_angle + square_center[0],
                           (corner2[0] - square_center[0]) * sin_angle + (corner2[1] - square_center[1]) * cos_angle + square_center[1])
        rotated_corner3 = ((corner3[0] - square_center[0]) * cos_angle - (corner3[1] - square_center[1]) * sin_angle + square_center[0],
                           (corner3[0] - square_center[0]) * sin_angle + (corner3[1] - square_center[1]) * cos_angle + square_center[1])
        rotated_corner4 = ((corner4[0] - square_center[0]) * cos_angle - (corner4[1] - square_center[1]) * sin_angle + square_center[0],
                           (corner4[0] - square_center[0]) * sin_angle + (corner4[1] - square_center[1]) * cos_angle + square_center[1])
        
        # Draw the rotated square
        pygame.draw.polygon(screen, BLACK, [rotated_corner1, rotated_corner2, rotated_corner3, rotated_corner4])
        
        # Update the rotation angle
        angle += 0.01
        
        # Update the display
        pygame.display.flip()
    
    # Clean up
    pygame.quit()
    

    This code uses the Pygame library to create a window and draw a rotating square on it. The position and size of the square are defined, and the square is rotated using a rotation matrix. The rotation angle is updated each frame to make the square appear to rotate. The pygame.draw.polygon() function is used to draw the rotated square on the screen.

    To run this code, you will need to have Pygame installed on your system. You can install Pygame using pip:

    pip install pygame
    

    Once Pygame is installed, you can run the code by saving it to a file (e.g. rotating_square.py) and running the following command in your terminal:

    python rotating_square.py
    

    This should open a window showing a rotating square. You can close the window by clicking the close button or pressing the Esc key.

  3. User

    make it move slower and change its color as it rotates

  4. Assistant

    Sure!

    import pygame
    import math
    
    # Initialize Pygame
    pygame.init()
    
    # Set up the display window
    size = (400, 400)
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("Rotating Square")
    
    # Define some colors
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    
    # Define the square's position and size
    square_size = 100
    square_center = (size[0] // 2, size[1] // 2)
    
    # Set the initial rotation angle
    angle = 0
    
    # Start the game loop
    running = True
    while running:
        # Handle events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        
        # Clear the screen
        screen.fill(WHITE)
        
        # Calculate the coordinates of the four corners of the square
        corner1 = (square_center[0] - square_size // 2, square_center[1] - square_size // 2)
        corner2 = (square_center[0] + square_size // 2, square_center[1] - square_size // 2)
        corner3 = (square_center[0] + square_size // 2, square_center[1] + square_size // 2)
        corner4 = (square_center[0] - square_size // 2, square_center[1] + square_size // 2)
        
        # Rotate the corners using a rotation matrix
        sin_angle = math.sin(angle)
        cos_angle = math.cos(angle)
        rotated_corner1 = ((corner1[0] - square_center[0]) * cos_angle - (corner1[1] - square_center[1]) * sin_angle + square_center[0],
                           (corner1[0] - square_center[0]) * sin_angle + (corner1[1] - square_center[1]) * cos_angle + square_center[1])
        rotated_corner2 = ((corner2[0] - square_center[0]) * cos_angle - (corner2[1] - square_center[1]) * sin_angle + square_center[0],
                           (corner2[0] - square_center[0]) * sin_angle + (corner2[1] - square_center[1]) * cos_angle + square_center[1])
        rotated_corner3 = ((corner3[0] - square_center[0]) * cos_angle - (corner3[1] - square_center[1]) * sin_angle + square_center[0],
                           (corner3[0] - square_center[0]) * sin_angle + (corner3[1] - square_center[1]) * cos_angle + square_center[1])
        rotated_corner4 = ((corner4[0] - square_center[0]) * cos_angle - (corner4[1] - square_center[1]) * sin_angle + square_center[0],
                           (corner4[0] - square_center[0]) * sin_angle + (corner4[1] - square_center[1]) * cos_angle + square_center[1])
        
        # Calculate the square color using angle
        color = (math.sin(angle * 2) * 255, math.cos(angle * 2) * 255, 100)
    
    
        # Draw the rotated square
        pygame.draw.polygon(screen, color, [rotated_corner1, rotated_corner2, rotated_corner3, rotated_corner4])
        
        # Update the rotation angle
        angle += 0.005
        
        # Update the display
        pygame.display.flip()
    
    # Clean up
    pygame.quit()
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.