-
-
Save myuon/3726663 to your computer and use it in GitHub Desktop.
Rotate a square in Gloss (with key event (DOWN-key))
This file contains 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
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 | |
import Graphics.Gloss.Interface.Pure.Game | |
data State | |
= State { angle :: Float | |
, isPositive :: Bool | |
, picture :: Picture } | |
diffAngle :: Float | |
diffAngle = 14.4 | |
main :: IO () | |
main | |
= play (InWindow "Guriguri" (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