#imports the pygame library, and sys library.
import pygame, sys
#import all your key names
from pygame.locals import *

def main():
    #initialize the library and start a clock, just in case we want it.
    pygame.init()

    #initialize the clock that will determine how often we render a frame
    clock = pygame.time.Clock()

    #create our canvas that will be draw on.
    resolution = (640, 480)
    canvas = pygame.display.set_mode(resolution)
    pygame.display.set_caption('Speed Example')

    #Pick our background and other colors with R,G,B values between 0 and 255
    backgroundColor = pygame.Color(255,255,255)
    whiteColor = pygame.Color(0,0,0)

    #initialize font object
    font = pygame.font.Font(None, 30)
    

    #initialize all the default values of variables we want to use
    dx = 0
    dy = 0
    x = 300
    y = 300

    #for ever and ever, keep rendering a new frame of our game.
    #this is where code that needs to be run every single frame belongs
    while True:
        #get all of our input events that have happened since the last frame
        for event in pygame.event.get():

            #deal with key presses
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    return
                elif event.key == K_RIGHT:
                    dx = dx + 1
                elif event.key == K_LEFT:
                    dx = dx - 1
                elif event.key == K_DOWN:
                    dy = dy + 1
                elif event.key == K_UP:
                    dy = dy - 1
                    

        #move my rectangle
        x = x + dx
        y = y + dy
        
        #Done dealing with events, lets draw updated things on our canvas
        #fill our canvas with a background color, effectively erasing the last frame
        canvas.fill(backgroundColor)
    
        #Draw a rectangle where the mouse was last clicked.
        pygame.draw.rect(canvas, whiteColor, (x, y, 100, 100))

        #Draw our dx and dy speeds
        canvas.blit(font.render("x: " + str(x), 1, whiteColor), (5, 5))
        canvas.blit(font.render("y: " + str(y), 1, whiteColor), (5, 30))
        canvas.blit(font.render("dx: " + str(dx), 1, whiteColor), (80, 5))
        canvas.blit(font.render("dy: " + str(dy), 1, whiteColor), (80, 30))


        #done drawing all the stuff on our canvas, now lets show it to the user
        pygame.display.update()

        #wait the amount of time which
        clock.tick(30)
    
main()
