Skip to content

Instantly share code, notes, and snippets.

@AjayMT
Created March 10, 2012 16:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AjayMT/2011941 to your computer and use it in GitHub Desktop.
Save AjayMT/2011941 to your computer and use it in GitHub Desktop.
Main file of a pong clone clone created in lua,using Codea
-- Use this function to perform your initial setup
function setup()
    displayMode(FULLSCREEN)
    p1=Player()
    p2=Player()
    players={p1,p2}
    ball=Ball()
    ball.acc=vec2(3,math.random(-5,5))
    win=false
    lose=false
    saveProjectInfo("Description","Single player pong clone.First to five wins!")
end
-- This function gets called once every frame
function draw()
    -- This sets a dark background color.
    if win==false and lose==false then
        if p1.points>=5 then
            win=true
            sound(SOUND_JUMP, 834)
        elseif p2.points>=5 then
            lose=true
            sound(SOUND_EXPLODE, 34169)
        end
        background(28, 57, 32, 255)
        fill(0, 52, 255, 255)
        font("AppleColorEmoji")
        fontSize(20)
        textMode(CORNER)
        text(p2.points,(WIDTH/2)- 20,20)
        text(p1.points,(WIDTH/2)+ 20,20)
        p2.x=0
        for k,v in pairs(players) do
            v:draw()
        end
        if ball.pos.x>WIDTH - 40 then
            if ball.pos.y>=p1.y and ball.pos.y<=p1.y+150 then
                if p1.acc>0 then
                    ball.acc.x=-ball.acc.x
                    ball.acc.y = ball.acc.y + (p1.acc*0.65)
                else
                    ball.acc.x=-(ball.acc.x)
                    ball.acc.y = ball.acc.y + (p1.acc*0.65)
                end
                sound(SOUND_PICKUP, 28986)
            end
        elseif ball.pos.x<40 then
            if ball.pos.y>=p2.y and ball.pos.y<=p2.y+150 then
                if p2.acc>0 then
                    ball.acc.x=-ball.acc.x
                    ball.acc.y = ball.acc.y + (p2.acc*0.65)
                else
                    ball.acc.x=-(ball.acc.x)
                    ball.acc.y = ball.acc.y + (p2.acc*0.65)
                end
                sound(SOUND_PICKUP, 1015)
            end
        end
        if ball.pos.x<WIDTH - (WIDTH/4) then
            if ball.pos.y>p2.y +150 then
                p2.acc=4
            elseif ball.pos.y<p2.y then
                p2.acc= -4
            else
                p2.acc=0
            end
        end
        if ball.pos.x<0 then
            p1.points = p1.points + 1
            ball.pos=vec2(WIDTH/2,HEIGHT/2)
            ball.acc=vec2(3,math.random(-5,5))
            sound(SOUND_POWERUP, 16040)
        elseif ball.pos.x>WIDTH then
            p2.points = p2.points + 1
            ball.pos=vec2(WIDTH/2,HEIGHT/2)
            ball.acc=vec2(3,math.random(-5,5))
            sound(SOUND_SHOOT, 41045)
        end
        for k,v in pairs(players) do
            if v.y+150>HEIGHT then
                v.y = v.y - 4
            elseif v.y<0 then
                v.y = v.y + 4
            end
        end
        ball:draw()
        if ball.pos.y>HEIGHT or ball.pos.y <= 0 then
            ball.acc.y = -ball.acc.y
            sound(SOUND_BLIT, 42607)
        end
        if CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING then
            if CurrentTouch.y>p1.y then
                p1.acc=4
            elseif CurrentTouch.y<p1.y then
                p1.acc=-4
            end
        else
            p1.acc=0
        end
    elseif win then
        fill(255, 226, 0, 255)
        font("AppleColorEmoji")
        fontSize(100)
        textMode(CENTER)
        text("YOU WIN!!!",WIDTH/2,HEIGHT/2)
        fontSize(50)
        text("Tap to reset.",WIDTH/2,(HEIGHT/2)-100)
        if CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING then
            setup()
            lose=false
            win=false
        end
    elseif lose then
        fill(255, 102, 0, 255)
        font("AppleColorEmoji")
        fontSize(100)
        textMode(CENTER)
        text("YOU LOSE!!!",WIDTH/2,HEIGHT/2)
        fontSize(50)
        text("Tap to reset.",WIDTH/2,(HEIGHT/2)-100)
        if CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING then
            setup()
            win=false
            lose=false
        end
   end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment