Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/theronic/cljs-snake
Snake in 100 lines of ClojureScript using Reagent for a #TechTalk at UCT
https://github.com/theronic/cljs-snake
clojure clojurescript snake
Last synced: 14 days ago
JSON representation
Snake in 100 lines of ClojureScript using Reagent for a #TechTalk at UCT
- Host: GitHub
- URL: https://github.com/theronic/cljs-snake
- Owner: theronic
- Created: 2015-08-12T10:29:40.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-05-15T11:00:37.000Z (over 1 year ago)
- Last Synced: 2024-10-04T10:47:52.483Z (about 1 month ago)
- Topics: clojure, clojurescript, snake
- Language: Clojure
- Homepage: http://petrustheron.com/posts/sota-front-end-development-clojurescript.html
- Size: 270 KB
- Stars: 21
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Snake in 100 Lines of ClojureScript
![Snake Gameplay GIF](https://s3-eu-west-1.amazonaws.com/petrus-blog/snake-gameplay-3.gif)
On 12 August 2015, I presented a talk at UCT on [The State of the Art in Front End Development](http://petrustheron.com/posts/sota-front-end-development-clojurescript.html), during which we built a game in ClojureScript.
You can download the slides with coding videos and audio by [reading the blog post](http://petrustheron.com/posts/sota-front-end-development-clojurescript.html).
## Tools:
- ClojureScript 1.7
- Boot-clj## To Run:
```
boot dev
```When it's done compiling, open `http://localhost:8002/`
## To Build for Production:
```
boot prod
```This will write out artifacts to the `docs` folder.
## Features:
You can:
- Turn left/right with the arrow keys
- Take the red pill
- Die by running into your tail
- Get a high score## Turning
When turning, the velocity vector is rotated by 90 degrees clockwise or counter-clockwise in the `next-state` function using a hard-coded [rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix).
## Data Model
The simulation state is stored in an atom named `state` with these keys:
- `:size` stores the size of the body of the snake, initially 3.
- `:position` stores the snake's head as an `[x y]` coordinate, e.g. `[7 9]`
- `:history` stores a list of previous head positions, so we can render the snake's body as the last N head positions, e.g. `([7 9] [6 9] [6 8] ...)`.
- `:velocity` holds a relative `[x y]` vector that is added to the `:position` coordinate on every animation tick.
- `:dead?` will be true when the snake die by running into its own body.
- `:pills` stores a set of edible `[x y]` pill coordinates.