├── .gitignore ├── Game.elm ├── Key.elm ├── LICENSE ├── README.md ├── elm.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /elm-stuff 3 | -------------------------------------------------------------------------------- /Game.elm: -------------------------------------------------------------------------------- 1 | module Game exposing (Model, Msg(..), applyPhysics, incrementShotsFired, init, initModel, keyDown, keyUp, main, subscriptions, update, updateVelocity, view) 2 | 3 | import Browser 4 | import Browser.Events as Events 5 | import Debug 6 | import Html exposing (Html, text) 7 | import Key exposing (..) 8 | import Json.Decode as Decode 9 | 10 | 11 | main : Program () Model Msg 12 | main = 13 | Browser.element 14 | { init = init 15 | , view = view 16 | , update = update 17 | , subscriptions = subscriptions 18 | } 19 | 20 | 21 | 22 | -- MODEL 23 | 24 | 25 | type alias Model = 26 | { velocity : Float 27 | , position : Float 28 | , shotsFired : Int 29 | } 30 | 31 | 32 | initModel : Model 33 | initModel = 34 | { velocity = 0 35 | , position = 0 36 | , shotsFired = 0 37 | } 38 | 39 | 40 | init : () -> ( Model, Cmd Msg ) 41 | init () = 42 | ( initModel, Cmd.none ) 43 | 44 | 45 | 46 | -- UPDATE 47 | 48 | 49 | type Msg 50 | = TimeUpdate Float 51 | | KeyDown Key 52 | | KeyUp Key 53 | 54 | 55 | update : Msg -> Model -> ( Model, Cmd Msg ) 56 | update msg model = 57 | case msg of 58 | TimeUpdate dt -> 59 | ( applyPhysics dt model, Cmd.none ) 60 | 61 | KeyDown key -> 62 | ( keyDown key model, Cmd.none ) 63 | 64 | KeyUp key -> 65 | ( keyUp key model, Cmd.none ) 66 | 67 | 68 | keyDown : Key -> Model -> Model 69 | keyDown key model = 70 | case key of 71 | Space -> 72 | incrementShotsFired model 73 | 74 | ArrowLeft -> 75 | updateVelocity -1.0 model 76 | 77 | ArrowRight -> 78 | updateVelocity 1.0 model 79 | 80 | _ -> 81 | model 82 | 83 | 84 | keyUp : Key -> Model -> Model 85 | keyUp key model = 86 | case key of 87 | ArrowLeft -> 88 | updateVelocity 0 model 89 | 90 | ArrowRight -> 91 | updateVelocity 0 model 92 | 93 | _ -> 94 | model 95 | 96 | 97 | applyPhysics : Float -> Model -> Model 98 | applyPhysics dt model = 99 | { model | position = model.position + model.velocity * dt } 100 | 101 | 102 | updateVelocity : Float -> Model -> Model 103 | updateVelocity newVelocity model = 104 | { model | velocity = newVelocity } 105 | 106 | 107 | incrementShotsFired : Model -> Model 108 | incrementShotsFired model = 109 | { model | shotsFired = model.shotsFired + 1 } 110 | 111 | 112 | 113 | -- VIEW 114 | 115 | 116 | view : Model -> Html msg 117 | view model = 118 | text (Debug.toString model) 119 | 120 | 121 | 122 | -- SUBSCRIPTIONS 123 | 124 | 125 | subscriptions : Model -> Sub Msg 126 | subscriptions model = 127 | Sub.batch 128 | [ Events.onAnimationFrameDelta TimeUpdate 129 | , Events.onKeyDown (Decode.map KeyDown keyDecoder) 130 | , Events.onKeyUp (Decode.map KeyUp keyDecoder) 131 | ] 132 | 133 | 134 | keyDecoder : Decode.Decoder Key 135 | keyDecoder = 136 | Decode.map fromCode (Decode.field "key" Decode.string) 137 | -------------------------------------------------------------------------------- /Key.elm: -------------------------------------------------------------------------------- 1 | module Key exposing (..) 2 | 3 | 4 | type Key 5 | = Space 6 | | ArrowLeft 7 | | ArrowRight 8 | | Unknown 9 | 10 | 11 | fromCode : String -> Key 12 | fromCode keyCode = 13 | case keyCode of 14 | " " -> 15 | Space 16 | 17 | "ArrowLeft" -> 18 | ArrowLeft 19 | 20 | "ArrowRight" -> 21 | ArrowRight 22 | 23 | _ -> 24 | Unknown 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Ossi Hanhinen 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of elm-game-base nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Elm game base 2 | 3 | Example base for a game in Elm 0.19. 4 | 5 | This codebase is made for [an article](http://ohanhi.github.io/base-for-game-elm-017.html). The main file here is [Game.elm](Game.elm). 6 | 7 | To get things up and running, you will need `npm` installed. Then clone this repository and navigate to the directory. The following commands should work for you even if you have a previous version of Elm installed globally: 8 | 9 | ```bash 10 | › npm install 11 | › npx elm reactor 12 | ``` 13 | 14 | Then you can point your browser to [http://localhost:8000/Game.elm](http://localhost:8000/Game.elm) and try it out (arrows left/right and spacebar to control). 15 | 16 | 17 | [![](https://img.shields.io/badge/Sponsored%20by-Chilicorn-brightgreen.svg)](http://chilicorn.org/) 18 | 19 | Licensed under [BSD (3-clause)](LICENSE). 20 | -------------------------------------------------------------------------------- /elm.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "application", 3 | "source-directories": [ 4 | "." 5 | ], 6 | "elm-version": "0.19.0", 7 | "dependencies": { 8 | "direct": { 9 | "elm/browser": "1.0.0", 10 | "elm/core": "1.0.0", 11 | "elm/html": "1.0.0", 12 | "elm/json": "1.0.0" 13 | }, 14 | "indirect": { 15 | "elm/time": "1.0.0", 16 | "elm/url": "1.0.0", 17 | "elm/virtual-dom": "1.0.0" 18 | } 19 | }, 20 | "test-dependencies": { 21 | "direct": {}, 22 | "indirect": {} 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "019-game", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "BSD3", 11 | "devDependencies": { 12 | "elm": "^0.19.0" 13 | } 14 | } 15 | --------------------------------------------------------------------------------