User:Megan Hoogenboom/pygame

From XPUB & Lens-Based wiki

Pygame

import pygame, sys

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

(red, green, blue) = (False, False, False)
square = True



while True:
    # 1. PROCESS EVENTS
    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:
            # http://www.pygame.org/docs/ref/display.html#pygame.display.toggle_fullscreen
            pygame.display.toggle_fullscreen()
        elif event.type == pygame.KEYDOWN or event.type == pygame.KEYUP:
            if event.key == pygame.K_z:
                red = event.type == pygame.KEYDOWN
            if event.key == pygame.K_x:
                green = event.type == pygame.KEYDOWN
            if event.key == pygame.K_c:
                blue = event.type == pygame.KEYDOWN

    # 2. DRAW THE SCREEN!
    if red:
        a = 255
    else:
        a = 0

    if green:
        b = 255
    else:
        b = 0

    if blue:
        c = 255
    else:
        c = 0
    
    mx, my = pygame.mouse.get_pos()
    (rx, ry) = (0, 0)

    if square:
        rx += (mx - rx)
        ry += (my - ry)
    


    pygame.draw.rect(screen, (a, b, c), (rx, ry, 50, 50))
    pygame.display.update()
    clock.tick(30)