Skip to content

Instantly share code, notes, and snippets.

@ikedaisuke
Created April 13, 2011 03:52
Show Gist options
  • Save ikedaisuke/916931 to your computer and use it in GitHub Desktop.
Save ikedaisuke/916931 to your computer and use it in GitHub Desktop.
Rotate a square in Gloss (with key event (DOWN-key))
module Main where
-- http://www.f13g.com/%A5%D7%A5%ED%A5%B0%A5%E9%A5%DF%A5%F3%A5%B0/Haskell/GLUT/#content_1_4
import Graphics.Gloss.Interface.Game
data State
= State { angle :: Float
, isPositive :: Bool
, picture :: Picture }
diffAngle :: Float
diffAngle = 14.4
main :: IO ()
main
= gameInWindow
"GuruGuru"
(200, 200)
(10,10)
black
10
initialState
draw
handleInput
(\_ s -> s { angle = changeAngle s
, picture = draw s } )
changeAngle :: State -> Float
changeAngle (State t x _)
| x = t + diffAngle
| otherwise = t - diffAngle
initialState :: State
initialState
= State { angle = 0
, isPositive = True
, picture
= Color white
$ Scale 120 120
$ Polygon [ ( 0.10, 0.10)
, (-0.10, 0.10)
, (-0.10, -0.10)
, ( 0.10, -0.10)
] }
draw :: State -> Picture
draw (State t _ _)
= rotate t (picture initialState)
handleInput :: Event -> State -> State
handleInput (EventKey k ks _ _) s
| SpecialKey KeyDown <- k
, Down <- ks
= s { isPositive = not (isPositive s) }
| otherwise = s
handleInput (EventMotion _) s = s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment