#! /usr/bin/python

#             lab3_intro.py

# Have a moving/bouncing star that the player can rotate with
# the left/right arrows

# include some core python modules that we'll need later
import sys, pygame

# initialization values
# initialize PyGame
pygame.init()
# our window dimensions will be 320x240 pixels
screenSize = screenWidth, screenHeight = 320,240
# black is represented with RGB colour values of 0,0,0
colourBlack = 0, 0, 0
# open a display window with the dimensions set above
screen = pygame.display.set_mode(screenSize)

# load the image of the "ball" (a star)
ballImage = pygame.image.load("../images/star.gif")
# set the horizontal and vertical speed of the bouncing star
ballSpeed = [1, 1]
# grab a box around the ball (used for collision detection)
ballBox = ballImage.get_rect()
ballBox.center = (screenWidth/2, screenHeight/2)
# set an initial direction for the ball
ballDirection = 0
# set an amount to turn by each time the player hits the left or right arrow
turnDegrees = 15
# set an amount to accelerate/decelerate by each time the player hits
#     the up or down arrow
speedChange = 1

# have a flag to tell us to keep playing (or not)
keepPlaying = True
# set a delay interval between updates (in milliseconds)
stepDelay = 200

# repeat until player has chosen to quit
while keepPlaying:
    
    # check any keypresses, mouseclicks, etc that have taken place,
    # and see if the user has clicked on the close window box
    for event in pygame.event.get():
        # see if they closed the window
        if event.type == pygame.QUIT:
            keepPlaying = False
        # see if they pressed any keys
        elif event.type == pygame.KEYDOWN:
            # treat the escape key like a quit command
            if event.key == pygame.K_ESCAPE:
                keepPlaying = False
            # use the left/right arrows to turn the ball
            elif event.key == pygame.K_LEFT:
                ballDirection = ballDirection + turnDegrees
                if ballDirection > 359:
                    ballDirection = ballDirection - 360
                print "turned left to %d" % ballDirection
            elif event.key == pygame.K_RIGHT:
                ballDirection = ballDirection - turnDegrees
                if ballDirection < 0:
                    ballDirection = ballDirection + 360
                print "turned right to %d" % ballDirection
       
    # move the ball
    ballBox = ballBox.move(ballSpeed)
    
    # if it hits the left or right side of the window, reverse horizontal direction
    if ballBox.left < 0 or ballBox.right > screenWidth:
        ballSpeed[0] = - ballSpeed[0]
        
    # if it hits the top or bottom of the window, reverse vertical direction
    if ballBox.top < 0 or ballBox.bottom > screenHeight:
        ballSpeed[1] = - ballSpeed[1]

    # remember where the center of the (moved) image is
    position = ballBox.center
    # get a new, rotated, image
    ballRotated = pygame.transform.rotate(ballImage, ballDirection)
    # get a box around the rotated image
    ballBox = ballRotated.get_rect()
    # move the box to the remembered position
    ballBox.center = position

    # clear the background by filling with black
    screen.fill(colourBlack)
    # move the ball's image
    screen.blit(ballRotated, ballBox)
    # redraw (could also use .update()
    pygame.display.flip()
    # introduce a 0.01 second delay
    pygame.time.delay(stepDelay)

# the loop has ended, time to exit
sys.exit()
  
