Skip to content

Instantly share code, notes, and snippets.

@takoeight0821
Created January 28, 2022 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takoeight0821/7fc27a140d9303cd39014d9cc0a42787 to your computer and use it in GitHub Desktop.
Save takoeight0821/7fc27a140d9303cd39014d9cc0a42787 to your computer and use it in GitHub Desktop.
Simple interactive animation. I created it to debug the system created in the assignment.
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"evancz/elm-playground": "1.0.3"
},
"indirect": {
"elm/json": "1.1.3",
"elm/svg": "1.0.1",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
module Main exposing (..)
import Playground exposing (..)
cupWidth : number
cupWidth =
100
cupHeight : number
cupHeight =
180
redCup : Shape
redCup =
rectangle red cupWidth cupHeight
blueCup : Shape
blueCup =
rectangle blue cupWidth cupHeight
greenBall : Shape
greenBall =
circle green 50
main =
game view update init
type alias Memory =
{ status : Status
, redCupPos : ( Number, Number )
, blueCupPos : ( Number, Number )
, answer : Answer
}
type Status
= Opened
| Switch Int
| Closing Int
| Shuffle Int
| Closed
| Opening Int
type Answer
= Red
| Blue
swap : Answer -> Answer
swap answer =
case answer of
Red ->
Blue
Blue ->
Red
init : Memory
init =
{ status = Opened
, redCupPos = ( -cupWidth, cupHeight )
, blueCupPos = ( cupWidth, cupHeight )
, answer = Red
}
view : Computer -> Memory -> List Shape
view computer { redCupPos, blueCupPos, answer } =
let
( redCupX, redCupY ) =
redCupPos
( blueCupX, blueCupY ) =
blueCupPos
in
[ rectangle black computer.screen.width computer.screen.height
, greenBall
|> (case answer of
Red ->
move redCupX 0
Blue ->
move blueCupX 0
)
, redCup
|> move redCupX redCupY
, blueCup
|> move blueCupX blueCupY
]
update : Computer -> Memory -> Memory
update computer memory =
case memory.status of
Opened ->
if computer.keyboard.down then
{ memory | status = Closing cupHeight }
else if computer.keyboard.shift then
{ memory | status = Switch 20, answer = swap memory.answer }
else
memory
Switch wait ->
if wait == 0 then
{ memory | status = Opened }
else
{ memory | status = Switch (wait - 1) }
Closing n ->
if n == 0 then
{ memory | status = Closed }
else
{ memory | status = Closing (n - 1), redCupPos = Tuple.mapSecond (\x -> x - 1) memory.redCupPos, blueCupPos = Tuple.mapSecond (\x -> x - 1) memory.blueCupPos }
Shuffle wait ->
if wait == 0 then
{ memory | status = Closed }
else
{ memory | status = Shuffle (wait - 1) }
Closed ->
if computer.keyboard.space then
{ memory | status = Shuffle 20, redCupPos = memory.blueCupPos, blueCupPos = memory.redCupPos }
else if computer.keyboard.up then
{ memory | status = Opening cupHeight }
else
memory
Opening n ->
if n == 0 then
{ memory | status = Opened }
else
{ memory | status = Opening (n - 1), redCupPos = Tuple.mapSecond (\x -> x + 1) memory.redCupPos, blueCupPos = Tuple.mapSecond (\x -> x + 1) memory.blueCupPos }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment