Hi everyone
first of all, this is my first post, so if I ask FAQs or deserve to
RTFM, please tell me.
I am learning clojure, and trying to adapt some game code written in a
Java framework called Slick. After a couple of beginner's mistake (the
kind you do to learn ;) ), I got a working convestion of a Scoller
example. Still, there are some things that I don't find very elegant,
or where I'm pretty sure to be reinventing some wheel.
Code is here :
http://tinyurl.com/dj4l9z
or
git://
github.com/phtrivier/clj-slick-tank.git
* The game is tile-based. I need to build up a collision map ("can the
tank walk on cell [3,2] ?"). Java does it with a two-dimensional array
of booleans. From discussion here :
http://groups.google.com/group/clojure/browse_thread/thread/5eb78c620eac19ea/b4d75ac4644c06a9?lnk=gst&q=two+dimensional+vector#b4d75ac4644c06a9
, I choosed to use a vector of vector (i probably should'nt have, but
that's another point). I wrote this :
(defn blocked?
"Is a position blocked in the screen?"
[screen x y]
(let [i (int x)
j (int y)]
(true? (get (get (screen :blocked) i) j))))
(defn make-row-generator
[cell-generator w]
(fn [i]
(vec (map (fn [j] (cell-generator i j))
(range 0 w)))))
(defn make-matrix
[w h cell-generator]
(vec (map (make-row-generator cell-generator w)
(range 0 h))))
(defn make-collision-map
"Builds a double dimensioned array telling
whether a cell is blocked"
[m w h]
(make-matrix
w h
(fn [i j]
(let [tileId (.getTileId m i j 0)]
(let [res
(Boolean/parseBoolean
(.getTileProperty m tileId "blocked" "false"))]
res
)))))
(the 'm' is a slick-specific stucture that holds the tile map).
Am I duplicating some existing library to build matrices ? Is there a
cleaner way to do it ?
* In a method I check for collisions :
;; Movement
(defn try-move
"Try and move a player in the screen given a direction.
Returns a list with the moved player, and a
boolean indicating whether the move was successfull.
"
[player screen dx dy]
(let [new_x (+ (player :x) dx)
new_y (+ (player :y) dy)]
(let [bxy (blocked? screen new_x new_y)
bx (blocked? screen new_x (player :y))
by (blocked? screen (player :x) new_y)]
(if bxy
(if bx
(if by
[player, false]
[(assoc player :y new_y), true])
[(assoc player :x new_x), true])
[(assoc player :x new_x :y new_y), true]))))
Will i get used to the nested 'if' blocks someday ? Any way to write
the equivalent of a switch case here, if only to improve
readibility ?
* Finally, my code starts with a rather ugly
(ns tank
(:import (org.newdawn.slick Animation
AppGameContainer
BasicGame
GameContainer
Graphics
Input
SlickException
SpriteSheet)
(org.newdawn.slick.tiled TiledMap)
(org.newdawn.slick.util Log)))
I know there is no way to import org.newdawn.slick.* (as discussed
here :
http://groups.google.com/group/clojure/browse_thread/thread/fa00a0ff4c264f9a/bcde2b6a4742fe6b?lnk=gst&q=import+all+classes+from+package#bcde2b6a4742fe6b
). What do you do in programs that need huge list of imports ? I'm
kinda spoiled by the Eclipse way of doing this, which is roughly :
import everything, forget about it, and let you IDE clear up the list.
And at least for development part it make things easier. If I hadn't
had the exact list of import to copy paste in this case, I would
probably have gone bored and depressed by the second class to import
manually (lazyness is not only for evaluation ;) )
Thanks for any ideas, sorry if post is too long / newbie-like.
Cheers
PH