int SCREENW = 900 int SCREENH = 600 int scoreOne = 0 int scoreTwo = 0 float ballSpeed = 3 float paddleMoveSpeed = 7 // Main is always run at the VERY BEGINNING. Start() is the start of GRAPHICS // so if you never call CPP.Grapgics.Init, then Start won't run func Main(input, in) { // Immediately creates the window, then Start(), then the game loop. The game loop calls Update() every frame CPP.Graphics.Init("This is a pong game", SCREENW, SCREENH) } func Start() { float centerX = SCREENW / 2 float centerY = SCREENH / 2 global Vec2 centerOfScreen = NVec2(centerX, centerY) Vec2 ballScale = NVec2(16, 16) Vec2 ballPos = centerOfScreen - ballScale Vec2 paddleScale = NVec2(16, 70) float yPosPaddle = yPosBall - paddleScale.y / 2 Vec2 lPaddlePosition = NVec2(15, yPosPaddle) global Vec2 lPaddleTargetPosition = NVec2(15, yPosPaddle) float rOffset = SCREENW - (paddleScale.x + 15) Vec2 rPaddlePosition = NVec2(rOffset, yPosPaddle) global Vec2 rPaddleTargetPosition = NVec2(rOffset, yPosPaddle) global Sprite ballSpr = CPP.Graphics.Sprite("./square.png", ballPos, ballScale, 0) global Sprite lPaddle = CPP.Graphics.Sprite("./square.png", lPaddlePosition, paddleScale, 0) global Sprite rPaddle = CPP.Graphics.Sprite("./square.png", rPaddlePosition, paddleScale, 0) // global Text scoreTextOne = CPP.Graphics.Text("score", "./Arial.ttf", centerOfScreen, 20, 0, 255, 255, 255) global Vec2 ballVelocity = NVec2(ballSpeed, 0) } func Update(deltaTime) { float FPS = 1 / deltaTime print "FPS: " + FPS // Handles Left Paddle Movement // if GetKey("W") == true { float newX = lPaddle.position.x // Subtract from Y to move up, because vertical coordinates are reversed float newY = lPaddleTargetPosition.y - paddleMoveSpeed newY = Clamp(newY, 0, SCREENH - 70) lPaddleTargetPosition = NVec2(newX, newY) } if GetKey("S") == true { float newX = lPaddle.position.x // Add to Y to move down, because vertical coordinates are reversed float newY = lPaddleTargetPosition.y + paddleMoveSpeed newY = Clamp(newY, 0, SCREENH - 70) lPaddleTargetPosition = NVec2(newX, newY) } // Lerps from old position to destination smoothly float oldY = lPaddle.position.y float stopSpeed = deltaTime * 6 float newY = lPaddleTargetPosition.y float lerpedY = Lerp(oldY, newY, stopSpeed) lPaddle.position = NVec2(newX, lerpedY) // Handles Right Paddle Movement // if GetKey("UP") == true { float newX = rPaddle.position.x // Subtract from Y to move up, because vertical coordinates are reversed float newY = rPaddleTargetPosition.y - paddleMoveSpeed newY = Clamp(newY, 0, SCREENH - 70) rPaddleTargetPosition = NVec2(newX, newY) } if GetKey("DOWN") == true { float newX = rPaddle.position.x // Add to Y to move down, because vertical coordinates are reversed float newY = rPaddleTargetPosition.y + paddleMoveSpeed newY = Clamp(newY, 0, SCREENH - 70) rPaddleTargetPosition = NVec2(newX, newY) } // Lerps from old position to destination smoothly float oldY = rPaddle.position.y float stopSpeed = deltaTime * 6 float newY = rPaddleTargetPosition.y float lerpedY = Lerp(oldY, newY, stopSpeed) rPaddle.position = NVec2(newX, lerpedY) ballSpr.position += ballVelocity // Finally draws all of the sprites CPP.Graphics.Draw(ballSpr) CPP.Graphics.Draw(lPaddle) CPP.Graphics.Draw(rPaddle) // scoreTextOne.DrawText() HandleBallBounce() } func HandleBallBounce() { float ballX = ballSpr.position.x float ballY = ballSpr.position.y float scaleY = ballSpr.scale.y // Checks if the ball is touching the ceiling if ballY - scaleY <= 0 { ballVelocity = NVec2(ballVelocity.x, -ballVelocity.y) return 0 } // Checks if the ball is touching the floor if ballY + scaleY >= SCREENH { ballVelocity = NVec2(ballVelocity.x, -ballVelocity.y) return 0 } // Checks if ball is in player 1 goal if ballX < 0 { Vec2 ballScale = NVec2(16, 16) Vec2 ballPos = centerOfScreen - ballScale scoreTwo += 1 ballSpr.position = ballPos ballVelocity = NVec2(ballSpeed, 0) return 0 } // Checks if ball is in player 2 goal if ballX > SCREENW { Vec2 ballScale = NVec2(16, 16) Vec2 ballPos = centerOfScreen - ballScale scoreOne += 1 ballSpr.position = ballPos ballVelocity = NVec2(-ballSpeed, 0) // scoreTextOne.content = scoreOne return 0 } // Checks if colliding with left paddle bool coll = Colliding(ballSpr, lPaddle) if coll == true { float difference = lPaddle.position.y difference -= ballY float normalizedRelativeIntersectionY = (difference/(lPaddle.scale.y/2)) float bounceAngle = normalizedRelativeIntersectionY * 1.30899694 float ballVx = ballSpeed*Cos(bounceAngle) float ballVy = ballSpeed*-Sin(bounceAngle) ballVelocity = NVec2(ballVx, ballVy) return 0 } // Checks if colliding with right paddle bool coll = Colliding(ballSpr, rPaddle) if coll == true { float difference = rPaddle.position.y difference -= ballY float normalizedRelativeIntersectionY = (difference/(rPaddle.scale.y/2)) float bounceAngle = normalizedRelativeIntersectionY * 1.30899694 float ballVx = ballSpeed*Cos(bounceAngle) float ballVy = ballSpeed*-Sin(bounceAngle) ballVelocity = NVec2(ballVx, ballVy) return 0 } } func Colliding(a, b) { bool b = CPP.Physics.AxisAlignedCollision(a, b) return b }