players.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. -- vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent:
  2. -- kate: space-indent on; indent-width 4; mixedindent off;
  3. players = {}
  4. p1 = {}
  5. p2 = {}
  6. function players.load()
  7. -- Keys for player control
  8. p1.keyleft = "h"
  9. p1.keyright = "j"
  10. p2.keyleft = "left"
  11. p2.keyright = "right"
  12. -- Player's colors
  13. p1.color = {255, 20, 20, 255}
  14. p2.color = {20, 255, 20, 255}
  15. -- Player's states
  16. p1.state = "alive"
  17. p2.state = "alive"
  18. -- Player's starting directions
  19. p1.direction = "right"
  20. p2.direction = "left"
  21. -- Player's starting points
  22. p1.x, p1.y = maxWidth / 4, maxHeight / 2
  23. p2.x, p2.y = maxWidth - maxWidth / 4, maxHeight / 2
  24. end
  25. function players.turnLeft(direction)
  26. if direction == "left" then
  27. newDirection = "down"
  28. end
  29. if direction == "down" then
  30. newDirection = "right"
  31. end
  32. if direction == "right" then
  33. newDirection = "up"
  34. end
  35. if direction == "up" then
  36. newDirection = "left"
  37. end
  38. return newDirection
  39. end
  40. function players.turnRight(direction)
  41. if direction == "left" then
  42. newDirection = "up"
  43. end
  44. if direction == "down" then
  45. newDirection = "left"
  46. end
  47. if direction == "right" then
  48. newDirection = "down"
  49. end
  50. if direction == "up" then
  51. newDirection = "right"
  52. end
  53. return newDirection
  54. end
  55. function players.movePlayer(x, y, toDirection)
  56. if toDirection == "left" then
  57. x = x - 1
  58. end
  59. if toDirection == "right" then
  60. x = x + 1
  61. end
  62. if toDirection == "up" then
  63. y = y - 1
  64. end
  65. if toDirection == "down" then
  66. y = y + 1
  67. end
  68. return x, y
  69. end
  70. function players.statePlayer(x, y, oldState)
  71. -- Handle collision Array.
  72. state = oldState
  73. if (y >= maxHeight) or (y <= 0) or (x >= maxWidth) or (x <= 0) then
  74. state = "crashed"
  75. end
  76. if not (state == "crashed") then
  77. if collisionArray[x][y] > 0 then
  78. state = "crashed"
  79. else
  80. collisionArray[x][y] = 1
  81. end
  82. end
  83. return state
  84. end
  85. -- Those functions for the Löve 2D game loop
  86. function update_players(dt)
  87. if (p1.state == "alive" and p2.state == "alive") then
  88. p1.x, p1.y = players.movePlayer(p1.x, p1.y, p1.direction)
  89. p1.state = players.statePlayer(p1.x, p1.y, p1.state)
  90. p2.x, p2.y = players.movePlayer(p2.x, p2.y, p2.direction)
  91. p2.state = players.statePlayer(p2.x, p2.y, p2.state)
  92. end
  93. end
  94. -- Those functions for the Löve 2D drawing loop
  95. function draw_players()
  96. -- Players play in canvas ...
  97. love.graphics.setCanvas(canvas)
  98. -- Player 1
  99. love.graphics.setColor(p1.color)
  100. love.graphics.point(p1.x, p1.y)
  101. -- Player 2
  102. love.graphics.setColor(p2.color)
  103. love.graphics.point(p2.x, p2.y)
  104. end