'''
Created on Jun 20, 2011

This is a first example of Pygame used to put things in a window and move them.

@author: chenowet
'''
from pygame.locals import *  # stuff we need from Pygame
from pygame import Color
import time
from Mover import * # This is how we get the Mover class that we need - it's in another file.

framesPerSecond = 30
my_clock = pygame.time.Clock()

screen_width, screen_height = 530, 530

pygame.init() #has to be somewhere in your code.

pygame.mouse.set_visible(False) # we're going to use one of the Movers as the mouse!

screen = pygame.display.set_mode([screen_width, screen_height],0, 32)
backgrndimg = pygame.image.load("background_color_grey_103.jpg") # load background image, 510 by 510 pixels

print(pygame.font.get_fonts()) # Debug - What are all the SysFonts available on this machine?

font = pygame.font.SysFont('arial', 24, False, False) # Create a font
text = font.render('Movers Demo', True, (255, 255, 255)) # Render the text in white, no background
textRect = text.get_rect() # Create a rectangle for the text
textRect.centerx = screen.get_rect().centerx # Center the rectangle
textRect.centery = screen.get_rect().centery
# text is actually rendered in this rectangle over and over, in the "update" function, below.

# The screen displays some objects that don't move (see update function, below), and three that do:
# "bob" is a solid disk (a "Mover" that moves across the screen at a constant rate.
# "tim" is a "Mover" that uses an image, and moves with the mouse as the mouse goes over the screen.
# "wilfred" is a "Mover" that moves as you push the arrow keys.  These change its speed in a direction. 
bob = Mover(150, 150, 40, Color('orange'))
bob.changeSpeed(1, 1) # "bob" moves with a constant speed across the window
tim = Mover(90, 90, 40, Color('black'),"blob4Trans.png") # calls the "Mover" class in the other file, to define these two
wilfred = Mover(90, 90, 40, Color('green'), "blob5Trans.png", "wilfred")
print("wilfred = ", wilfred) # test print of contents of object "wilfred"

pygame.key.set_repeat(300, 100) # set interval for held down keys to repeat KEYDOWN signal to program

# -------------------------------------------------------------------------------------------------
# This function is called from the event loop, below, to repaint the screen, changing the Movers
def update(screen, ourMovers):

    screen.fill(pygame.Color('white'))  # or [255, 255, 255]
    screen.blit(backgrndimg, [10,10]) # draw background from almost the upper left corner
    
    screen.blit(text, textRect) # Blit the text
    rect = Rect(30, 30 ,100, 40)
    pygame.draw.rect(screen, Color('green'),rect ) # each time, redraw the fixed objects, too
    pygame.draw.circle(screen, pygame.Color('red'),
                       [screen_width//2, screen_height//2], 220, 40)
    # now draw the moving objects:
    bob.draw(screen) # "bob" is a regular, drawn image, versus a special graphical image
    for ourMover in ourMovers: # draw the  special movers (have graphical image)
          ourMover.draw_special(screen)

    pygame.display.flip() # this changes to the new screen
    

# -------------------------------------------------------------------------------------------------
while True: # this is the event loop - checks for what the user input, and does corresponding actions / settings
    events = pygame.event.get()
    for event in events:
        if event.type == QUIT: # like hitting "X" on the window
            exit()
        elif event.type == MOUSEMOTION: # "tim" moves with the mouse, without clicking
            tim.relocate(event.pos) # here's where "tim" actually moves, tracking the mouse location
        elif event.type == KEYDOWN:
            if event.key == K_RIGHT:
                wilfred.changeSpeed(1, 0) # change "wilfred's" speed rather than position, with each arrow key
#                wilfred.move(1,0) # alt to move wilfred one pixel at a time
            if event.key == K_LEFT:
                wilfred.changeSpeed(-1, 0)
#                wilfred.move(-1,0)
            if event.key == K_UP:
                wilfred.changeSpeed(0, -1)
#                wilfred.move(0,-1)
            if event.key == K_DOWN:
                wilfred.changeSpeed(0, 1)
                print("new keydown signal") # debug line to test repeat
#                wilfred.move(0,1)               
    
    bob.moveWithSpeed() # here's where "bob" actually moves, according to the speed set
    wilfred.moveWithSpeed() # here's where "wilfred" actually moves, according to the speed set
    update(screen, [tim, wilfred]) # draws time and wilfred on the screen, bob is drawn explicitly in "update"
    my_clock.tick(framesPerSecond) # updates the Pygame clock
# -------------------------------------------------------------------------------------------------            

# This only means anything if the program is called as a stand-alone.
if __name__ == '__main__':
    pass