Skip to content

Commit

Permalink
Implemented colors and beginning of block implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Ly committed Nov 12, 2012
1 parent 3888353 commit eb55607
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
37 changes: 29 additions & 8 deletions src/breakout/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,48 @@
(.setColor Color/YELLOW)
(.fillOval x y diameter diameter))))

(defn draw-paddle [paddle #^Graphics g]
(let [origin (:origin paddle)
(defn get-color [color]
(case color
:green Color/GREEN
:red Color/RED
:magenta Color/MAGENTA
:yellow Color/YELLOW
:black Color/BLACK
:orange Color/ORANGE
:blue Color/BLUE))

(defn draw-rect [rect #^Graphics g]
(let [origin (:origin rect)
x (:x origin)
y (:y origin)
size (:size paddle)
size (:size rect)
width (:width size)
height (:height size)]
height (:height size)
color (:color rect)]
(doto g
(.setColor Color/GREEN)
(.setColor (get-color color))
(.fillRect x y width height))))

(defn draw-paddle [paddle #^Graphics g]
(draw-rect paddle g))

(defn draw-block [block #^Graphics g]
(draw-rect block g))

(defn draw-game [blocks paddle ball score]
(fn [#^Graphics g]
(doto g
(.setColor Color/RED)
(.fillRect 0 0 600 400))


(map #(draw-block % g) blocks)
(draw-paddle paddle g)
(draw-ball ball g)))

(defn -main [& args]
(let [frame (JFrame. "Breakout")
canvas (Canvas.)
blocks (default-blocks)
ball (default-ball)
paddle (default-paddle)]

Expand All @@ -79,17 +98,18 @@

(loop [score 0
old-time (System/currentTimeMillis)
blocks blocks
paddle paddle
ball ball]
(reset! paddle-offset [0 0])
(Thread/sleep 10)

(.setTitle frame (str "Breakout: " score))
(let [blocks (atom nil)
current-time (System/currentTimeMillis)
(let [current-time (System/currentTimeMillis)
new-time (long (if (> (- current-time old-time) 250)
current-time
old-time))
updated-blocks (update-blocks blocks ball)
updated-paddle (update-paddle paddle paddle-offset)
updated-ball (update-ball ball paddle)]

Expand All @@ -99,5 +119,6 @@
(recur
(+ score 1)
new-time
updated-blocks
updated-paddle
updated-ball)))))
16 changes: 15 additions & 1 deletion src/breakout/game.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@
;; Ball settings
(def ball-speed 5)

(def block-size (atom {:width 50 :height 20}))

(defn default-ball []
{:origin {:x 300 :y 250} :radius 5 :velocity {:x ball-speed :y (- ball-speed)} })
(defn default-paddle []
{:origin {:x 300 :y 300} :size {:width 50 :height 10}})
{:origin {:x 300 :y 300} :size {:width 50 :height 10} :color :green})

(defn default-blocks []
(let [{width :width, height :height, :as size} @block-size
colors [:yellow :green :blue :magenta :red :orange]]
(for [x (range 1 5)
y (range 1 5)]
{:orientation {:x (* x width) :y (* y height)}
:size size
:color (nth colors (mod x (count colors)))})))

;; Updates the location of the paddle
(defn update-paddle [paddle offset]
Expand Down Expand Up @@ -138,6 +149,9 @@
:origin
new-origin)))

(defn update-blocks [blocks ball]
blocks)

;; Updates the ball velocity and location
(defn update-ball [ball paddle]
(cond
Expand Down

0 comments on commit eb55607

Please sign in to comment.