From 406cac4cff148405ad7369eedc34cb472ba79b41 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 14 Jan 2022 08:13:36 -0500 Subject: [PATCH] Added scoring and ball reverts to center of screen --- Slang/script.slg | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/Slang/script.slg b/Slang/script.slg index 203c851..6b5fb7e 100644 --- a/Slang/script.slg +++ b/Slang/script.slg @@ -8,20 +8,24 @@ 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 xPosBall = SCREENW / 2 - float yPosBall = SCREENH / 2 - Vec2 ballPosition = NVec2(xPosBall, yPosBall) + 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) @@ -31,7 +35,7 @@ func Start() Vec2 rPaddlePosition = NVec2(rOffset, yPosPaddle) global Vec2 rPaddleTargetPosition = NVec2(rOffset, yPosPaddle) - global Sprite ballSpr = CPP.Graphics.Sprite("./square.png", ballPosition, ballScale, 0) + 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) @@ -122,6 +126,27 @@ func HandleBallBounce() 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) + return 0 + } + // Checks if colliding with left paddle bool coll = Colliding(ballSpr, lPaddle) if coll == true