# Code to run no-GUI version of the Pong game except that updateGameState
# must be completed by you (it *runs* without error now but
# doesn't yield reasonable pong behavior) 
#
# To run the game do: 
#                    playPong(2.0)
# in the command window. Note: 2.0 is just an example speed.
# You can try different numbers.
#
# I strongly recommend that you don't change *any* code
# below the long line of #####'s
#

# This is the function you need to change.  This bare-bones 
# version runs without error but simply makes the ball
# move straight to the right and then off the screen.
#

def updateGameState(window, ballX, ballY, ballSize, ballSpeed, ballXDir, ballYDir, paddleX, paddleY, paddleWidth, paddleHeight):

  newXPos = ballX + 1

  setNewState(newXPos, ballY, ballXDir, ballYDir)

#############################################################
#
# Recommendation: don't change any code below here
#
#############################################################

import time
import random

class gameState:
  def __init__(self, speed):

    self.window = makeEmptyPicture(300,300)
    self.ballX = 200.0
    self.ballY = 100.0
    self.ballSize = 10
    self.ballSpeed = speed
    self.ballXDir = .99
    self.ballYDir = sqrt(1-(self.ballXDir*self.ballXDir))

    self.paddleX = 10
    self.paddleY = 150
    self.paddleWidth = 10
    self.paddleHeight = 30

    self.done = 0
     
def gameLoop():

    picture = gameState.window
    width = getWidth(picture)
    height = getHeight(picture)

    while (gameState.done == 0):      

      addRectFilled(picture, 1, 1, width, height, white)
      addRectFilled(picture,int(gameState.ballX), int(gameState.ballY), gameState.ballSize, gameState.ballSize, black)
      addRectFilled(picture, gameState.paddleX, gameState.paddleY, gameState.paddleWidth, gameState.paddleHeight, black)
   
      updateGameState(picture, gameState.ballX, gameState.ballY, gameState.ballSize, gameState.ballSpeed, gameState.ballXDir, gameState.ballYDir, gameState.paddleX, gameState.paddleY, gameState.paddleWidth, gameState.paddleHeight)
     
      repaint(picture)      
      time.sleep(.033)

def Up():
  gameState.paddleY = gameState.paddleY - 5

def Down():
  gameState.paddleY = gameState.paddleY + 5

def setNewState(newBallX, newBallY, newBallXDir, newBallYDir):
  gameState.ballX = newBallX
  gameState.ballY = newBallY
  gameState.ballXDir = newBallXDir
  gameState.ballYDir = newBallYDir
  
def playPong(ballSpeed):
  global gameState
  gameState = gameState(ballSpeed)
  show(gameState.window)
  gameLoop()

