|
@@ -0,0 +1,72 @@
|
|
|
+; (local (speed ball-speed) (values 1 200))
|
|
|
+(local (w h) (love.window.getMode))
|
|
|
+; (local _state {:x 100 :y 100 :dx 2 :dy 1 :left (- (/ h 2) 50) :right (- (/ h 2) 50)})
|
|
|
+; (global state {:x 100 :y 100 :dx 2 :dy 1 :left (- (/ h 2) 50) :right (- (/ h 2) 50)})
|
|
|
+; (var pause false)
|
|
|
+
|
|
|
+(local keys {:a [:left -1] :z [:left 1] :up [:right -1] :down [:right 1]})
|
|
|
+
|
|
|
+;; (fn on-paddle? []
|
|
|
+;; (or (and (< state.x 20)
|
|
|
+;; (< state.left state.y) (< state.y (+ state.left 100)))
|
|
|
+;; (and (< (- w 20) state.x)
|
|
|
+;; (< state.right state.y) (< state.y (+ state.right 100)))))
|
|
|
+
|
|
|
+(local Pong {})
|
|
|
+
|
|
|
+(fn Pong.new [cls]
|
|
|
+ (let [t {:x 100 :y 100
|
|
|
+ :dx 2 :dy 1
|
|
|
+ :left (- (/ h 2) 50) :right (- (/ h 2) 50)
|
|
|
+ :speed 1 :ball-speed 200
|
|
|
+ :pause false}]
|
|
|
+ (setmetatable t cls)))
|
|
|
+
|
|
|
+(fn Pong.on-paddle? [self]
|
|
|
+ (or (and (< self.x 20)
|
|
|
+ (< self.left self.y) (< self.y (+ self.left 100)))
|
|
|
+ (and (< (- w 20) self.x)
|
|
|
+ (< self.right self.y) (< self.y (+ self.right 100)))))
|
|
|
+
|
|
|
+(global pong nil)
|
|
|
+
|
|
|
+{
|
|
|
+ :name "pong"
|
|
|
+
|
|
|
+ :init (fn init []
|
|
|
+ (set pong (Pong:new)))
|
|
|
+
|
|
|
+ :update (fn update [dt set-mode]
|
|
|
+ (when (not pong.pause)
|
|
|
+ (set pong.x (+ pong.x (* pong.dx dt ball-speed)))
|
|
|
+ (set pong.y (+ pong.y (* pong.dy dt ball-speed)))
|
|
|
+ (each [key action (pairs keys)]
|
|
|
+ (let [[player dir] action]
|
|
|
+ (when (love.keyboard.isDown key)
|
|
|
+ (tset state player (+ (. state player) (* dir speed))))))
|
|
|
+
|
|
|
+ (when (or
|
|
|
+ (< pong.y 0)
|
|
|
+ (> pong.y h))
|
|
|
+ (set pong.dy (- 0 pong.dy)))
|
|
|
+
|
|
|
+ (when (pong.on-paddle?)
|
|
|
+ (set pong.dx (- 0 pong.dx)))
|
|
|
+
|
|
|
+ (when (< pong.x 0)
|
|
|
+ (set-mode "mode.end" "2"))
|
|
|
+ (when (> pong.x w)
|
|
|
+ (set-mode "mode.end" "1"))))
|
|
|
+
|
|
|
+ :keypressed (fn keypressed [key set-mode]
|
|
|
+ (when (= "p" key)
|
|
|
+ (set pong.pause (not pong.pause))))
|
|
|
+
|
|
|
+ :draw (fn draw [m]
|
|
|
+ (when (not pong.pause)
|
|
|
+ (love.graphics.rectangle "fill" 10 pong.left 10 100)
|
|
|
+ (love.graphics.rectangle "fill" (- w 20) pong.right 10 100)
|
|
|
+ (love.graphics.circle "fill" pong.x pong.y 10))
|
|
|
+ (when (pong.pause)
|
|
|
+ (love.graphics.printf "Press 'p' to unpause" 0 (/ h 3) w "center")))
|
|
|
+ }
|