main.lua 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. -- vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent:
  2. -- kate: space-indent on; indent-width 4; mixedindent off;
  3. -- Callback functions
  4. -- see http://www.love2d.org/wiki/Tutorial:Callback_Functions
  5. --
  6. -- for key constants, see http://www.love2d.org/wiki/KeyConstant
  7. --
  8. -- love.load() - at the game's start, used to initialise ressources
  9. -- love.update(dt) - the game loop function, where "dt" stands
  10. -- for the fraction of seconds since the function was called last
  11. -- love.draw() - graphics output loop
  12. -- love.mousepressed(x, y, button) - mouse down event callback function,
  13. -- where x, y the mouse's position, and button the number of the
  14. -- button pressed.
  15. -- love.mousereleased(x, y, button) - mouse button release event callback function,
  16. -- see mousepressed
  17. -- love.keypressed(key) - key press event callback function, where key is either
  18. -- a character, or any of the constants like "up", "down", "left", "right"
  19. -- love.keyreleased(key) - key release event callback function - see keypressed
  20. -- love.focus(f) - windows focus received/lost event callback function, where f
  21. -- is a boolean (true = focus gained, false = focus lost)
  22. -- love.quit - windows close event callback function
  23. require("init")
  24. require("players")
  25. function love.load()
  26. init.load()
  27. players.load()
  28. end
  29. function love.keypressed(key)
  30. if key == p1.keyleft then
  31. p1.direction = players.turnLeft(p1.direction)
  32. end
  33. -- what happens if p1 presses right?
  34. if key == p1.keyright then
  35. p1.direction = players.turnRight(p1.direction)
  36. end
  37. -- what happens if p2 presses left?
  38. if key == p2.keyleft then
  39. p2.direction = players.turnLeft(p2.direction)
  40. end
  41. -- what happens if p2 presses right?
  42. if key == p2.keyright then
  43. p2.direction = players.turnRight(p2.direction)
  44. end
  45. end
  46. function love.update(dt)
  47. update_players(dt)
  48. if love.keyboard.isDown(game_keyquit) then
  49. love.event.quit()
  50. end
  51. end
  52. function love.draw()
  53. -- Call our player drawing function
  54. draw_players()
  55. -- Some text over the Canvas
  56. love.graphics.setCanvas(canvas)
  57. love.graphics.setColor(text_color)
  58. font = love.graphics.newFont('SourceCodePro-Regular.ttf', 28)
  59. font:setFilter('linear')
  60. -- Set font before drawing text .. and something is wrong here...
  61. love.graphics.setFont(font);
  62. love.graphics.print ('p1:' .. p1.state .. ' p2:' .. p2.state , 25, 25, 0, 1, 1)
  63. -- Back to the screen
  64. love.graphics.setCanvas()
  65. love.graphics.setBackgroundColor(bg_color)
  66. -- Border
  67. love.graphics.setColor(border_color)
  68. love.graphics.rectangle("line", 0, 0, maxWidth - 1, maxHeight - 1)
  69. -- Draw the Player Canvas
  70. love.graphics.setColor(255, 255, 255, 255)
  71. love.graphics.draw(canvas)
  72. end