main.lua 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. require("player")
  2. love.window.setTitle(' Tux phys game ')
  3. player = {}
  4. platform = {}
  5. block = {}
  6. canw = 800
  7. canv = 650
  8. function love.load()
  9. love.physics.setMeter(64) --1 meter = 64px
  10. world = love.physics.newWorld(0, 9*64, true) --hgrav=0,vgrav=9
  11. platform.body = love.physics.newBody(world, canw/2, canv-50/2)
  12. platform.shape = love.physics.newRectangleShape(canw, 50)
  13. platform.fixture = love.physics.newFixture(platform.body, platform.shape);
  14. player.img = love.graphics.newImage('images/tux.png')
  15. player.body = love.physics.newBody(world, canw/2, canv/2, "dynamic")
  16. player.shape = love.physics.newRectangleShape(player.img:getWidth(), player.img:getHeight())
  17. player.fixture = love.physics.newFixture(player.body, player.shape, 1)
  18. player.fixture:setRestitution(1)
  19. player.speed = 200
  20. player.y_velocity = 0
  21. player.jump = -300
  22. player.y = canv-50/2
  23. player.x = love.graphics.getWidth() / 2
  24. block.body = love.physics.newBody(world, 200, 550, "dynamic")
  25. block.shape = love.physics.newRectangleShape(0, 0, 50, 100)
  26. block.fixture = love.physics.newFixture(block.body, block.shape, 5)
  27. love.graphics.setBackgroundColor(104, 136, 248)
  28. love.window.setMode(canw, canv)
  29. end
  30. function love.draw()
  31. -- ground
  32. love.graphics.setColor(246, 246, 250)
  33. love.graphics.polygon("fill", platform.body:getWorldPoints(platform.shape:getPoints()))
  34. -- player
  35. love.graphics.setColor(255, 255, 255)
  36. love.graphics.draw(player.img, player.body:getX(), player.body:getY(), player.body:getAngle(),1,1,player.img:getWidth()/2, player.img:getHeight()/2)
  37. -- block
  38. love.graphics.setColor(50, 50, 50)
  39. love.graphics.polygon("fill", block.body:getWorldPoints(block.shape:getPoints()))
  40. love.graphics.setColor(0, 0, 0)
  41. love.graphics.print(player.body:getY(), canw/2, canv/2)
  42. love.graphics.print(platform.body:getY(), canw/3, canv/2)
  43. end