Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function love.load()
- -- Variables
- score = 0
- -- Graphics stuff
- love.graphics.setBackgroundColor( 255, 255, 255 )
- icon = love.graphics.newImage( "icon.png" )
- love.graphics.setIcon( icon )
- font = love.graphics.newFont( "verdana.ttf", 10)
- love.graphics.setFont( font )
- -- Le world and ground
- world = love.physics.newWorld( 0, 9.81*128 )
- world:setCallbacks( beginContact, endContact, preSolve, postSolve )
- objects = {}
- -- Le ground
- objects.ground = {}
- objects.ground.body = love.physics.newBody( world, -150, 380 )
- objects.ground.shape = love.physics.newRectangleShape( 1000, 100 )
- objects.ground.fixture = love.physics.newFixture( objects.ground.body, objects.ground.shape )
- objects.ground.fixture:setFriction( 0.9 )
- objects.ground.fixture:setUserData( "Floor" )
- -- Le Walls
- objects.walls = {}
- -- Le Left Wall
- objects.walls.leftwall = {}
- objects.walls.leftwall.body = love.physics.newBody( world, 0, 0 )
- objects.walls.leftwall.shape = love.physics.newRectangleShape( 1, 1000 )
- objects.walls.leftwall.fixture = love.physics.newFixture( objects.walls.leftwall.body, objects.walls.leftwall.shape )
- objects.walls.leftwall.fixture:setRestitution( 0.4 )
- objects.walls.leftwall.fixture:setUserData( "LeftWall" )
- -- Le Right Wall
- objects.walls.rightwall = {}
- objects.walls.rightwall.body = love.physics.newBody( world, 300, 0 )
- objects.walls.rightwall.shape = love.physics.newRectangleShape( 1, 1000 )
- objects.walls.rightwall.fixture = love.physics.newFixture( objects.walls.rightwall.body, objects.walls.rightwall.shape )
- objects.walls.rightwall.fixture:setRestitution( 0.4 )
- objects.walls.rightwall.fixture:setUserData( "RightWall" )
- -- Le Player
- objects.player = {}
- objects.player.body = love.physics.newBody( world, 142, 320, "dynamic" )
- objects.player.shape = love.physics.newRectangleShape( 16, 16 )
- objects.player.fixture = love.physics.newFixture( objects.player.body, objects.player.shape, 1 )
- objects.player.fixture:setRestitution( 0.1 )
- objects.player.fixture:setUserData( "Player" )
- objects.player.onground = false
- -- Le Platforms and Pickups
- generatePlatforms()
- end
- function generatePlatforms()
- objects.platforms = {}
- objects.pickups = {}
- lastY = 360
- for i = 1, 10, 1 do
- x, y = math.random( 0, 300 ), lastY - 60
- lastY = y
- -- Platforms
- t = {}
- t.x = x
- t.y = y
- t.r = math.random( 0, 255 )
- t.g = math.random( 0, 255 )
- t.b = math.random( 0, 255 )
- t.body = love.physics.newBody( world, x, y )
- t.shape = love.physics.newRectangleShape( 64, 16 )
- t.fixture = love.physics.newFixture( t.body, t.shape )
- t.fixture:setUserData( "Floor" )
- t.fixture:setFriction( 0.9 )
- table.insert( objects.platforms, t )
- -- Pickups
- p = {}
- p.x = x
- p.y = y - 18
- p.r = 255
- p.g = 255
- p.b = 0
- p.width = 6
- p.height = 6
- p.body = love.physics.newBody ( world, p.x, p.y )
- p.shape = love.physics.newRectangleShape( p.width, p.height )
- p.fixture = love.physics.newFixture( p.body, p.shape )
- p.fixture:setUserData( "Pickup" )
- table.insert( objects.pickups, p )
- end
- end
- function love.draw()
- drawEntities()
- drawHud()
- end
- function drawEntities()
- -- Floor
- love.graphics.setColor( 220, 220, 220 )
- love.graphics.polygon( "fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints()))
- -- Platforms
- for i,v in ipairs(objects.platforms) do
- love.graphics.setColor( v.r, v.g, v.b )
- love.graphics.polygon( "fill", v.body:getWorldPoints(v.shape:getPoints()))
- end
- -- Pickups
- for i,v in ipairs(objects.pickups) do
- love.graphics.setColor( 0, 0, 0 )
- love.graphics.rectangle( "fill", v.x - (v.width / 2) + 1, v.y - (v.height / 2) + 1, v.width, v.height )
- love.graphics.setColor( v.r, v.g, v.b )
- love.graphics.polygon( "fill", v.body:getWorldPoints(v.shape:getPoints()))
- end
- -- Player
- love.graphics.setColor( 255, 0, 0 )
- love.graphics.polygon( "fill", objects.player.body:getWorldPoints(objects.player.shape:getPoints()))
- end
- function drawHud()
- love.graphics.setColor(0, 0, 0)
- love.graphics.print(love.timer.getFPS() .. " fps", 2, 2)
- love.graphics.print( "X: " .. math.floor(objects.player.body:getX()) .. " Y: " .. math.floor(objects.player.body:getY()), 2, 12 )
- end
- function love.update(dt)
- world:update(dt)
- -- Keys
- if love.keyboard.isDown( "right" ) then
- objects.player.body:applyForce( 400, 0 )
- end
- if love.keyboard.isDown( "left" ) then
- objects.player.body:applyForce( -400, 0 )
- end
- end
- function love.keypressed( key, unicode )
- if key == " " and objects.player.onground then
- objects.player.body:applyForce( 0, -7239 )
- end
- if key == "g" then
- for i,v in ipairs(objects.platforms) do
- v.body:destroy()
- end
- for i,v in ipairs(objects.pickups) do
- v.body:destroy()
- end
- generatePlatforms()
- end
- end
- function beginContact( a, b, coll )
- print( type(coll) )
- if a:getUserData() == "Floor" and b:getUserData() == "Player" then
- objects.player.onground = true
- end
- if a:getUserData() == "Player" and b:getUserData() == "Floor" then
- objects.player.onground = true
- end
- if a:getUserData() == "Player" and b:getUserData() == "Pickup" then
- for i,v in ipairs( objects.pickups ) do
- if v.fixture == b then
- objects.pickups[i].body:destroy()
- objects.pickups[i] = nil
- end
- end
- end
- end
- function endContact( a, b, coll)
- if a:getUserData() == "Floor" and b:getUserData() == "Player" then
- objects.player.onground = false
- end
- if a:getUserData() == "Player" and b:getUserData() == "Floor" then
- objects.player.onground = false
- end
- end
- function preSolve( a, b, coll)
- end
- function postSolve( a, b, coll)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement