Skip to content

Commit

Permalink
updated for Elm 0.14.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Balcerek committed Jan 17, 2015
1 parent a1b4e7f commit e8b7121
Show file tree
Hide file tree
Showing 73 changed files with 1,317 additions and 2,480 deletions.
42 changes: 32 additions & 10 deletions Chapter11KeyboardSignals.elm → Chapter10KeyboardSignals.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import Lib (..)
import Window
import Signal
import Markdown

main = lift (pageTemplate [content] "Chapter10Calculator" "toc" "Chapter12Paddle") Window.width
main = Signal.map (pageTemplate [content] "Chapter9Calculator" "toc" "Chapter11Paddle") Window.width

content = [markdown|
content = Markdown.toElement """
# Chapter 11 Keyboard Signals
# Chapter 10 Keyboard Signals
The `Keyboard` module defines functions for dealing with keyboard
input. Elm defines the `KeyCode` type as an alias for `Int` and
Expand Down Expand Up @@ -39,10 +41,15 @@ codes. [Try](KeyboardSignals1.html) the
action:
% KeyboardSignals1.elm
module KeyboardSignals1 where
import Keyboard
import Signal ((<~))
import Text (asText)
main = lift asText Keyboard.keysDown
main = asText <~ Keyboard.keysDown
The `lastPressed` function returns a signal which provides the code of
the last pressed key — even when the key is not currently being
Expand All @@ -51,10 +58,15 @@ pressed any more. Check it out using the
[here](KeyboardSignals2.html):
% KeyboardSignals2.elm
module KeyboardSignals2 where
import Keyboard
import Signal ((<~))
import Text (asText)
main = lift asText Keyboard.lastPressed
main = asText <~ Keyboard.lastPressed
The `isDown` function takes a key code as its argument and returns a
boolean signal indicating whether the given key is currently being
Expand All @@ -65,11 +77,16 @@ for certain special keys: `shift`, `ctrl`, `space` and
pressing the *A* key:
% KeyboardSignals3.elm
import Keyboard
module KeyboardSignals3 where
import Char
import Keyboard
import Signal ((<~))
import Text (asText)
main = lift asText (Keyboard.isDown (Char.toCode 'A'))
main = asText <~ (Keyboard.isDown (Char.toCode 'A'))
The `directions` function is useful for building games. It takes
four key codes as arguments and returns a signal of `{ x: Int, y: Int
Expand All @@ -88,15 +105,20 @@ member in a similar way. [Run](KeyboardSignals4.html) the
pressing the *Q*, *A*, *O* and *P* keys in various combinations:
% KeyboardSignals4.elm
module KeyboardSignals4 where
import Keyboard
import Signal ((<~))
import Text (asText)
main = lift asText (Keyboard.directions 81 65 79 80)
main = asText <~ (Keyboard.directions 81 65 79 80)
There are also two helper functions defined in terms of `directions`:
`wasd` and `arrows`.
The [next](Chapter12Paddle.html) chapter presents a game which uses
The [next](Chapter11Paddle.html) chapter presents a game which uses
keyboard as input.
|]
"""
35 changes: 21 additions & 14 deletions Chapter12Paddle.elm → Chapter11Paddle.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import Lib (..)
import Window
import Signal
import Markdown

content w = pageTemplate [content1]
"Chapter11KeyboardSignals" "toc" "Chapter13TicTacToe" w
main = lift content Window.width
"Chapter10KeyboardSignals" "toc" "Chapter12TicTacToe" w
main = Signal.map content Window.width

content1 = [markdown|
content1 = Markdown.toElement """
# Chapter 12 Paddle
# Chapter 11 Paddle
Now that we know keyboard signals, we will use them to create a game.
The *[Paddle.elm](Paddle.elm)* program is a game. The player uses the
Expand All @@ -23,9 +25,14 @@ The code starts with the `Paddle` module declaration and a list of imports.
module Paddle where
import Window
import Text as T
import Color (blue, green, orange, red, white)
import Graphics.Collage (Form, circle, collage, filled, group, move, moveY, rect)
import Graphics.Element (Element, container, empty, layers, middle)
import Keyboard
import Signal ((<~), Signal, foldp, merge)
import Text as T
import Time (Time, fps)
import Window
After the imports, four functions which draw various elements of the
game view are defined. The `borders` function draws the blue wall by
Expand Down Expand Up @@ -63,7 +70,7 @@ to the user when the game is over.
gameOver : Element
gameOver =
T.toText "Game Over"
T.fromString "Game Over"
|> T.color red
|> T.bold
|> T.height 60
Expand All @@ -73,7 +80,7 @@ to the user when the game is over.
The `State` type represents the state of the game.
% Paddle.elm
type State =
type alias State =
{
x: Float,
y: Float,
Expand Down Expand Up @@ -128,7 +135,7 @@ a data type representing events from both signals. The `Event` data
type represents the events.
% Paddle.elm
data Event = Tick Time | PaddleDx Int
type Event = Tick Time | PaddleDx Int
The `Tick` constructor takes a `Time` value as parameter and creates
an event representing a time-based tick. The `PaddleDx` constructor
Expand All @@ -140,7 +147,7 @@ standard library `fps` function.
% Paddle.elm
clockSignal : Signal Event
clockSignal = lift Tick <| fps 100
clockSignal = Tick <~ fps 100
The `keyboardSignal` function uses the `Keyboard.arrows` signal to
create a signal of keyboard-based events. Only the `x` members from
Expand All @@ -151,7 +158,7 @@ events: `-1`, `0`, or `1`.
% Paddle.elm
keyboardSignal : Signal Event
keyboardSignal = lift (.x >> PaddleDx) Keyboard.arrows
keyboardSignal = (.x >> PaddleDx) <~ Keyboard.arrows
The `eventSignal` merges both signal into one combined signal.
% Paddle.elm
Expand Down Expand Up @@ -232,15 +239,15 @@ producing the final game signal that is rendered on the screen.
% Paddle.elm
main : Signal Element
main = lift view gameSignal
main = view <~ gameSignal
In order to transform the game state, we have used a monolitic `step`
function, that reacts to each possible combination of input event and
current state. The solution works, but it has the disadvantage that
the function which transforms the state may become big and difficult
to maintain for larger programs. We will explore alternatives to that
approach in the subsequent chapters. The
[next](Chapter13TicTacToe.html) chapter presents a program which uses
[next](Chapter12TicTacToe.html) chapter presents a program which uses
an alternative approach.
|]
"""
Loading

0 comments on commit e8b7121

Please sign in to comment.