Z-Sharp/Slang/script.slg
2022-01-13 10:07:42 -05:00

72 lines
2.0 KiB
Plaintext

int SCREENW = 900
int SCREENH = 600
int scoreOne = 0
int scoreTwo = 0
float paddleMoveSpeed = 200
func Main(input, in)
{
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)
Vec2 ballScale = NVec2(16, 16)
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 - 15
Vec2 rPaddlePosition = NVec2(rOffset, yPosPaddle)
global Vec2 rPaddleTargetPosition = NVec2(15, yPosPaddle)
global Sprite ballSprite = CPP.Graphics.Sprite("./square.png", ballPosition, ballScale, 0)
global Sprite lPaddle = CPP.Graphics.Sprite("./square.png", lPaddlePosition, paddleScale, 0)
global Sprite rPaddle = CPP.Graphics.Sprite("./square.png", rPaddlePosition, paddleScale, 0)
}
func Update(deltaTime)
{
//print deltaTime
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 * deltaTime
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 * deltaTime
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 * 15
float newY = lPaddleTargetPosition.y
float lerpedY = Lerp(oldY, newY, stopSpeed)
//print "0 < " + newY + " < " + SCREENH
lPaddle.position = NVec2(newX, lerpedY)
print "FPS: " + 1 / deltaTime
// Finally draws all of the sprites
CPP.Graphics.Draw(ballSprite)
CPP.Graphics.Draw(lPaddle)
CPP.Graphics.Draw(rPaddle)
}