Created
April 20, 2014 20:42
-
-
Save jrm2k6/11124782 to your computer and use it in GitHub Desktop.
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
import Keyboard | |
import Window | |
(gameWidth,gameHeight) = (600,800) | |
(halfWidth,halfHeight) = (300,400) | |
type Input = {space:Bool, dir:Int, delta:Time} | |
type Spaceship = {x:Float, y:Float, rotation:Float} | |
type Ball = {x:Float, y:Float, vx:Float, vy:Float} | |
type Game = {spaceship:Spaceship, ball:Ball, state:State} | |
defaultGame : Game | |
defaultGame = | |
{ spaceship = {x=0, y=-halfHeight+40, rotation=90}, | |
ball = {x=0, y=-halfHeight+40, vx=100, vy=100}, | |
state = NotShooting | |
} | |
data State = Shooting | NotShooting | |
defaultSpaceship : Float -> Spaceship | |
defaultSpaceship h = {x=0, y=h + 40, rotation=90} | |
delta = inSeconds <~ fps 35 | |
input = sampleOn delta (Input <~ Keyboard.space | |
~ lift .x Keyboard.arrows | |
~ delta) | |
drawSpaceship : Spaceship -> Color -> Form | |
drawSpaceship spaceship clr = (rotate (degrees spaceship.rotation) ( move (spaceship.x, spaceship.y) (filled clr (ngon 3 20)))) | |
drawBall : Ball -> Color -> Form | |
drawBall ball clr = ( move (ball.x, ball.y) (filled clr (circle 8))) | |
stepGame : Input -> Game -> Game | |
stepGame ({space, dir, delta} as input) ({spaceship, ball, state} as game) = | |
let spaceship' = moveSpaceship spaceship delta dir | |
ball' = moveBall ball delta state | |
state' = updateState state ball space | |
in { game | spaceship <- spaceship', | |
ball <- ball', | |
state <- state'} | |
updateState : State -> Ball -> Bool -> State | |
updateState state ball isSpacePressed = case (state, ball, isSpacePressed) of | |
(NotShooting, _, True) -> Shooting | |
(s, b, _) -> if b.y < halfHeight then s else NotShooting | |
moveBall : Ball -> Time -> State -> Ball | |
moveBall ({x,y,vx,vy} as ball) delta state = let y' = if state == Shooting && y < halfHeight then y + vx * delta | |
else -halfHeight+40 | |
in {ball | y <- y'} | |
moveSpaceship : Spaceship -> Time -> Int -> Spaceship | |
moveSpaceship spaceship delta angle = let rotation' = spaceship.rotation - ((toFloat angle)) | |
in {spaceship | rotation <- rotation'} | |
gameState : Signal Game | |
gameState = foldp stepGame defaultGame input | |
showRotation : Spaceship -> String | |
showRotation {x, y, rotation} = "Rotation " ++ show rotation | |
display : (Int, Int) -> Game -> Input -> Element | |
display (w,h) {spaceship, ball, state} i = collage w h [ | |
move (0, 0) (filled yellow (rect gameWidth gameHeight)), | |
move (0, 0) (filled green (rect 5 5)), | |
move (0, 0) (toForm (asText (showRotation spaceship))), | |
move (0, -20) (toForm (asText i.dir)), | |
(drawSpaceship spaceship red), | |
(drawBall ball blue)] | |
main = lift3 display Window.dimensions gameState input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment