Advertisement
triclops200

Pong

Nov 25th, 2012
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns pong.core
  2.   (:gen-class))
  3. (import '(javax.swing JFrame))
  4. (import '(java.awt Color Toolkit Image Font GraphicsEnvironment Graphics2D))
  5. (import '(java.awt.image BufferedImage))
  6. (import '(java.awt.event KeyListener WindowAdapter KeyEvent))
  7. (def width  (atom 600))
  8. (def height (atom 300))
  9. (defn get-time [& args] (System/currentTimeMillis))
  10.  
  11. (defn elapsed-time [t]
  12.   (- (get-time) t))
  13.  
  14. (defn sleep [n]
  15.   (Thread/sleep n)
  16.   )
  17. (defstruct paddle :y :vy :x)
  18. (def pdl1 (atom (struct paddle (rand-int 300) 0 20)))
  19. (def pdl2 (atom (struct paddle (rand-int 300) 0 570)))
  20. (def keys-pressed (atom {}))
  21.  
  22. (defstruct ball :h :w :x :y :vx :vy)
  23. (def score1 (atom 0))
  24. (def score2 (atom 0))
  25. (defn createball [w h x y vx vy] (struct ball w h x y vx vy))
  26. (def mainball (atom (createball 5 5 300 150 (- (rand-int 5) 2) (- (rand-int 5) 2))))
  27. (defn updateball [b] (swap! b assoc :x (+ (@b :vx) (@b :x)) :y (+ (@b :y) (@b :vy))))
  28. (defn updateAI [pdle]
  29.   (if (< (+ (@pdle :y) 15) (@mainball :y))
  30.     (swap! pdle assoc :vy (+ (@pdle :vy) 0.3))
  31.     0)
  32.   (if (> (+ (@pdle :y) 15) (@mainball :y))
  33.     (swap! pdle assoc :vy (- (@pdle :vy) 0.3))
  34.     0)
  35.   )
  36. (defn updatePC [pdle]
  37.   (if (contains? @keys-pressed (KeyEvent/VK_DOWN))
  38.     (swap! pdle assoc :vy (+ (@pdle :vy) 0.3))
  39.     0)
  40.   (if (contains? @keys-pressed (KeyEvent/VK_UP))
  41.     (swap! pdle assoc :vy (- (@pdle :vy) 0.3))
  42.     0)
  43.   )
  44. (defn updatepaddle [pdl]
  45.   (swap! pdl assoc :y (+ (@pdl :vy) (@pdl :y)))
  46.   (if (> (@pdl :vy) 2)
  47.     (swap! pdl assoc :vy 2)
  48.     0)
  49.   (if (< (@pdl :vy) -2)
  50.     (swap! pdl assoc :vy -2)
  51.     0)
  52.   (if (> (+ (@pdl :y) 30) @height)
  53.     (do
  54.       (swap! pdl assoc :y (- @height 30))
  55.       (swap! pdl assoc :vy (* (@pdl :vy) -1))
  56.       )
  57.     0
  58.     )
  59.   (if (< (@pdl :y) 0)
  60.     (do
  61.       (swap! pdl assoc :y 0)
  62.       (swap! pdl assoc :vy (* (@pdl :vy) -1))
  63.       )
  64.     0
  65.     )
  66.   (swap! pdl assoc :vy (* (@pdl :vy) 0.95))
  67.   )
  68. (defn colliding? [b p]
  69.   (let [bx (@b  :x)
  70.         by (@b  :y)
  71.         bw (@b  :w)
  72.         bh (@b  :h)
  73.         px (+ (@p  :x) (* (/ 10 2) 1.0))
  74.         py (+ (@p  :y) (* (/ 30 2) 1.0))
  75.         hpw (* (/ 10 2) 1.0)
  76.         hph (* (/ 30 2) 1.0)
  77.         ]
  78.     (and
  79.       (or
  80.         (and (>= (+ bx bw) (- px hpw))  (<= (+ bx bw) (+ px hpw)))
  81.         (and (>=  bx (- px hpw))  (<= bx (+ px hpw)))
  82.         )
  83.       (or
  84.         (and (>= (+ by bh) (- py hph))  (<= (+ by bh) (+ py hph)))
  85.         (and (>=  by (- py hph))  (<= by (+ py hph)))
  86.         )
  87.       )
  88.     )
  89.   )
  90. (defn gmover-msg [x]
  91.   (if (= @score1 3)
  92.       "YOU LOSE!"
  93.       "YOU WIN!"))
  94. (declare disp)
  95. (def center-msg (atom ""))
  96. (defn update [frame bbuf]
  97.   (if (= (@mainball :vx) 0)
  98.     (swap! mainball assoc :vx 1)
  99.     0)
  100.   (if (= (@mainball :vy) 0)
  101.     (swap! mainball assoc :vy 1)
  102.     0)
  103.   (updateball mainball)
  104.   (updateAI pdl1)
  105.   (updatePC pdl2)
  106.   (updatepaddle pdl1)
  107.   (updatepaddle pdl2)
  108.   (if (>= (+ (@mainball :x) (@mainball :w)) @width)
  109.     (do
  110.       (def mainball (atom (createball 5 5 300 150 (- (rand-int 5) 2) (- (rand-int 5) 2 ))))
  111.       (swap! score1 inc)
  112.       )
  113.     0)
  114.   (if (<= (@mainball :x) 0)
  115.     (do
  116.       (def mainball (atom (createball 5 5 300 150 (- (rand-int 5) 2) (- (rand-int 5) 2 ))))
  117.       (swap! score2 inc)
  118.       )
  119.     0)
  120.   (if (>= (+ (@mainball :y) (@mainball :h)) @height)
  121.     (swap! mainball assoc :vy (* (@mainball :vy) -1))
  122.     0)
  123.   (if (<= (- (@mainball :y) 0) 0)
  124.     (swap! mainball assoc :vy (* (@mainball :vy) -1))
  125.     0)
  126.   (if (colliding? mainball pdl1)
  127.     (do
  128.       (swap! mainball assoc :vx (* (@mainball :vx) -1.2) :vy (* (@mainball :vy) 1.2))
  129.       (swap! mainball assoc :vy (+ (@mainball :vy) (* (@pdl1 :vy) 0.2)))
  130.       )
  131.     0)
  132.   (if (colliding? mainball pdl2)
  133.     (do
  134.       (swap! mainball assoc :vx (* (@mainball :vx) -1.2) :vy (* (@mainball :vy) 1.2))
  135.       (swap! mainball assoc :vy (+ (@mainball :vy) (* (@pdl2 :vy) 0.2)))
  136.       )
  137.     0)
  138.   (if (or (= @score1 3) (= @score2 3) (contains? @keys-pressed (KeyEvent/VK_ESCAPE)))
  139.     (do
  140.     (swap! center-msg gmover-msg)
  141.       (disp frame bbuf)
  142.       (println "TESTING")
  143.       (Thread/sleep 3000)
  144.       (System/exit 0))
  145.     0)
  146.   )
  147.  
  148.  
  149. (def running (atom true))
  150.  
  151.  
  152.  
  153. (def exit-action (proxy [WindowAdapter] []
  154.                    (windowClosing [e] (System/exit 0))))
  155. (defn keypress [ch]
  156.   (swap! keys-pressed assoc ch true))
  157. (defn keyrelease [ch]
  158.   (swap! keys-pressed dissoc ch ))
  159. (def key-action (proxy [KeyListener] []
  160.                   (keyPressed [e]  (keypress (.getKeyCode e)))
  161.                   (keyReleased [e] (keyrelease (.getKeyCode e)))
  162.                   (keyTyped [e] 0)
  163.                   ))
  164. (defn make-frame [width height title]
  165.   (JFrame/setDefaultLookAndFeelDecorated true)
  166.   (let [f (JFrame. title)]
  167.     (.setBounds f width height width height)
  168.     (.setResizable f false)
  169.     (.setFocusable f true)
  170.     (.setVisible f true)
  171.     (.addWindowListener f exit-action)
  172.     (.addKeyListener  f key-action)
  173.     f))
  174. (defn disp  [frame bbuf]  
  175.   (let [g (.getGraphics bbuf) gg (.getGraphics frame)]
  176.     (.setColor g (Color/BLACK))
  177.     (.fillRect g 0 0 @width @height)
  178.     (.setColor g (Color/WHITE))
  179.     (.fillRect g (@mainball :x) (@mainball :y) (@mainball :w) (@mainball :h))
  180.     (.fillRect g (@pdl1 :x) (@pdl1 :y) 10 30)
  181.     (.fillRect g (@pdl2 :x) (@pdl2 :y) 10 30)
  182.     (.drawString g (str "SCORE: " @score1) 20 20)
  183.     (.drawString g (str "SCORE: " @score2) 530 20)
  184.     (.drawString g @center-msg  260 140)
  185.     (.drawImage gg bbuf 0 0 frame)
  186.     ))
  187. (def pressed-keys (atom {}))
  188. (defn on-keypress [ch]
  189.   (assoc pressed-keys (keyword (str ch)) true))
  190. (defn on-keyrelease [ch]
  191.   (assoc pressed-keys (keyword (str ch)) nil))
  192. (defn run [frame]
  193.   (let [pt (atom (get-time))
  194.         bbuf (BufferedImage. @width @height BufferedImage/TYPE_INT_RGB)
  195.         ]
  196.     (loop []
  197.       (do
  198.         (if (> (elapsed-time @pt) 15)
  199.           (loop [ct (elapsed-time @pt)]
  200.             (if (> ct 15)
  201.               (do (update frame bbuf) (recur (- ct 15)))
  202.               (swap! pt get-time)))
  203.           0)
  204.         (disp frame bbuf)
  205.         (if (= @running true)
  206.           (recur)
  207.           (System/exit 0)
  208.           )
  209.         )
  210.       )
  211.     )
  212.   )
  213.  
  214.  
  215. (def st (atom (get-time)))
  216. (defn -main [& args]  (run (make-frame @width @height "Pong") ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement