Create a gist now

Instantly share code, notes, and snippets.

anonymous /gist:11231536
Created Apr 23, 2014

What would you like to do?
(ns noobtuts.core
(:import [java.awt.event KeyEvent])
(:use [quil.core]))
(def r-left (atom {:x 10 :y 65 :w 10 :h 70}))
(def r-right (atom {:x 430 :y 65 :w 10 :h 70}))
(def ball (atom {:x 225 :y 100 :w 10 :h 10}))
(def ball-dir (atom [1 0]))
(def keys
:w {:pressed false}
:s {:pressed false}
:up {:pressed false}
:down {:pressed false}}))
(defn draw-rect [r]
(rect (:x r) (:y r) (:w r) (:h r)))
(defn next-ball [b [dx dy]]
(assoc b :x (+ (:x b) dx)
:y (+ (:y b) dy)))
(defn update []
(swap! ball next-ball @ball-dir)
(println @keys)
(true? (:pressed (:w @keys)))
(swap! r-left update-in [:y] dec)
(true? (:pressed (:s @keys)))
(swap! r-left update-in [:y] inc))
(true? (:pressed (:up @keys)))
(swap! r-right update-in [:y] dec)
(true? (:pressed (:down @keys)))
(swap! r-right update-in [:y] inc)))
(defn draw []
(background-float 0x20)
(fill 0xff)
(draw-rect @r-left)
(draw-rect @r-right)
(draw-rect @ball))
(defn key-pressed []
(= (key-code) KeyEvent/VK_W)
(swap! keys assoc-in [:w :pressed] true)
(= (key-code) KeyEvent/VK_S)
(swap! keys assoc-in [:s :pressed] true)
(= (key-code) KeyEvent/VK_UP)
(swap! keys assoc-in [:up :pressed] true)
(= (key-code) KeyEvent/VK_DOWN)
(swap! keys assoc-in [:down :pressed] true)
(defn key-released []
(= (key-code) KeyEvent/VK_W)
(swap! keys assoc-in [:w :pressed] false)
(= (key-code) KeyEvent/VK_S)
(swap! keys assoc-in [:s :pressed] false)
(= (key-code) KeyEvent/VK_UP)
(swap! keys assoc-in [:up :pressed] false)
(= (key-code) KeyEvent/VK_DOWN)
(swap! keys assoc-in [:down :pressed] false)
(defsketch pong
:title " 2d pong game"
:size [450 200]
:setup (fn [] (smooth) (no-stroke) (frame-rate 60))
:draw (fn [] (update) (draw))
:key-pressed key-pressed
:key-released key-released)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment