Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Pool game

in General Posts: 50

Here is my pool game, any improvements would be appreciated, I still haven’t got the balls to go on the right side after they go in the pocket.

-- Pool
solids={1,2,3,4,5,6,7}
stripes={9,10,11,12,13,14,15}
function setup()
    displayMode(FULLSCREEN)
    HEIGHT=600
    w1 = physics.body(EDGE,vec2(95,55),vec2(WIDTH/2-30,55))
    d1 = physics.body(EDGE,vec2(WIDTH/2+30,55),vec2(WIDTH-95,55))
    w2 = physics.body(EDGE,vec2(55,100),vec2(55,HEIGHT-100))
    w3 = physics.body(EDGE,vec2(WIDTH-55,100),vec2(WIDTH-55,HEIGHT-100)) 
    w4 = physics.body(EDGE,vec2(95,HEIGHT-55),vec2(WIDTH/2-30,HEIGHT-55))
    d2 = physics.body(EDGE, vec2(WIDTH/2+30,HEIGHT-55),vec2(WIDTH-95,HEIGHT-55))
    physics.gravity(0,0)
    cue = move(300,HEIGHT/2,0,0)
    one = move(750,HEIGHT/2,0,0)
    two = move(862.5,300,0,0)
    three = move(860,320,0,0)
    four = move(870,230,0,0)
    five = move(830,245,0,0)
    six = move(800,265,0,0)
    seven = move(830,317.5,0,0)
    eight = move(800,300,0,0)
    nine = move(800,335,0,0)
    ten = move(775,275,0,0)
    eleven = move(775,315,0,0)
    twelve = move(830,282.5,0,0)
    thirteen = move(840,350,0,0)
    fourteen = move(870,370,0,0)
    fifteen = move(860,290,0,0)
    player=-1
end
function move(cuex,cuey,speedx,speedy)
    local p = physics.body(CIRCLE,20)
    p.restitution = .8
    p.linearVelocity = vec2(speedx,speedy)
    p.linearDamping = 0.8
    p.x = cuex
    p.y= cuey
    p.bullet = true
    return p
end
function poolangle(srcx,srcy,lenx,leny)
    local distx = lenx-srcx
    local disty = leny-srcy
    angle = math.deg(math.atan(disty/distx))
    return angle
end
ballplace=160
function inthehole(center_x, center_y, radius,p)
    dist = math.sqrt((center_x - p.x)^2 + (center_y - p.y)^2)
    if dist <= radius then
        if p == cue then
            p:destroy()
            p = move(200,300,0,0)    
        else
            ballp = ballplace
            ballplace = ballplace + 40
            player = -player
            if player == -1 then
                ballplace = WIDTH-ballplace
            end
            p:destroy()
            p = move(ballplace,650,0,0)
            if player == -1 then
                ballplace = ballp
            end
        end
    end
    return p
end
function hit(one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,cue)
    array = {one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,cue}
    for i=1,16 do
        array[i] = inthehole(50,50,30,array[i])
        array[i] = inthehole(50,HEIGHT-50,30,array[i])
        array[i] = inthehole(975,HEIGHT-50,30,array[i])
        array[i] = inthehole(975,50,30,array[i])
        array[i] = inthehole(512.5,35,25,array[i])
        array[i] = inthehole(512.5,HEIGHT-35,25,array[i])
    end
    return array[1],array[2],array[3],array[4],array[5],array[6],array[7],array[8],array[9],array[10],array[11],array[12],array[13],array[14],array[15],array[16]
end
-- This function gets called once every frame
function draw()
    gameover = true
    -- This sets a dark background color
    one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,cue = hit(one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,cue)
    pushStyle()
    strokeWidth(5)
    background(35, 83, 160, 255)
    sprite("Documents:hay",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
    fill(255)
    ellipse(cue.x, cue.y,40)
    fill(255, 255, 255, 255)
    sprite("Documents:8",eight.x,eight.y,40,40)
    sprite("Documents:1",one.x,one.y,40,40)
    sprite("Documents:2",two.x,two.y,40,40)
    sprite("Documents:3",three.x,three.y,40,40) 
    sprite("Documents:4",four.x,four.y,40,40) 
    sprite("Documents:5",five.x,five.y,40,40) 
    sprite("Documents:6",six.x,six.y,40,40) 
    sprite("Documents:7",seven.x,seven.y,40,40) 
    sprite("Documents:9",nine.x,nine.y,40,40) 
    sprite("Documents:10",ten.x,ten.y,40,40) 
    sprite("Documents:11",eleven.x,eleven.y,40,40) 
    sprite("Documents:12",twelve.x,twelve.y,40,40) 
    sprite("Documents:13",thirteen.x,thirteen.y,40,40) 
    sprite("Documents:14",fourteen.x,fourteen.y,40,40)
    sprite("Documents:15",fifteen.x,fifteen.y,40,40) 
    fontSize(25)
    text("Player One",100,730)
    text("Player Two",900,730)
    sprite("Documents:bad guy",900,650)
    sprite("Documents:good",100,650)
    if CurrentTouch.state == BEGAN then
        push=true
        srcx = cue.x
        srcy = cue.y
    elseif CurrentTouch.state == MOVING then
        lenx = CurrentTouch.x
        leny = CurrentTouch.y
        distx = lenx-srcx
        disty = leny-srcy
        line(cue.x,cue.y,cue.x-distx,cue.y-disty)
        --line(cue.x,cue.y,cue.x+(xchange/math.abs(xchange))*100,cue.y+(xchange/math.abs(xchange))*100*(ychange/xchange))
    elseif CurrentTouch.state == ENDED then
        if push then
            cue:destroy()
            cue = move(cue.x,cue.y,-distx*4,-disty*4)
            push=false
        end
    popStyle()
    end
end

Comments

  • dave1707dave1707 Mod
    Posts: 6,787

    @dmoeller You’re forgetting that no one can see any sprite you use from your Documents folder. When I run the game, all I see are the words player one, player two, and a white cue ball against a black background. If you want to share programs that use sprites, use the ones provided with Codea or provide a copy of your sprites along with the code.

  • Posts: 50

    Ok, sorry

  • dave1707dave1707 Mod
    Posts: 6,787

    @dmoeller If you want, you can replace the ball sprites with ellipses and use text to put a number on them.

  • dave1707dave1707 Mod
    Posts: 6,787

    @dmoeller Here’s a function I put together createPoolBalls that you can use for your pool balls.

    function setup()
        createPoolBalls()    
    end
    
    function createPoolBalls()
        poolBalls={}    -- pool ball table
        col={   color(255,255,0),color(0,0,255),color(255,0,0),color(150,0,255),
                color(255,0,180),color(0,255,0),color(255,50,100),color(0,0,0),
                color(255,255,0),color(0,0,255),color(255,0,0),color(150,0,255),
                color(255,0,180),color(0,255,0),color(255,50,100),color(255)}    
        for z=1,#col do -- loop thru color table
            poolBalls[z]=image(40,40)       
            setContext(poolBalls[z])       
            if z>8 then -- create striped ball
                fill(255)
                ellipse(20,20,40)   -- create solid white ball
                if z<16 then  
                    stroke(col[z])
                    strokeWidth(20)
                    line(10,20,30,20)   -- put stripe on ball
                end
            else
                fill(col[z])
                ellipse(20,20,40)   -- create solid color ball       
            end
            noStroke()
            fill(255)
            ellipse(20,20,20)   -- draw small white circle on ball
            if z<16 then
                fill(0)
                fontSize(15)
                text(z,20,20)   -- put number on ball 
            end      
            setContext()
        end
    end
    
    function draw()
        background(73, 162, 76, 255)
        for z=1,#col do
            if z==8 or z==16  then  -- 8 ball or cue ball
                sprite(poolBalls[z],z*20,400)
            elseif z>8 then
                sprite(poolBalls[z],30+(z-8)*50,300)    -- striped
            else
                sprite(poolBalls[z],30+z*50,500)        -- solid
            end
        end
    end
    
Sign In or Register to comment.