Code: Select all
-- Shape Drop by Xcmd
-- Released as Public Domain
function initVars()
math.randomseed( os.time() )
ballImg = love.graphics.newImage("ball.png")
ballX = {}
ballY = {}
ballBody = {}
ballShape = {}
shapeList = {}
ballCount = -1
theShape = 1 -- 1: graphic ball, 2: drawn ball, 3: triangle, 4: rectangle, 5: unmoving square
theWorld = love.physics.newWorld(800,600)
theWorld:setGravity(0, 50)
local f = love.graphics.newFont(love.default_font, 12)
love.graphics.setFont(f)
pause = 1
speed = 10
groundAngle = 0
ground = love.physics.newBody(theWorld, 400, 300, 50)
groundRect = love.physics.newRectangleShape(ground,0,0,500,5,groundAngle)
instructions = "Left Click: Place Shape || Right Click: Change Shape || Space: Pause/Unpause || G: Rotate Ground || R: Reset"
end
function load()
initVars()
end
function update(dt)
if pause == 0 then
theWorld:update(dt)
if love.keyboard.isDown(love.key_g) then
if groundAngle < 360 then
groundAngle = groundAngle + (dt * speed)
ground:setAngle(groundAngle)
elseif groundAngle >= 360 then
groundAngle = 0
ground:setAngle(groundAngle)
end
end
if love.keyboard.isDown(love.key_f) then
if groundAngle > 0 then
groundAngle = groundAngle - (dt * speed)
ground:setAngle(groundAngle)
elseif groundAngle <= 0 then
groundAngle = 360
ground:setAngle(groundAngle)
end
end
end
end
function draw()
for i = 0, ballCount do
if shapeList[i] == 1 then
love.graphics.draw(ballImg, ballBody[i]:getX(), ballBody[i]:getY(), ballBody[i]:getAngle())
elseif shapeList[i] == 2 then
love.graphics.circle(1, ballBody[i]:getX(), ballBody[i]:getY(), ballShape[i]:getRadius())
elseif shapeList[i] == 3 or shapeList[i] == 4 then
love.graphics.polygon(love.draw_line, ballShape[i]:getPoints())
elseif shapeList[i] == 5 then
love.graphics.polygon(0, ballShape[i]:getPoints())
end
end
love.graphics.polygon(love.draw_line, groundRect:getPoints())
love.graphics.draw(instructions, 5, 15)
end
function mousepressed(x, y, button)
if button == love.mouse_left then
ballCount = ballCount + 1
ballX[ballCount] = x
ballY[ballCount] = y
ballBody[ballCount] = love.physics.newBody(theWorld, x, y)
ballBody[ballCount]:setMass(0, 0, 1, 1)
ballBody[ballCount]:setSpin(36)
ballBody[ballCount]:setVelocity(math.random(-50, 50),math.random(-50, 0))
if theShape == 1 or theShape == 2 then
ballShape[ballCount] = love.physics.newCircleShape(ballBody[ballCount], 14)
elseif theShape == 3 then
ballShape[ballCount] = love.physics.newPolygonShape(ballBody[ballCount], 0,0,28,28,0,28)
elseif theShape == 4 then
ballShape[ballCount] = love.physics.newRectangleShape(ballBody[ballCount], 0,0,28,28)
elseif theShape == 5 then
ballShape[ballCount] = love.physics.newRectangleShape(ballBody[ballCount], 0,0,28,28)
ballBody[ballCount]:setMass(0, 0, 0, 0)
ballBody[ballCount]:setSpin(0)
end
shapeList[ballCount] = theShape
end
if button == love.mouse_right then
if theShape < 5 then
theShape = theShape + 1
elseif theShape == 5 then
theShape = 1
end
end
end
function keypressed (key)
if key == love.key_space then
if pause == 0 then
pause = 1
elseif pause == 1 then
pause = 0
end
end
if key == love.key_r then
initVars()
end
if key == love.key_escape then
love.system.exit()
end
if key == love.key_rightbracket then
speed = speed + 10
end
if key == love.key_leftbracket then
speed = speed - 10
end
end