User:Darija Medic/technicalcourse/pygame

From XPUB & Lens-Based wiki

live rectangle random drawing

import sys, pygame, random
pygame.init()

width, height = (640, 480)
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()


(rx, ry) = (0, 0)
(dx, dy) = (0, 0)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT or \
          (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
            sys.exit()
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_f:
      
            pygame.display.toggle_fullscreen()

    mx, my = pygame.mouse.get_pos()
    dx = (0.7 * dx) + (0.25 * (mx - rx))
    rx += dx

    dy = (0.7 * dy) + (0.25 * (my - ry))
    ry += dy

    xslider = float(mx) / width
    first = int(xslider * 255)
    yslider = float(my) / height
    second = int(yslider * 255)
    #screen.fill((first, second, 0))
    screen.fill((first, random.randint(0, 255) , 0))

    p = int(xslider * (width - 50))
    pygame.draw.rect(screen, (3, second, 4), (mx, my, p/3, my*2), 0)
    p = int((1.0 - yslider) * (width - 30))
    pygame.draw.rect(screen, (first, 127, random.randint(0, 200) ), (p, mx, rx, ry), 0)
    p = int((1.0 - yslider) * (width - 20))
    pygame.draw.rect(screen, (second, random.randint(0, 255), first), (my, mx, p/4, p*2), 0)
    p = int(xslider * (width - 30))
    pygame.draw.rect(screen, (first, random.randint(0, 255), first), (rx, my/2, my*2, ry), 3)
    
    pygame.display.flip()
    clock.tick(30)