main.fnl 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. (local (w h) (love.window.getMode))
  2. (local keys {:a [:left -1] :z [:left 1] :up [:right -1] :down [:right 1]})
  3. (global Pong {})
  4. (set Pong.__index Pong)
  5. (fn Pong.init [cls]
  6. (let [t {:x 100 :y 100
  7. :dx 2 :dy 1
  8. :left (- (/ h 2) 50) :right (- (/ h 2) 50)
  9. :speed 1 :ball-speed 200
  10. :pause false}]
  11. (setmetatable t cls)))
  12. (fn Pong.on-paddle? [self]
  13. (or (and (< self.x 20)
  14. (< self.left self.y) (< self.y (+ self.left 100)))
  15. (and (< (- w 20) self.x)
  16. (< self.right self.y) (< self.y (+ self.right 100)))))
  17. (fn Pong.update [self dt set-mode]
  18. (when (not self.pause)
  19. (set self.x (+ self.x (* self.dx dt self.ball-speed)))
  20. (set self.y (+ self.y (* self.dy dt self.ball-speed)))
  21. (each [key action (pairs keys)]
  22. (let [[player dir] action]
  23. (when (love.keyboard.isDown key)
  24. (tset self player (+ (. self player) (* dir self.speed))))))
  25. (when (or
  26. (< self.y 0)
  27. (> self.y h))
  28. (set self.dy (- 0 self.dy)))
  29. (when (: self :on-paddle?)
  30. (set self.dx (- 0 self.dx)))
  31. (when (< self.x 0)
  32. (set-mode "mode.end" "2"))
  33. (when (> self.x w)
  34. (set-mode "mode.end" "1"))))
  35. (fn Pong.keypressed [self key set-mode]
  36. (when (= "p" key)
  37. (set self.pause (not self.pause))))
  38. (fn Pong.draw [self m]
  39. (when (not self.pause)
  40. (love.graphics.rectangle "fill" 10 self.left 10 100)
  41. (love.graphics.rectangle "fill" (- w 20) self.right 10 100)
  42. (love.graphics.circle "fill" self.x self.y 10))
  43. (when self.pause
  44. (love.graphics.printf "Press 'p' to unpause" 0 (/ h 3) w "center")))
  45. Pong