|
@@ -1,21 +1,11 @@
|
|
|
-; (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)))))
|
|
|
-
|
|
|
(global Pong {})
|
|
|
(set Pong.__index Pong)
|
|
|
|
|
|
-(fn Pong.new [cls]
|
|
|
+(fn Pong.init [cls]
|
|
|
(let [t {:x 100 :y 100
|
|
|
:dx 2 :dy 1
|
|
|
:left (- (/ h 2) 50) :right (- (/ h 2) 50)
|
|
@@ -29,45 +19,38 @@
|
|
|
(and (< (- w 20) self.x)
|
|
|
(< self.right self.y) (< self.y (+ self.right 100)))))
|
|
|
|
|
|
-(global pong nil)
|
|
|
-
|
|
|
-{
|
|
|
- :name "pong"
|
|
|
-
|
|
|
- :init (fn init []
|
|
|
- (global pong (: Pong :new)))
|
|
|
-
|
|
|
- :update (fn update [dt set-mode]
|
|
|
- (when (not pong.pause)
|
|
|
- (set pong.x (+ pong.x (* pong.dx dt pong.ball-speed)))
|
|
|
- (set pong.y (+ pong.y (* pong.dy dt pong.ball-speed)))
|
|
|
- (each [key action (pairs keys)]
|
|
|
- (let [[player dir] action]
|
|
|
- (when (love.keyboard.isDown key)
|
|
|
- (tset pong player (+ (. pong player) (* dir pong.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")))
|
|
|
- }
|
|
|
+(fn Pong.update [self dt set-mode]
|
|
|
+ (when (not self.pause)
|
|
|
+ (set self.x (+ self.x (* self.dx dt self.ball-speed)))
|
|
|
+ (set self.y (+ self.y (* self.dy dt self.ball-speed)))
|
|
|
+ (each [key action (pairs keys)]
|
|
|
+ (let [[player dir] action]
|
|
|
+ (when (love.keyboard.isDown key)
|
|
|
+ (tset self player (+ (. self player) (* dir self.speed))))))
|
|
|
+
|
|
|
+ (when (or
|
|
|
+ (< self.y 0)
|
|
|
+ (> self.y h))
|
|
|
+ (set self.dy (- 0 self.dy)))
|
|
|
+
|
|
|
+ (when (: self :on-paddle?)
|
|
|
+ (set self.dx (- 0 self.dx)))
|
|
|
+
|
|
|
+ (when (< self.x 0)
|
|
|
+ (set-mode "mode.end" "2"))
|
|
|
+ (when (> self.x w)
|
|
|
+ (set-mode "mode.end" "1"))))
|
|
|
+
|
|
|
+(fn Pong.keypressed [self key set-mode]
|
|
|
+ (when (= "p" key)
|
|
|
+ (set self.pause (not self.pause))))
|
|
|
+
|
|
|
+(fn Pong.draw [self m]
|
|
|
+ (when (not self.pause)
|
|
|
+ (love.graphics.rectangle "fill" 10 self.left 10 100)
|
|
|
+ (love.graphics.rectangle "fill" (- w 20) self.right 10 100)
|
|
|
+ (love.graphics.circle "fill" self.x self.y 10))
|
|
|
+ (when self.pause
|
|
|
+ (love.graphics.printf "Press 'p' to unpause" 0 (/ h 3) w "center")))
|
|
|
+
|
|
|
+Pong
|