-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPart3-0.elm
112 lines (59 loc) · 1.78 KB
/
Part3-0.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import Graphics.Element exposing (..)
import Time
import Window
import Color exposing (..)
import Graphics.Collage exposing (..)
import Graphics.Element exposing (..)
import Keyboard
import Text exposing (monospace, fromString)
import Time exposing (..)
import Window
import Signal exposing ((<~), (~), foldp, sampleOn)
import Debug exposing (log)
type H = H
--
-- First steps towards Game state
--
type alias Object a = { a | x:Float, y:Float, vx:Float, vy:Float }
type alias Player = Object {}
type alias Ball = Object {}
player : Float -> Player
player x = { x=x, y=0, vx=0, vy=0 }
defaultBall : Ball
defaultBall = { x=0, y=0, vx=200, vy=200 }
type alias Game = { player1:Player, player2:Player, ball: Ball }
defaultGame : Game
defaultGame =
{ player1 = player (20-halfWidth),
player2 = player (halfWidth-20),
ball = defaultBall
}
main = show defaultGame
--
-- Supporting functions and data structures from previous exercises
--
(gameWidth,gameHeight) = (600,400)
(halfWidth,halfHeight) = (300,200)
pongGreen = rgb 60 100 60
textGreen = rgb 160 200 160
type Direction = Up | Stopped | Down
type alias Input = {paddle1:Direction, paddle2:Direction, delta:Time }
-- <~ same as map
delta : Signal Time
delta = inSeconds <~ fps 35
dirToInt : Direction -> Int
dirToInt dir =
case dir of
Up -> 1
Stopped -> 0
Down -> -1
intToDir : Int -> Direction
intToDir x =
if | x == 1 -> Up
| x == (-1) -> Down
| otherwise -> Stopped
-- <~ signal ~ signal -- same as map2. Think of it as mapN
input : Signal Input
input = sampleOn delta <| Input <~ Signal.map (intToDir << .y) Keyboard.wasd
~ Signal.map (intToDir << .y) Keyboard.arrows
~ delta