1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- (local (w h) (love.window.getMode))
- (local keys {:a [:left -1] :z [:left 1] :up [:right -1] :down [:right 1]})
- (global Pong {})
- (set Pong.__index Pong)
- (fn Pong.init [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)))))
- (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
|