function on.escapeKey() titles = true game = false ballY = 10 ballX = 150 ballUp = false ballAngle = math.pi/2 paddle1 = 150 paddle2 = 150 score1 = 0 score2 = 0 twoHumans = false timeSpeed = 1 time = 0 timerDone = true hits = 0 timerMod = false escapeMe = false gameOver = false end on.escapeKey() function on.tabKey() if twoHumans == false then twoHumans = true else twoHumans = false end end function on.paint(gc) if titles then gc:setFont("sansserif", "b", 30) gc:drawString("PONG", 300/3, 200/2, "middle") -- prints the word PONG in the center of the screen gc:setFont("sansserif", "r", 10) gc:drawString("An arcade clone by Karl Noss", 10, 150, "bottom") gc:drawString("Press enter to play, tab to toggle modes", 10, 170, "bottom") gc:drawString("Version 1.0", 10, 190, "bottom") if twoHumans then gc:drawString("2P", 10, 10, "top") else gc:drawString("1P", 10, 10, "top") end elseif game then gc:drawRect(0, 0, 300, 200) gc:drawString(score2 .. " to " .. score1, 10, 10, "top") gc:fillRect(paddle2 - 15, 0, 30, 5) -- each paddle is 30 by 5 gc:fillRect(paddle1 - 15, 200 - 5, 30, 5) gc:fillRect(ballX - 5, ballY - 5, 10, 10) -- ball is 10 by 10 -- draws the ball and paddles if gameOver then gc:setFont("sansserif", "b", 30) gc:drawString("Game Over", 300/3, 200/2, "middle") return end -- draws the Game Over screen if not timerDone then return end if ballY <= 5 then -- checks to see if ball is in computer's zone if math.abs(ballX - paddle2) <= 30 and ballY > 0 then -- is the ball touching the paddle? -- code to hit ball y = 10 -- 10 is half the width of the paddle x = math.abs(paddle2 - ballX) ballAngle = math.atan(y/x) if paddle2 > ballX then -- if the ball is to the left of the paddle's center, we need it to be deflected off to the left ballLeft = true else ballLeft = false end ballUp = false getNewBallX() getNewBallY() setTimeSpeed() else score1 = score1 + 1 ballY = 10 ballX = 150 ballUp = false ballAngle = math.pi/2 paddle1 = 150 paddle2 = 150 hits = 0 timeSpeed = 1 timerDone = false timerMod = true time = timer.getMilliSecCounter() timer.start(3) if score1 == 10 then gameOver = true time = timer.getMilliSecCounter() --escapeMe = true end end elseif ballY >= 195 then -- checks to see if ball is in player's zone if math.abs(ballX - paddle1) <= 30 and ballY < 200 then -- is the ball touching the paddle? -- code to hit ball y = 10 -- 10 is half the width of the paddle x = math.abs(paddle1 - ballX) ballAngle = math.atan(y/x) if paddle1 > ballX then ballLeft = true else ballLeft = false end ballUp = true getNewBallX() getNewBallY() setTimeSpeed() else score2 = score2 + 1 ballY = 10 ballX = 150 ballUp = false ballAngle = math.pi/2 paddle1 = 150 paddle2 = 150 hits = 0 timeSpeed = 1 timerDone = false timerMod = true time = timer.getMilliSecCounter() timer.start(3) if score2 == 10 then gameOver = true time = timer.getMilliSecCounter() --escapeMe = true end end elseif ballX <= 0 then -- for when the ball is at the left edge of the screen ballLeft = false getNewBallX() getNewBallY() elseif ballX >= 300 then -- ball at right edge of screen ballLeft = true getNewBallX() getNewBallY() else -- for when the ball is not colliding with anything getNewBallX() getNewBallY() end end if not timerMod then -- Starts the repaint timer timerDone = false timer.start(0.01) end end function on.timer() if ( (timerMod) and (timer.getMilliSecCounter() - time > 1000) ) then timerDone = true timer.stop() timerMod = false if escapeMe then escapeMe = false on.escapeKey() end platform.window:invalidate() elseif not timerMod then timerDone = true timer.stop() platform.window:invalidate() end end function on.enterKey() game = true titles = false end function on.charIn(ch) if ch == '(' then on.arrowLeft() elseif ch == ')' then on.arrowRight() elseif ch == '+' then on.arrowUp() elseif ch == '-' then on.arrowDown() end end function on.arrowLeft() if paddle1 > 10 then paddle1 = paddle1 - 20 end end function on.arrowRight() if paddle1 < 290 then paddle1 = paddle1 + 20 end end function on.arrowUp() if paddle2 > 10 and twoHumans then paddle2 = paddle2 - 20 end end function on.arrowDown() if paddle2 < 290 and twoHumans then paddle2 = paddle2 + 20 end end function getNewBallX() if ballLeft == true then -- if the ball is going to be deflected to the left ballX = ballX - timeSpeed * math.cos(ballAngle) -- Essentially, the triangle's hypotenuse is 1 (arbitrarily picked by me) so cos(theta) = bX/1 else ballX = ballX + timeSpeed * math.cos(ballAngle) end if paddle2 + 20 > ballX and not twoHumans and ballY < 50 then paddle2 = paddle2 - 20 elseif paddle2 - 20 < ballX and not twoHumans and ballY < 50 then paddle2 = paddle2 + 20 end end function getNewBallY() if ballUp then ballY = ballY - timeSpeed * math.sin(ballAngle) else ballY = ballY + timeSpeed * math.sin(ballAngle) end end function setTimeSpeed() hits = hits + 1 if hits > 3 then timeSpeed = 1.25 end if hits > 10 then timeSpeed = 1.75 end end