Created
June 26, 2014 12:10
-
-
Save lnicola/ab9f726c4fec7e2209c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Random | |
| import Graphics.Rendering.OpenGL | |
| import Data.IORef | |
| import Graphics.UI.GLUT | |
| lim_stanga = -10 :: Integer | |
| lim_dreapta = 9 :: Integer | |
| lim_sus = 9 :: Integer | |
| lim_jos = -9 :: Integer | |
| first f (x, y) = (f x, y) | |
| second f (x, y) = (x, f y) | |
| wrapL a b x = if x < a then b else x | |
| wrapR a b x = if x > b then a else x | |
| main = do | |
| getArgsAndInitialize | |
| initialDisplayMode $= [DoubleBuffered] | |
| createAWindow "Shoot" | |
| mainLoop | |
| createAWindow windowName = do | |
| window <- createWindow windowName | |
| windowSize $= Size 600 600 | |
| food <- newIORef (6, 6) | |
| sarpe <- newIORef [(2, 0), (1, 0), (0, 0)] | |
| displayCallback $= (display sarpe food) | |
| keyboardMouseCallback $= Just (keyboard window sarpe food) | |
| cursor $= None | |
| miscaSarpe sarpe food walk = do | |
| s <- get sarpe | |
| f <- get food | |
| let nou = walk $ head s | |
| if f == nou | |
| then do | |
| sarpe $~ (nou :) | |
| nx <- randomRIO(lim_stanga, lim_dreapta) | |
| ny <- randomRIO(lim_jos, lim_sus) | |
| food $= (nx, ny) | |
| else if (elem nou s) then return() else sarpe $~ ((nou :) . init) | |
| postRedisplay Nothing | |
| actions = [(KeyRight, first $ wrapR lim_stanga lim_dreapta . (1 +)), (KeyLeft, first $ wrapL lim_stanga lim_dreapta . subtract 1), (KeyUp, second $ wrapR lim_jos lim_sus . (1 +)), (KeyDown, second $ wrapL lim_jos lim_sus . subtract 1)] | |
| keyboard _ sarpe food (SpecialKey sk) Down _ _ = case lookup sk actions of | |
| Just action -> miscaSarpe sarpe food action | |
| Nothing -> return () | |
| keyboard window _ _ (Char '\ESC') _ _ _ = destroyWindow window | |
| keyboard _ _ _ _ _ _ _ = return () | |
| display sarpe food = do | |
| clear [ColorBuffer] | |
| currentColor $= Color4 1 1 0 1 | |
| get sarpe >>= renderPrimitive Quads . mapM drawSegment | |
| currentColor $= Color4 1 0 0 1 | |
| get food >>= renderPrimitive Quads . drawSegment | |
| swapBuffers | |
| drawSegment (px, py) = do | |
| let (x, y) = ((fromIntegral px) / 10, (fromIntegral py) / 10) | |
| vertex (Vertex3 x y (0 :: GLfloat)) | |
| vertex (Vertex3 (x + lat) y 0) | |
| vertex (Vertex3 (x + lat) (y + lat) 0) | |
| vertex (Vertex3 x (y + lat) 0) | |
| where lat = 0.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment