Scratch Cat Platformer

Instructions: SCRATCH CAT PLATFORMER 2 is out!\nhttps://scratch.mit.edu/projects/726314868/\n\nMove with the arrow keys. Avoid spikes and lava. Get keys to get through locked doors.\n\nM key to change music.\n\nNOTE:\n\nI am aware that there are a few bugs. They may or may not be fixed in the future.

@Stage\n\nwhen flag clicked\nset [music v] to [1]\nchange [☁ players v] by (1)\nforever\n    if <((music) mod (2)) = [1]> then\n        play sound [Dance Slow Mo v] until done\n    else\n        play sound [Dance Chill Out v] until done\n    end\nend\n\nwhen [h v] key pressed\nask [Command? /reset /change scene to \(scene number\)] and wait\nif <(answer) = [/reset]> then\n    broadcast (Reset and Begin Level v)\nelse\n    if < (answer) contains [/change scene to]?> then\n        set [current letter v] to [17]\n        set [command scene v] to []\n        repeat until <(current letter) = (length of (answer))>\n            change [current letter v] by (1)\n            set [command scene v] to (join (command scene) (letter (current letter) of (answer)))\n        end\n        set [scene # v] to (command scene)\n        broadcast (Change Scene v)\n    end\nend\n\nwhen [m v] key pressed\nchange [music v] by (1)\nstop all sounds\n\n@Thumbnail\n\nwhen flag clicked\nshow\nshow variable [apples v]\ngo to [front v] layer\nset [ghost v] effect to (100)\n\nclear graphic effects\nhide variable [apples v]\n\n@Player\n\nwhen flag clicked\nset [gravity v] to [-1.6]\nset [jump force v] to [12]\nset [acceleration v] to [1.5]\nset [resistance v] to [0.85]\nset [spawn scene v] to [1]\nset [spawn x v] to [-150]\nset [spawn y v] to [55]\nbroadcast (Setup v) and wait\nbroadcast (Reset and Begin Level v)\n\ndefine Move - in steps (steps)\nchange [falling v] by (1)\nrepeat (steps)\n    set [last value v] to (x position)\n    change x by ((speed x) / (steps))\n    Check Touching Solid\n    if <(touching) > [0]> then\n        Collide X - Slope or Wall?\n    end\n    set [last value v] to (y position)\n    change y by ((speed y) / (steps))\n    Check Touching Solid\n    if <(touching) > [0]> then\n        Collide Y - Ceiling or Floor\n    end\nend\n\ndefine Check Touching Solid\nif <<touching (level v)?> or <touching (platform v)?>> then\n    set [touching v] to [1]\nelse\n    set [touching v] to [0]\nend\n\nwhen I receive [tick - player v]\nMoved - by Moving Platform\nswitch backdrop to <touching (level v)?>\nControls - Up and Down\nControls - Left and Right\nMove - in steps (([abs v] of (speed x) ) + ([abs v] of (speed y) ))\nSet Costume\n\ndefine Controls - Up and Down\nif <key (up arrow v) pressed?> then\n    if <<(wall slide) > [0]> and <(jumping) = [0]>> then\n        start sound [Coin v]\n        set [jumping v] to [1]\n        set [falling v] to [3]\n        set [wall slide v] to [0]\n        turn right (180) degrees\n        set [speed x v] to (((direction) / (90)) * (7))\n        set [long jump v] to [12]\n    end\n    if <<(falling) < [3]> and <(jumping) = [0]>> then\n        start sound [Coin v]\n        set [jumping v] to [1]\n        set [falling v] to [3]\n    end\n    if <<(jumping) > [0]> and <(jumping) < (JUMP DURATION)>> then\n        change [jumping v] by (1)\n        set [speed y v] to (JUMP FORCE)\n    end\nelse\n    set [jumping v] to [0]\nend\nchange [speed y v] by (GRAVITY)\nif <(wall slide) > [0]> then\n    Check Can Wall Slide\n    if <(speed y) < [0]> then\n        set [speed y v] to ((speed y) * (0.6))\n    end\nend\n\ndefine Controls - Left and Right\nif <(long jump) > [0]> then\n    change [long jump v] by (-1)\n    stop [this script v]\nend\nset [key x v] to (<key (right arrow v) pressed?> - <key (left arrow v) pressed?>)\nif <(KEY X) = [0]> then\n    if <<(falling) > [2]> and <(wall slide) < [1]>> then\n        set [speed x v] to ((speed x) * (0.98))\n        stop [this script v]\n    end\n    if <([abs v] of (speed x) ) < [1]> then\n        set [frame v] to [0]\n        set [speed x v] to [0]\n    else\n        change [frame v] by (0.5)\n    end\nelse\n    change [speed x v] by ((KEY X) * (ACCELERATION))\n    point in direction ((KEY X) * (90))\n    change [frame v] by (1)\nend\nset [speed x v] to ((speed x) * (RESISTANCE))\n\ndefine Reset and Begin Level\nset [speed y v] to [0]\nset [speed x v] to [0]\nset [falling v] to [99]\nset [wall slide v] to [0]\nset [jumping v] to [0]\nset [long jump v] to [0]\npoint in direction (90)\nBegin Scene # (spawn scene) go to x, y: (spawn x) (spawn y)\n\nwhen I receive [game loop v]\nset [ghost v] effect to (0)\nforever\n    broadcast (Tick - First v)\n    broadcast (Tick - Platforms v)\n    broadcast (Tick - Player v)\n    broadcast (Tick - Last v)\nend\n\nwhen I receive [tick - last v]\nif <(x position) > [235]> then\n    Begin Scene # ((SCENE #) + (1)) go to x, y: [-235] []\nend\nif <(x position) < [-235]> then\n    Begin Scene # ((SCENE #) + (-1)) go to x, y: [235] []\nend\nif <(y position) > [180]> then\n    Begin Scene # ((SCENE #) + (100)) go to x, y: [] [-160]\nend\nif <(y position) < [-180]> then\n    Begin Scene # ((SCENE #) + (-100)) go to x, y: [] [180]\nend\nif <touching (danger v)?> then\n    broadcast (Lose Life v)\nend\n\ndefine Begin Scene # (scene #) go to x, y: (x) (y)\nset [scene # v] to (scene #)\nif <(x) > []> then\n    set x to (x)\nend\nif <(y) > []> then\n    set y to (y)\nend\nbroadcast (Change Scene v)\nsay []\nstop [other scripts in sprite v]\nwait (0) seconds\nswitch costume to (hitbox v2 v)\nset rotation style [don't rotate v]\nif <(x) > []> then\n    Fix Collision in Direction [0]\nelse\n    Fix Collision in Direction [90]\nend\nSet Costume\nbroadcast (Game Loop v)\n\ndefine Fix Collision in Direction (dir)\nset [temp v] to (direction)\nset [distance v] to [1]\npoint in direction (dir)\nrepeat (128)\n    Check Touching Solid\n    if <(touching) < [1]> then\n        point in direction (temp)\n        stop [this script v]\n    end\n    move (distance) steps\n    turn right (180) degrees\n    change [distance v] by (1)\nend\n\nwhen I receive [tick - first v]\nswitch costume to (hitbox v2 v)\nset rotation style [don't rotate v]\nset [platform dx v] to []\nset [platform dy v] to []\n\ndefine Set Costume\nset rotation style [left-right v]\nif <(wall slide) > [0]> then\n    switch costume to (hangon01 v)\n    stop [this script v]\nend\nif <(falling) > [1]> then\n    if <(speed y) > [0]> then\n        switch costume to (jump01 v)\n    else\n        switch costume to (run_j2 v)\n    end\n    stop [this script v]\nend\nif <<([abs v] of (speed x) ) < [0.1]> and <([abs v] of (KEY X) ) > [0]>> then\n    switch costume to (push v)\nelse\n    if <(frame) > [0]> then\n        switch costume to ((1) + ([floor v] of ((frame) mod (16)) ))\n    else\n        switch costume to (hit angle 0 v)\n        repeat (5)\n            Check Touching Solid\n            if <(touching) < [1]> then\n                switch costume to ((costume [number v]) + (5))\n                stop [this script v]\n            end\n            next costume\n        end\n        switch costume to (stand v)\n    end\nend\n\ndefine Collide X - Slope or Wall?\nCheck Can Wall Slide\nchange y by (1)\nCheck Touching Solid\nif <(touching) > [0]> then\n    change y by (1)\n    Check Touching Solid\n    if <(touching) > [0]> then\n        change y by (-2)\n        set x to (last value)\n        set [speed x v] to [0]\n        stop [this script v]\n    end\n    set [speed x v] to ((speed x) * (0.8))\nend\nset [speed x v] to ((speed x) * (0.95))\n\ndefine Collide Y - Ceiling or Floor\nset y to (last value)\nif <(speed y) > [0]> then\n    set [speed y v] to [0]\n    stop [this script v]\nend\nif <(falling) > [0]> then\n    set [falling v] to [0]\n    Slip\nend\nset [speed y v] to ((speed y) * (0.8))\nSlip\n\ndefine Slip\nchange y by (-2)\nchange x by (1)\nCheck Touching Solid\nif <(touching) < [1]> then\n    set [falling v] to [9]\n    change [speed x v] by (1)\n    stop [this script v]\nend\nchange x by (-2)\nCheck Touching Solid\nif <(touching) < [1]> then\n    set [falling v] to [9]\n    change [speed x v] by (-1)\n    stop [this script v]\nend\nchange y by (2)\nchange x by (1)\n\ndefine Check Can Wall Slide\nif <(falling) < [2]> then\n    set [wall slide v] to [0]\n    stop [this script v]\nend\nswitch costume to (hitbox wallslide v)\nset rotation style [left-right v]\nCheck Touching Solid\nset [wall slide v] to (touching)\nswitch costume to (hitbox v2 v)\nset rotation style [don't rotate v]\n\nwhen I receive [lose life v]\nstop [other scripts in sprite v]\nstart sound [Wobble v]\nrepeat (10)\n    change [ghost v] effect by (10)\nend\nwait (0.5) seconds\nReset and Begin Level\n\ndefine Moved - by Moving Platform\nchange x by (PLATFORM DX)\nchange y by (PLATFORM DY)\nCheck Touching Solid\nif <(touching) > [0]> then\n    Find Closest Space to (x position) (y position) max [16]\n    if <(touching) > [0]> then\n        broadcast (Lose Life v)\n    end\nend\n\ndefine Find Closest Space to (x) (y) max (max)\nset [temp v] to (direction)\nset [distance v] to [1]\npoint in direction (0)\nrepeat (max)\n    repeat (16)\n        go to x: (x) y: (y)\n        move (distance) steps\n        Check Touching Solid\n        if <(touching) < [1]> then\n            point in direction (temp)\n            stop [this script v]\n        end\n        turn right (22.5) degrees\n    end\n    change [distance v] by (1)\nend\ngo to x: (x) y: (y)\nmove (distance) steps\npoint in direction (temp)\n\nwhen I receive [reset and begin level v]\nReset and Begin Level\n\nwhen I receive [teleport v]\nswitch costume to (blank v)\nset [scene # v] to (teleport scene)\nbroadcast (Change Scene v)\ngo to x: (teleport x) y: (teleport y)\nswitch costume to (stand v)\n\n@Level\n\nwhen flag clicked\ngo to x: (0) y: (0)\ngo to [back v] layer\n\nwhen I receive [change scene v]\nswitch costume to (join [Scene] (SCENE #))\nerase all\nStamp Outline\nStamp Shadow\n\ndefine Stamp Outline\nset rotation style [don't rotate v]\nset [brightness v] effect to (-100)\nrepeat (8)\n    move (3) steps\n    stamp\n    move (-3) steps\n    turn right (45) degrees\nend\nclear graphic effects\n\ndefine Stamp Shadow\nset [brightness v] effect to (-100)\nset [ghost v] effect to (90)\nchange y by (-20)\nstamp\nchange y by (20)\nclear graphic effects\n\nwhen I receive [reset and begin level v]\nswitch costume to (join [Scene] (SCENE #))\nerase all\nStamp Outline\nStamp Shadow\n\n@Danger\n\nwhen flag clicked\ngo to x: (0) y: (0)\ngo to [back v] layer\n\nwhen I receive [change scene v]\nswitch costume to (blank v)\nswitch costume to (join [Scene] (SCENE #))\nif <<(SCENE #) = [104]> or <<(SCENE #) = [106]> or <<(SCENE #) = [-100]> or <(SCENE #) = [5]>>>> then\n    forever\n        set y to ((3) * ([sin v] of ((timer) * (180)) ))\n    end\nend\n\n@Platform\n\nwhen I receive [change scene v]\ngo to [back v] layer\nFind Animation [n]\nif <(SCENE #) = [5]> then\n    create clone of (_myself_ v)\nend\ndelete this clone\n\ndefine Animate Platform\nset [follow platform? v] to [y]\ngo to x: (-14) y: (100)\nforever\n    glide (5) secs to x: (18) y: (-130)\n    wait (1) seconds\n    glide (5) secs to x: (-14) y: (100)\n    wait (1) seconds\nend\n\nwhen I receive [tick - platforms v]\nTick (x position) (y position)\nset [last x v] to (x position)\nset [last y v] to (y position)\n\ndefine Tick (new x) (new y)\nif <(new y) > (last y)> then\n    change y by (1)\nelse\n    set y to ((last y) + (1))\nend\nset [platform changing x v] to (((new x) - (last x)) + (join (letter (2) of (follow platform?)) (letter (3) of (follow platform?))))\nset [platform changing y v] to ((new y) - (last y))\nif <touching (player v)?> then\n    if <(letter (1) of (follow platform?)) = [y]> then\n        set [platform dx v] to (PLATFORM CHANGING X)\n    end\n    set [platform dy v] to (PLATFORM CHANGING Y)\nend\ngo to x: (new x) y: (new y)\n\ndefine Animate 2\nset [follow platform? v] to [y]\ngo to x: (23) y: (-84)\nwait (3) seconds\nforever\n    repeat (4)\n        glide (pick random (0.5) to (3.0)) secs to x: (pick random (23) to (114)) y: (pick random (-84) to (84))\n        wait (1) seconds\n    end\n    glide (pick random (0.5) to (3.0)) secs to x: (23) y: (-84)\n    wait (1) seconds\nend\n\ndefine Find Animation (clone?)\nswitch costume to (join [Scene] (SCENE #))\nshow\nif <(SCENE #) = [5]> then\n    Animate 3\n    stop [this script v]\nend\nif <(SCENE #) = [6]> then\n    Animate 4\n    stop [this script v]\nend\nif <(SCENE #) = [104]> then\n    Door at [0] [12] key [Key 1]\n    stop [this script v]\nend\nif <(SCENE #) = [105]> then\n    Animate Platform\n    stop [this script v]\nend\nif <(SCENE #) = [106]> then\n    Animate 2\n    stop [this script v]\nend\nif <(SCENE #) = [3]> then\n    Animate 5\n    stop [this script v]\nend\nif <(SCENE #) = [3]> then\n    Animate 6\n    stop [this script v]\nend\nif <(SCENE #) = [-100]> then\n    Animate 7\n    stop [this script v]\nend\nhide\n\nhide\n\ndefine Animate 3\nset [follow platform? v] to [n]\ngo to x: (-26) y: (86)\nforever\n    glide (3) secs to x: (-26) y: (20)\n    glide (2) secs to x: (-120) y: (20)\n    glide (1) secs to x: (-120) y: (-50)\n    glide (3) secs to x: (81) y: (-49)\n    wait (2) seconds\n    glide (2) secs to x: (-26) y: (86)\nend\n\ndefine Animate 4\ngo to x: (0) y: (0)\nforever\n    if <([x position v] of [player v]) < [0]> then\n        set [follow platform? v] to [y-3]\n    else\n        set [follow platform? v] to [y03]\n    end\nend\n\ndefine Door at (x) (y) key (key name)\nswitch costume to (key name)\ngo to x: (x) y: (y)\nforever\n    wait until <touching (player v)?>\n    if <[collected v] contains (key name)?> then\n        start sound [Buzz Whir v]\n        repeat (24)\n            change y by (4)\n        end\n        wait (1) seconds\n        start sound [Buzz Whir v]\n        repeat (24)\n            change y by (-4)\n        end\n        start sound [Crunch v]\n    else\n        start sound [Low Squeak v]\n    end\n    wait until <not <touching (player v)?>>\nend\n\ndefine Animate 5\nset [follow platform? v] to [y]\ngo to x: (125) y: (50)\nforever\n    glide (2) secs to x: (-18) y: (18)\n    wait (2) seconds\n    glide (1) secs to x: (127) y: (18)\n    glide (1) secs to x: (138) y: (150)\n    wait (1) seconds\n    glide (1) secs to x: (125) y: (18)\nend\n\ndefine Animate 6\nset [follow platform? v] to [y]\ngo to x: (-175) y: (-175)\nforever\n    glide (2) secs to x: (145) y: (0)\n    wait (4) seconds\n    glide (2) secs to x: (145) y: (-175)\n    wait (4) seconds\nend\n\ndefine Animate 7\nset [follow platform? v] to [n]\ngo to x: (-144) y: (-45)\nforever\n    glide (2) secs to x: (180) y: (-85)\n    wait (0.5) seconds\n    glide (2) secs to x: (-144) y: (-25)\n    wait (1) seconds\n    glide (0.5) secs to x: (-144) y: (5)\n    wait (1) seconds\nend\n\n@Collectibles\n\nwhen I receive [change scene v]\nif <(SCENE #) = (my scene)> then\n    show\nelse\n    hide\nend\n\nwhen I start as a clone\ngo to [front v] layer\nrepeat until <touching (player v)?>\n    change y by ((0.5) * ([sin v] of (frame) ))\n    if < (costume [name v]) contains [Key]?> then\n        turn right ([sin v] of (frame) ) degrees\n    end\n    if <(SCENE #) = [apple]> then\n        if <(follow platform?) = (my scene)> then\n            change x by (PLATFORM CHANGING X)\n            change y by (PLATFORM CHANGING Y)\n        end\n    end\n    change [frame v] by (5)\nend\nset [my scene v] to []\nif <(costume [name v]) = [apple]> then\n    start sound [Bite v]\n    change [apples v] by (1)\nelse\n    if <(costume [name v]) = [banana]> then\n        say [Congrats! You found the magic banana!]\n        play sound [Chomp v] until done\n        say []\n    else\n        if <(costume [name v]) = [green flag]> then\n            set [spawn scene v] to (SCENE #)\n            set [spawn x v] to (x position)\n            set [spawn y v] to (y position)\n        else\n            start sound [Wand v]\n            repeat (20)\n                change y by (2)\n                change [ghost v] effect by (5)\n            end\n            add (costume [name v]) to [collected v]\n            hide\n        end\n    end\nend\nrepeat (20)\n    change y by (2)\n    change [ghost v] effect by (5)\nend\nhide\n\nwhen I receive [setup v]\ndelete all of [collected v]\nset [frame v] to [0]\nset [apples v] to [0]\nhide\nPlace [apple] scene [-100] x y [100] [50] follow [n]\nPlace [apple] scene [-97] x y [-15] [0] follow [n]\nPlace [apple] scene [-97] x y [33] [15] follow [n]\nPlace [apple] scene [0] x y [-95] [10] follow [n]\nPlace [apple] scene [0] x y [0] [50] follow [n]\nPlace [apple] scene [1] x y [-75] [75] follow [n]\nPlace [apple] scene [1] x y [124] [9] follow [n]\nPlace [apple] scene [1] x y [-100] [150] follow [n]\nPlace [green flag] scene [1] x y [20] [70] follow [n]\nPlace [apple] scene [2] x y [93] [133] follow [n]\nPlace [apple] scene [2] x y [-75] [80] follow [n]\nPlace [apple] scene [3] x y [-60] [85] follow [n]\nPlace [apple] scene [3] x y [90] [-50] follow [n]\nPlace [apple] scene [100] x y [-85] [55] follow [n]\nPlace [apple] scene [100] x y [100] [50] follow [n]\nPlace [apple] scene [101] x y [60] [-114] follow [n]\nPlace [Key 1] scene [101] x y [-141] [-30] follow [n]\nPlace [green flag] scene [101] x y [190] [-124] follow [n]\nPlace [apple] scene [103] x y [96] [-123] follow [n]\nPlace [apple] scene [104] x y [34] [96] follow [n]\nPlace [apple] scene [105] x y [34] [15] follow [y]\nPlace [apple] scene [106] x y [47] [-30] follow [y]\nPlace [apple] scene [201] x y [0] [0] follow [y]\nPlace [apple] scene [203] x y [50] [5] follow [y]\nPlace [apple] scene [204] x y [25] [60] follow [y]\nPlace [apple] scene [205] x y [0] [0] follow [y]\nPlace [apple] scene [206] x y [140] [75] follow [y]\nPlace [Key 2] scene [150] x y [150] [-150] follow [n]\n\ndefine Place (costume name) scene (scene) x y (x) (y) follow (follow)\nset [my scene v] to (scene)\nswitch costume to (costume name)\ngo to x: (x) y: (y)\nset [follow platform? v] to (follow)\ncreate clone of (_myself_ v)\nset [my scene v] to []\n\n@Portal\n\nwhen I receive [change scene v]\nclear graphic effects\nswitch costume to (blank v)\nswitch costume to (join [Scene] (SCENE #))\nforever\n    if <touching (player v)?> then\n        if <(costume [name v]) = [Scene-100]> then\n            set [teleport scene v] to [1]\n            set [teleport x v] to [0]\n            set [teleport y v] to [0]\n        else\n            if <(costume [name v]) = [Scene-97]> then\n                set [teleport scene v] to [102]\n                set [teleport x v] to [75]\n                set [teleport y v] to [122]\n            end\n        end\n        broadcast (Teleport v)\n    end\nend\n\n