main.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. -- vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent:
  2. -- Callback functions
  3. -- see http://www.love2d.org/wiki/Tutorial:Callback_Functions
  4. --
  5. -- for key constants, see http://www.love2d.org/wiki/KeyConstant
  6. --
  7. -- love.load() - at the game's start, used to initialise ressources
  8. -- love.update(dt) - the game loop function, where "dt" stands
  9. -- for the fraction of seconds since the function was called last
  10. -- love.draw() - graphics output loop
  11. -- love.mousepressed(x, y, button) - mouse down event callback function,
  12. -- where x, y the mouse's position, and button the number of the
  13. -- button pressed.
  14. -- love.mousereleased(x, y, button) - mouse button release event callback function,
  15. -- see mousepressed
  16. -- love.keypressed(key) - key press event callback function, where key is either
  17. -- a character, or any of the constants like "up", "down", "left", "right"
  18. -- love.keyreleased(key) - key release event callback function - see keypressed
  19. -- love.focus(f) - windows focus received/lost event callback function, where f
  20. -- is a boolean (true = focus gained, false = focus lost)
  21. -- love.quit - windows close event callback function
  22. function love.load()
  23. local init = require("init")
  24. init.load()
  25. end
  26. function turnLeft(direction)
  27. if direction == "left" then
  28. newDirection = "down"
  29. end
  30. if direction == "down" then
  31. newDirection = "right"
  32. end
  33. if direction == "right" then
  34. newDirection = "up"
  35. end
  36. if direction == "up" then
  37. newDirection = "left"
  38. end
  39. return newDirection
  40. end
  41. function turnRight(direction)
  42. if direction == "left" then
  43. newDirection = "up"
  44. end
  45. if direction == "down" then
  46. newDirection = "left"
  47. end
  48. if direction == "right" then
  49. newDirection = "down"
  50. end
  51. if direction == "up" then
  52. newDirection = "right"
  53. end
  54. return newDirection
  55. end
  56. function movePlayer(x, y, toDirection)
  57. if toDirection == "left" then
  58. x = x - 1
  59. end
  60. if toDirection == "right" then
  61. x = x + 1
  62. end
  63. if toDirection == "up" then
  64. y = y - 1
  65. end
  66. if toDirection == "down" then
  67. y = y + 1
  68. end
  69. return x, y
  70. end
  71. function love.keypressed(key)
  72. if key == p1_keyleft then
  73. p1_direction = turnLeft(p1_direction)
  74. end
  75. -- what happens if p1 presses right?
  76. if key == p1_keyright then
  77. p1_direction = turnRight(p1_direction)
  78. end
  79. -- what happens if p2 presses left?
  80. if key == p2_keyleft then
  81. p2_direction = turnLeft(p2_direction)
  82. end
  83. -- what happens if p2 presses right?
  84. if key == p2_keyright then
  85. p2_direction = turnRight(p2_direction)
  86. end
  87. end
  88. function statePlayer(currentstate)
  89. if collisionArray[x1][y1] == 0 then
  90. else
  91. p1_state = "crashed"
  92. end
  93. end
  94. function love.update(dt)
  95. if love.keyboard.isDown(game_keyquit) then
  96. love.event.quit()
  97. end
  98. -- Handle collision Array.
  99. -- TODO: Implement collision detection
  100. if (p1_state == "alive" and p2_state == "alive") then
  101. -- Player 1
  102. x1, y1 = movePlayer(x1, y1, p1_direction)
  103. if (y1 >= maxHeight) or (y1 <= 0) or (x1 >= maxWidth) or (x1 <= 0) then
  104. p1_state = "crashed"
  105. end
  106. if not (p1_state == "crashed") then
  107. if collisionArray[x1][y1] > 0 then
  108. p1_state = "crashed"
  109. else
  110. collisionArray[x1][y1] = 1
  111. end
  112. end
  113. -- Player 2
  114. x2, y2 = movePlayer(x2, y2, p2_direction)
  115. if (y2 >= maxHeight) or (y2 <= 0) or (x2 >= maxWidth) or (x2 <= 0) then
  116. p2_state = "crashed"
  117. end
  118. if not (p2_state == "crashed") then
  119. if collisionArray[x2][y2] > 0 then
  120. p2_state = "crashed"
  121. else
  122. collisionArray[x2][y2] = 2
  123. end
  124. end
  125. end
  126. end
  127. function love.draw()
  128. -- Players play in canvas ...
  129. love.graphics.setCanvas(canvas)
  130. -- Player 1
  131. love.graphics.setColor(p1_color)
  132. love.graphics.point(x1, y1)
  133. -- Player 2
  134. love.graphics.setColor(p2_color)
  135. love.graphics.point(x2, y2)
  136. -- Back to the screen
  137. love.graphics.setCanvas()
  138. love.graphics.setBackgroundColor(bg_color)
  139. -- Border
  140. love.graphics.setColor(border_color)
  141. love.graphics.rectangle("line", 0, 0, maxWidth - 1, maxHeight - 1)
  142. -- Draw the Player Canvas
  143. love.graphics.setColor(255, 255, 255, 255)
  144. love.graphics.draw(canvas)
  145. -- Some Text over the Canvas
  146. love.graphics.setColor(text_color)
  147. love.graphics.print ('p1:' .. p1_state .. ' p2:' .. p2_state , 25, 25)
  148. end