Skip to content

Instantly share code, notes, and snippets.

@Bri-G
Created March 12, 2012 17:29
Show Gist options
  • Save Bri-G/2023503 to your computer and use it in GitHub Desktop.
Save Bri-G/2023503 to your computer and use it in GitHub Desktop.
ball in a box
-- Use this function to perform your initial setup
function setup()
    
    displayMode(FULLSCREEN)
    boxheight = 50
    boxwidth = 50
    ballX = 400
    ballY = 400
    ballZ = 74
    updown = -4
    leftright = -4
    frontback = 4
    offset = 75
    diameter = 20
 --   sc = 1
    rot = 0
end
-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(46, 46, 186, 255)
    orientation()
    -- This sets the line thickness
    stroke(64, 43, 75, 255)
    strokeWidth(5)
    -- Do your drawing here
    for loop = 0, boxeshigh do
        endX = startX + barwidth
        endY = startY + loop*boxheight
        line(startX, endY, endX, endY)
    end
        line(startX-50, startY-50, endX+50, startY-50)
    line(startX-100, startY-100, endX+100, startY-100)
    line(startX-150, startY-150, endX+150, startY-150)
    boxadjd = ((barwidth + 300)/boxeswide)
    for loop = 0, boxeswide do
        newX = startX + loop*boxwidth
        lowerX = startX + loop*boxadjd - 150
        endY = startY + barheight
        line(newX, startY, newX, endY)
        line(newX, startY, lowerX, startY-150 )
    end
    endY = startY
    endX = startX + barwidth
    motion()
    strokeWidth(2)
    stroke(113, 62, 67, 255)
    fill(231, 10, 10, 255)
    ellipse(ballX,ballY,200,200)
    for loop = 1, 10 do
        lp = 11-loop
        ewidth = lp*(30-lp)
        ellipse(ballX,ballY,ewidth,200)
    end
    strokeWidth(2)
    noFill()
    for loop = 1, 10 do
        lp = 11-loop
        ewidth = lp*(30-lp)
        ellipse(ballX,ballY,200,ewidth)
    end
    strokeWidth(4)
end
function orientation()
    if WIDTH > 1000 then
        startX = 162
        startY = 268
        barwidth = WIDTH - 2*162
        barheight = HEIGHT - 368
    else
        startX = 134
        startY = 324
        barwidth = WIDTH - 2*134
        barheight = HEIGHT - 474
    end
    boxeshigh = barheight/boxheight
    boxeswide = barwidth/boxwidth  
end
function motion()
    rot = rot + 1
    if rot > 90 then rot = 0 end
    if WIDTH < 1000 then
        if ballY > 750 + ballZ then
            sound(SOUND_HIT, 1234)
            updown = -4        
        elseif ballY < 450 - ballZ then
            sound(SOUND_HIT, 1234)
            updown = 4
        end
        if ballX > 520 + ballZ then
            sound(SOUND_HIT, 1234)
            leftright = -4
        elseif ballX < 220 - ballZ then
            sound(SOUND_HIT, 1234) 
            leftright = 4
        end 
        if ballZ > 150 then
            sound(SOUND_HIT, 1234)
            frontback = - 1
        elseif ballZ < 0 then
            sound(SOUND_HIT, 1234)
            frontback = 1
        end
    elseif WIDTH >1000 then
        if ballY > 520 + ballZ then
            sound(SOUND_HIT, 1234)
            updown = -4        
        elseif ballY < 350 - ballZ then
            sound(SOUND_HIT, 1234)
            updown = 4
        end
        if ballX > 800 + ballZ then
            sound(SOUND_HIT, 1234)
            leftright = -4
        elseif ballX < 200 - ballZ then
            sound(SOUND_HIT, 1234) 
            leftright = 4
        end 
        if ballZ > 150 then
            sound(SOUND_HIT, 1234)
            frontback = - 1
        elseif ballZ < 0 then
            sound(SOUND_HIT, 1234)
            frontback = 1
        end      
    end 
    ballY = ballY + updown
    ballX = ballX + leftright
    ballZ = ballZ + frontback
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment