This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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