math.randomseed(os.time()) -- Create Empty Images For Paddles and Ball Player = {} Player[1] = { x = 2, y = 50, speed = 2, score = 0, img = Image.createEmpty(16, 64) } Player[2] = { x = 462 , y = 130, speed = 2.4, score = 0, img = Image.createEmpty(16, 64) } Ball = { x = 240, y = math.random(60,180), xspeed = -3, yspeed = 3, img = Image.createEmpty(8, 8) } -- Color The Images We Created Player[1].img:clear(Color.new(255,128,64)) Player[2].img:clear(Color.new(128,0,64)) Ball.img:clear(Color.new(218,218,218)) -- Variables and Tables Resolution = { width = 480, height = 272 } gamestate = "paused" white = Color.new(255,255,255) whoScored = nil paddleSpot = math.random(Player[2].img:height() ) -- Create Functions function drawImages() screen:blit(Player[1].x,Player[1].y,Player[1].img) screen:blit(Player[2].x,Player[2].y,Player[2].img) screen:blit(Ball.x,Ball.y,Ball.img) end function ballMovement() if Ball.y <= 0 then Ball.yspeed = 3 end if Ball.y + Ball.img:height() > Resolution.height then Ball.yspeed = -3 end Ball.x = Ball.x + Ball.xspeed Ball.y = Ball.y + Ball.yspeed end function paused() pad = Controls.read() screen:print(160,145,"Press X To Resume Play",white) if pad:cross() then gamestate = "playing" end end function playerMovement() pad = Controls.read() if pad:up() and Player[1].y > 0 then Player[1].y = Player[1].y - Player[1].speed end if pad:down() and Player[1].y < ( Resolution.height - Player[1].img:height() ) then Player[1].y = Player[1].y + Player[1].speed end if pad:start() then gamestate = "paused" end end function checkCollision(obj1,obj2) if (obj1.x + obj1.img:width() > obj2.x) and (obj1.x < obj2.x + obj2.img:width() ) and (obj1.y + obj1.img:height() > obj2.y) and (obj1.y < obj2.y + obj2.img:height() ) then if (obj1.y > obj2.y) and obj1.y < (obj2.y + obj2.img:height()/2 ) then obj1.yspeed = -3 else obj1.yspeed = 3 end if obj2 == Player[1] then obj1.xspeed = 3 else obj1.xspeed = -3 end end end function checkScore() if Ball.x < 0 - Ball.img:width() then gamestate = "scored" Player[2].score = Player[2].score + 1 whoScored = "Player 2" Ball.x = 240 Ball.y = math.random(80,160) Ball.xpeed = 3 end if Ball.x > Resolution.width then gamestate = "scored" Player[1].score = Player[1].score + 1 whoScored = "Player 1" Ball.x = 240 Ball.y = math.random(80,160) Ball.xspeed = -3 end end function drawScore() screen:print(10, 5, "Player 1",white) screen:print(10,15,Player[1].score,white) screen:print(400, 5, "Player 2",white) screen:print(400,15,Player[2].score,white) end function hasScored() pad = Controls.read() if Player[1].score == 10 then screen:print(160,135, "Player 1 Wins The Game",white) elseif Player[2].score == 10 then screen:print(160,135, "Player 2 Wins The Game",white) else screen:print(160,135,whoScored .. " Scored 1 Point!",white) end screen:print(160,145,"Press X To Resume Play",white) if pad:cross() then if Player[1].score == 10 or Player[2].score == 10 then Player[1].score = 0 Player[2].score = 0 end gamestate = "playing" end end function cpuAI() if Ball.xspeed == 3 and Ball.x > 240 then if Player[2].y + paddleSpot < Ball.y and Player[2].y < ( Resolution.height - Player[2].img:height() ) then Player[2].y = Player[2].y + Player[2].speed paddleSpot = math.random(Player[2].img:height() ) end if Player[2].y + paddleSpot > Ball.y and Player[2].y > 0 then Player[2].y = Player[2].y - Player[2].speed paddleSpot = math.random(Player[2].img:height() ) end end end while true do screen:clear() drawImages() drawScore() if gamestate == "paused" then paused() end if gamestate == "scored" then hasScored() end if gamestate == "playing" then playerMovement() ballMovement() cpuAI() checkCollision(Ball,Player[1]) checkCollision(Ball,Player[2]) checkScore() end screen.waitVblankStart() screen.flip() end