Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Added results.clj file. Now all level wins saves in user's home direc…

…tory in .snakejure file. Also menu jlist internal structure modified. Now it's date vector [level-name result] instead of string level-name and added CellRenderer to build JPanel using vector.
  • Loading branch information...
commit 5af5b73a051e5ac1160d1e66a19cff1248b48405 1 parent 7694f78
@nbeloglazov authored
@@ -11,11 +11,12 @@ To win game you must "lay" on every "noiser" (magenta rectangles) simultaneously
First you should choose level you want to play and press enter.
To start/pause game press space. You can change direction with arrow keys.
To return to main menu you can press escape.
+If you win at least 1 level .snakejure file will be created in your home directory for saving your results.
+Actually it's just with simple map in it in form {"level name 1" :win, "level name 2" :win}
## Installation
-You can see, that it's leiningen project, but I don't use leiningen it right know.
-I hope I'll start use it in future.
+You need to have leiningen to install all. Just run lein deps and neccessary libs will be downloaded.
## Structure
@@ -27,6 +28,7 @@ game_gui.clj - contains functions, which creates and runs concrete level.
menu_gui.clj - contains functions, which creates frame and displays menu, where user can choose some level.
It also creates panel by function from game_gui.clj and displays it.
level_loader.clj - contains functions for loading all levels from levels directory.
+restults.clj - few functions which read/write .snakejure file in home directory.
levels directory - directory with clj files, which represents levels.
Each level is a separate clj file with at least 1 public function - "level-description" with no arguments:
4 project.clj
@@ -1,6 +1,6 @@
(defproject snakejure "0.1"
:description "Snake game written in clojure"
- :dependencies [[org.clojure/clojure "1.2.0-master-SNAPSHOT"]
- [org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]
+ :dependencies [[org.clojure/clojure "1.2.0-RC1"]
+ [org.clojure/clojure-contrib "1.2.0-RC1"]
[log4j/log4j "1.2.16"]]
:dev-dependencies [[swank-clojure "1.2.0"]])
50 src/snakejure/menu_gui.clj
@@ -1,29 +1,49 @@
- (:import (javax.swing JFrame JList JOptionPane)
+ (:import (javax.swing JFrame JList JLabel JOptionPane JPanel ListCellRenderer)
(java.awt.event KeyAdapter KeyEvent)
- (java.awt Dimension)
+ (java.awt Dimension Color)
(org.apache.log4j Logger FileAppender SimpleLayout Level))
(:use [snakejure.level-loader :only (get-levels-map)]
- [ :only (create-game-panel)]))
+ [ :only (create-game-panel)]
+ [snakejure.results :only (update-results get-results-map)]))
-(def width 100)
-(def height 400)
+(def #^{:private true} width 300)
+(def #^{:private true} height 400)
(def *log-file* "log.txt")
(def *log-level* (Level/TRACE))
-(defn init-logging
- "Inits logging to log all level changes to *log-file*"
+(defn- init-logging
+ "Inits logging to log it to *log-file*"
(doto (Logger/getRootLogger)
(.addAppender (FileAppender. (SimpleLayout.) *log-file* false))
(.setLevel *log-level*)))
-(defn- create-poor-jlist []
+(defn- get-level-vectors
+ "Create vector of levels + according result"
+ []
+ (let [level-names (sort (keys (get-levels-map)))
+ results (get-results-map)]
+ (for [name level-names] [name (results name)])))
+(defn- create-list-renderer []
+ (proxy [ListCellRenderer] []
+ (getListCellRendererComponent [list [name result] index is-sel has-focus]
+ (doto (JPanel.)
+ (.setName name)
+ (.add (JLabel. name))
+ (.setBackground (if is-sel
+ (Color/LIGHT_GRAY)
+ (Color/WHITE)))))))
+(defn- create-poor-jlist
"Creates Jlist without any listeners."
- (let [level-names (apply sorted-set (keys (get-levels-map)))]
- (doto (JList. (to-array level-names))
- (.setSelectedIndex 0))))
+ []
+ (doto (JList. (to-array (get-level-vectors)))
+ (.setCellRenderer (create-list-renderer))
+ (.setSelectedIndex 0)))
(defn- switch-panel [frame panel]
(doto (.getContentPane frame)
@@ -39,7 +59,9 @@
(defn end-fn [frame name result]
(case result
- :win (JOptionPane/showMessageDialog frame (str "You win level " name))
+ :win (do
+ (JOptionPane/showMessageDialog frame (str "You win level " name))
+ (update-results name :win))
:lose (JOptionPane/showMessageDialog frame (str "You lose level " name))
(switch-panel frame (create-jlist frame)))
@@ -49,7 +71,7 @@
(proxy [KeyAdapter] []
(keyPressed [e]
(when (= (.getKeyCode e) (KeyEvent/VK_ENTER))
- (let [name (.getSelectedValue (.getSource e))
+ (let [name (first (.. e getSource getSelectedValue))
levels-map (get-levels-map)
game-panel (create-game-panel ((levels-map name))
(partial end-fn frame name))]
28 src/snakejure/results.clj
@@ -0,0 +1,28 @@
+(ns snakejure.results
+ (:import ( File)))
+(defn- get-results-file
+ "Returns file .snakejure, which will be looked for in home directory."
+ []
+ (File. (str (System/getProperty "user.home") "/.snakejure")))
+(defn get-results-map
+ "Return map with results. If ~/.snakejure file doesn't exist return empty map."
+ []
+ (let [file (get-results-file)]
+ (if (.exists file)
+ (load-file (.getAbsolutePath file))
+ {})))
+(defn- save-results-map
+ "Writes results to ~/.snakejure file."
+ [results]
+ (spit (get-results-file) results))
+(defn update-results
+ "Loads results, updated them and saves."
+ [level-name result]
+ (-> (get-results-map)
+ (assoc level-name result)
+ (save-results-map)))
Please sign in to comment.
Something went wrong with that request. Please try again.