-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
67 lines (50 loc) · 1.51 KB
/
Main.hs
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
import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT
import Data.Char
import FGLUT
import GameStates
import SnakeGame
import qualified SnakeGame( SnakeOperation (U, D, L, R) )
import SnakeRender
import Randomize
import Utils
main = do
(progName, _) <- getArgsAndInitialize
newGS <- runRandIO newGSRand
initialWindowSize $= Size 480 480
startGlut ("Snake") (200) (newGS) (dealEvent)
dealEvent :: GS -> Event -> IO GS
dealEvent state EventDisplay = do
clear [ColorBuffer]
renderGame (state.>world)
windowTitle $= if (state.>world.>dead)
then "Dead"
else "Snake"
flush
return state
dealEvent state (EventTimer clk) = do
nextWorld <- runRandIO (nextSnakeState sw operation)
return state {
world = nextWorld
}
where
si = state.>input
sw = state.>world
operation = si.>lastOperation
dealEvent state (EventReshape (Size w h)) = do
viewport $= (Position 0 0, Size w h)
postRedisplay Nothing
return state
dealEvent state (EventKeyDown key position) =
if key' == 'o'
then runRandIO newGSRand
else return state { input = next (state.>input) key' }
where
key' = toLower key
next :: GSInput -> Char -> GSInput
next si 'w' = si { lastOperation = SnakeGame.U }
next si 'a' = si { lastOperation = SnakeGame.L }
next si 's' = si { lastOperation = SnakeGame.D }
next si 'd' = si { lastOperation = SnakeGame.R }
next si _ = si
dealEvent state _ = return state