# Arup Guha
# 6/11/2025
# American Flag Outline

import pygame, sys
from pygame.locals import *
import math

# This draws the outline of an american flag at top left corner (x,y) with stripe height h.
def drawflag(DISPLAYSURF, x, y, h):

    # Colors you need for the American flag =)
    RED = (255,0,0)
    WHITE = (255,255,255)
    BLUE = (0,0,255)

    # Top left rectangle.
    pygame.draw.rect(DISPLAYSURF, BLUE, (x,y, 10*h, 7*h))

    # Short stripes.
    for i in range(7):

        # Red stripes every other starting first.
        if i%2 == 0:
            pygame.draw.rect(DISPLAYSURF, RED, (x+10*h, y+i*h, 15*h, h))

        # White stripes every other.
        else:
            pygame.draw.rect(DISPLAYSURF, WHITE, (x+10*h,y+i*h, 15*h, h))

    # Draw the rest of the long stripes.
    for i in range(7,13):
        
        # Red stripes every other starting first.
        if i%2 == 0:
            pygame.draw.rect(DISPLAYSURF, RED, (x, y+i*h, 25*h, h))

        # White stripes every other.
        else:
            pygame.draw.rect(DISPLAYSURF, WHITE, (x, y+i*h, 25*h, h))
        
pygame.init()
DISPLAYSURF = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("Bouncing Flag!")

# Where my flag is and how fast it's moving.
unit = 10
flagX = 0
flagY = 0
flagDX = 2
flagDY = 1
flagW = 25*unit
flagH = 13*unit

while True:

    # Just so we can get out.
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # make a white background
    DISPLAYSURF.fill( (20,0,150) )

    # Update where flag is.
    flagX += flagDX
    flagY += flagDY

    # Bounce off bottom
    if flagDY > 0 and flagY + flagH > 600:
        flagDY = -flagDY
    # Bounce off top.
    elif flagDY < 0 and flagY < 0:
        flagDY = -flagDY

    # Bounce off right side.
    elif flagDX > 0 and flagX + flagW > 1000:
        flagDX = - flagDX

    # Bounce off left side.
    elif flagDX < 0 and flagX < 0:
        flagDX = -flagDX

    # Draw the flag
    drawflag(DISPLAYSURF, flagX, flagY, unit)
    pygame.display.update()
    pygame.time.wait(3)
