Advertisement
Guest User

uyhjtrfgeds

a guest
Jul 13th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.29 KB | None | 0 0
  1. function love.load()
  2.     -- Variables
  3.     score = 0
  4.  
  5.     -- Graphics stuff
  6.     love.graphics.setBackgroundColor( 255, 255, 255 )
  7.     icon = love.graphics.newImage( "icon.png" )
  8.     love.graphics.setIcon( icon )
  9.     font = love.graphics.newFont( "verdana.ttf", 10)
  10.     love.graphics.setFont( font )
  11.  
  12.     -- Le world and ground
  13.     world = love.physics.newWorld( 0, 9.81*128 )
  14.     world:setCallbacks( beginContact, endContact, preSolve, postSolve )
  15.     objects = {}
  16.  
  17.     -- Le ground
  18.     objects.ground = {}
  19.     objects.ground.body = love.physics.newBody( world, -150, 380 )
  20.     objects.ground.shape = love.physics.newRectangleShape( 1000, 100 )
  21.     objects.ground.fixture = love.physics.newFixture( objects.ground.body, objects.ground.shape )
  22.     objects.ground.fixture:setFriction( 0.9 )
  23.       objects.ground.fixture:setUserData( "Floor" )
  24.  
  25.       -- Le Walls
  26.       objects.walls = {}
  27.  
  28.       -- Le Left Wall
  29.       objects.walls.leftwall = {}
  30.       objects.walls.leftwall.body = love.physics.newBody( world, 0, 0 )
  31.       objects.walls.leftwall.shape = love.physics.newRectangleShape( 1, 1000 )
  32.       objects.walls.leftwall.fixture = love.physics.newFixture( objects.walls.leftwall.body, objects.walls.leftwall.shape )
  33.       objects.walls.leftwall.fixture:setRestitution( 0.4 )
  34.       objects.walls.leftwall.fixture:setUserData( "LeftWall" )
  35.  
  36.  
  37.       -- Le Right Wall
  38.       objects.walls.rightwall = {}
  39.       objects.walls.rightwall.body = love.physics.newBody( world, 300, 0 )
  40.       objects.walls.rightwall.shape = love.physics.newRectangleShape( 1, 1000 )
  41.       objects.walls.rightwall.fixture = love.physics.newFixture( objects.walls.rightwall.body, objects.walls.rightwall.shape )
  42.       objects.walls.rightwall.fixture:setRestitution( 0.4 )
  43.       objects.walls.rightwall.fixture:setUserData( "RightWall" )
  44.  
  45.  
  46.     -- Le Player
  47.     objects.player = {}
  48.     objects.player.body = love.physics.newBody( world, 142, 320, "dynamic" )
  49.       objects.player.shape = love.physics.newRectangleShape( 16, 16 )
  50.      objects.player.fixture = love.physics.newFixture( objects.player.body, objects.player.shape, 1 )
  51.       objects.player.fixture:setRestitution( 0.1 )
  52.       objects.player.fixture:setUserData( "Player" )
  53.       objects.player.onground = false
  54.  
  55.       -- Le Platforms and Pickups
  56.       generatePlatforms()
  57. end
  58.  
  59. function generatePlatforms()
  60.       objects.platforms = {}
  61.       objects.pickups = {}
  62.       lastY = 360
  63.     for i = 1, 10, 1 do
  64.         x, y = math.random( 0, 300 ), lastY - 60
  65.         lastY = y
  66.  
  67.         -- Platforms
  68.         t = {}
  69.         t.x = x
  70.         t.y = y
  71.         t.r = math.random( 0, 255 )
  72.         t.g = math.random( 0, 255 )
  73.         t.b = math.random( 0, 255 )
  74.         t.body = love.physics.newBody( world, x, y )
  75.         t.shape = love.physics.newRectangleShape( 64, 16 )
  76.         t.fixture = love.physics.newFixture( t.body, t.shape )
  77.         t.fixture:setUserData( "Floor" )
  78.         t.fixture:setFriction( 0.9 )
  79.         table.insert( objects.platforms, t )
  80.  
  81.         -- Pickups
  82.         p = {}
  83.         p.x = x
  84.         p.y = y - 18
  85.         p.r = 255
  86.         p.g = 255
  87.         p.b = 0
  88.         p.width = 6
  89.         p.height = 6
  90.         p.body = love.physics.newBody ( world, p.x, p.y )
  91.         p.shape = love.physics.newRectangleShape( p.width, p.height )
  92.         p.fixture = love.physics.newFixture( p.body, p.shape )
  93.         p.fixture:setUserData( "Pickup" )
  94.         table.insert( objects.pickups, p )
  95.     end
  96. end
  97.  
  98. function love.draw()
  99.     drawEntities()
  100.     drawHud()
  101. end
  102.  
  103. function drawEntities()
  104.     -- Floor
  105.     love.graphics.setColor( 220, 220, 220 )
  106.     love.graphics.polygon( "fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints()))
  107.  
  108.  
  109.     -- Platforms
  110.     for i,v in ipairs(objects.platforms) do
  111.         love.graphics.setColor( v.r, v.g, v.b )
  112.         love.graphics.polygon( "fill", v.body:getWorldPoints(v.shape:getPoints()))
  113.     end
  114.  
  115.     -- Pickups
  116.     for i,v in ipairs(objects.pickups) do
  117.         love.graphics.setColor( 0, 0, 0 )
  118.         love.graphics.rectangle( "fill", v.x - (v.width / 2) + 1, v.y - (v.height / 2) + 1, v.width, v.height )
  119.         love.graphics.setColor( v.r, v.g, v.b )
  120.         love.graphics.polygon( "fill", v.body:getWorldPoints(v.shape:getPoints()))
  121.     end
  122.  
  123.     -- Player
  124.     love.graphics.setColor( 255, 0, 0 )
  125.     love.graphics.polygon( "fill", objects.player.body:getWorldPoints(objects.player.shape:getPoints()))
  126. end
  127.  
  128. function drawHud()
  129.     love.graphics.setColor(0, 0, 0)
  130.     love.graphics.print(love.timer.getFPS() .. " fps", 2, 2)
  131.     love.graphics.print( "X: " .. math.floor(objects.player.body:getX()) .. " Y: " .. math.floor(objects.player.body:getY()), 2, 12 )
  132. end
  133.  
  134. function love.update(dt)
  135.     world:update(dt)
  136.  
  137.     -- Keys
  138.     if love.keyboard.isDown( "right" ) then
  139.         objects.player.body:applyForce( 400, 0 )
  140.     end
  141.     if love.keyboard.isDown( "left" ) then
  142.         objects.player.body:applyForce( -400, 0 )
  143.     end
  144.  
  145. end
  146.  
  147. function love.keypressed( key, unicode )
  148.     if key == " " and objects.player.onground then
  149.         objects.player.body:applyForce( 0, -7239 )
  150.     end
  151.     if key == "g" then
  152.           for i,v in ipairs(objects.platforms) do
  153.               v.body:destroy()
  154.         end
  155.           for i,v in ipairs(objects.pickups) do
  156.               v.body:destroy()
  157.         end
  158.         generatePlatforms()
  159.     end
  160. end
  161.  
  162. function beginContact( a, b, coll )
  163.     print( type(coll) )
  164.     if a:getUserData() == "Floor" and b:getUserData() == "Player" then
  165.         objects.player.onground = true
  166.     end
  167.     if a:getUserData() == "Player" and b:getUserData() == "Floor" then
  168.         objects.player.onground = true
  169.     end
  170.     if a:getUserData() == "Player" and b:getUserData() == "Pickup" then
  171.         for i,v in ipairs( objects.pickups ) do
  172.             if v.fixture == b then
  173.                 objects.pickups[i].body:destroy()
  174.                 objects.pickups[i] = nil
  175.             end
  176.         end
  177.     end
  178. end
  179.  
  180. function endContact( a, b, coll)
  181.     if a:getUserData() == "Floor" and b:getUserData() == "Player" then
  182.         objects.player.onground = false
  183.     end
  184.     if a:getUserData() == "Player" and b:getUserData() == "Floor" then
  185.         objects.player.onground = false
  186.     end
  187. end
  188.  
  189. function preSolve( a, b, coll)
  190.  
  191. end
  192.  
  193. function postSolve( a, b, coll)
  194.  
  195. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement