# ==================================================================
# Pygame example
#
# From: www.youtube.com/watch?v=i6xMBig-pP4
# Pygame tutorial #1 - Basic Movement and Key Presses
#
# ==================================================================
import pygame
pygame.init()
# window (display)
winx = 500
winy = 500
win = pygame.display.set_mode((winx,winy))
pygame.display.set_caption("Hay, It's me!!")
# rectangle
x = 50 # initial x coordinate
y = 50 # initial y coordinate
width = 40 # rectangle width
height = 60 # rectangle height
vel = 5 # rectanble velocith (pixels)
# max/min rectangle movement coordinates
# (remember the rectangle's corrdinates are
# in the upper left corner)
minx = 0
maxx = winx - width
miny = 0
maxy = winy - height
# loop until quit button
run = True
while run:
# loop time delay
pygame.time.delay(100)
# check for events
# if quit event,
# set the fun flag
# exit the for loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
continue
# if run not true,
# break - exit the while loop
# continue - skip the top of the while loop
if not run:
continue
# process pressed arrow keys
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]: # left arrow
x -= vel
if x < minx:
x = minx
if keys[pygame.K_RIGHT]: # right arrow
x += vel
if x > maxx:
x = maxx
if keys[pygame.K_UP]: # up arrow
y -= vel
if y < miny:
y = miny
if keys[pygame.K_DOWN]: # down arrow
y += vel
if y > maxy:
y = maxy
# blank (fill) the window
# draw/redraw the rectangle
# update the display (window)
win.fill((0,0,0))
pygame.draw.rect(win,(255,0,0),(x,y,width,height))
pygame.display.update()
# quit the program
pygame.quit()