Skip to content

Instantly share code, notes, and snippets.

Created April 23, 2014 20:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/11231536 to your computer and use it in GitHub Desktop.
(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
(atom
{
: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)
(cond
(true? (:pressed (:w @keys)))
(swap! r-left update-in [:y] dec)
(true? (:pressed (:s @keys)))
(swap! r-left update-in [:y] inc))
(cond
(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 []
(cond
(= (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 []
(cond
(= (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 "noobtuts.com 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