Scratch Platformer Game 11.Coding Platformer Enemies

Instructions: Instructions\nMove with arrows or with joystick\n\nUpdated : can play on mobile device

@Stage\n\n@Thumbnail\n\nwhen flag clicked\nshow\ngo to [front v] layer\nset [ghost v] effect to (100)\n\n@Player\n\nwhen flag clicked\nbroadcast (main menu 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 y) ) + ([abs v] of (speed x) ))\nSet Costume\n\ndefine Controls - Up and Down\nif <<key (up arrow v) pressed?> or <(STICK Y) > [0.5]>> then\n    if <<(wall slide) > [0]> and <(jumping) = [0]>> then\n        start sound [Jump 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 [Jump 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?> or <(STICK X) > [0.5]>> - <<key (left arrow v) pressed?> or <(STICK X) < [-0.5]>>)\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 [invulnerable v] to [0]\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 (about to leave scene v)\nbroadcast (Change Scene v)\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 <(frame) > [0]> then\n    switch costume to ((1) + ([floor v] of ((frame) mod (16)) ))\nelse\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)\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))\nSlip\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))\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]\nset [invulnerable v] to [1]\nstop [other scripts in sprite v]\nstart sound [Oops v]\nrepeat (10)\n    change [ghost v] effect by (10)\nend\nwait (0.5) seconds\nchange [lives v] by (-1)\nif <(lives) > [0]> then\n    Reset and Begin level\nelse\n    broadcast (agme over v)\nend\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)\npoint in direction (temp)\nstop [this script v]\n\nwhen I receive [start game v]\nset [gravity v] to [-1.2]\nset [jump force v] to [12]\nset [jump duration v] to [6]\nset [acceleration v] to [1.5]\nset [resistance v] to [0.8]\nbroadcast (reset game v) and wait\nbroadcast (Setup v) and wait\nset [lives v] to [3]\nset [spawn scene v] to [1]\nset [spawn x v] to [-132]\nset [spawn y v] to [50]\nReset and Begin level\n\nwhen I receive [player bounce v]\nset [speed y v] to [10]\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 Shadow\nStamp Outline\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\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\nwhen flag clicked\nforever\n    play sound [Dance Around v] until done\nend\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 #))\n\nif <(SCENE #) = [3]> 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]\nswitch costume to (costume1 v)\ngo to [back v] layer\nhide\nif <(SCENE #) = [4]> then\n    Door at [-24] [52] key [Key 1]\nend\nif <(SCENE #) = [0]> then\nend\n\ndefine Animate Platform\nswitch costume to (costume1 v)\ngo to x: (19) y: (-47)\npoint in direction (90)\nforever\n    wait (1) seconds\n    glide (2) secs to x: (15) y: (90)\n    wait (1) seconds\n    glide (1) secs to x: (163) y: (51)\n    wait (1) seconds\n    glide (2) secs to x: (19) y: (-47)\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\nif <touching (player v)?> then\n    set [platform dx v] to ((new x) - (last x))\n    set [platform dy v] to ((new y) - (last y))\nend\ngo to x: (new x) y: (new y)\n\ndefine Animate 2\nswitch costume to (costume1 v)\ngo to x: (102) y: (87)\npoint in direction (0)\nforever\n    wait (0.5) seconds\n    glide (0.5) secs to x: (102) y: (-25)\n    wait (1) seconds\n    glide (2) secs to x: (102) y: (87)\nend\n\ndefine Animate 3\nswitch costume to (costume1 v)\ngo to x: (-130) y: (-64)\npoint in direction (90)\nforever\n    wait (1) seconds\n    glide (2) secs to x: (15) y: (-64)\n    wait (1) seconds\n    glide (2) secs to x: (-130) y: (-64)\nend\n\ndefine Animate 4\nswitch costume to (wide v)\ngo to x: (-198) y: (-54)\npoint in direction (90)\nforever\n    wait (1) seconds\n    glide (5) secs to x: (219) y: (-54)\n    wait (1) seconds\n    glide (5) secs to x: (-198) y: (-54)\nend\n\ndefine Door at (x) (y) key (key name)\nshow\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 (x) (y) (x2) (y2) (time)\nshow\npoint in direction (90)\ngo to x: (x) y: (y)\nforever\n    wait (0.5) seconds\n    glide (time) secs to x: (x2) y: (y2)\n    wait (0.5) seconds\n    glide (time) secs to x: (x) y: (y)\nend\n\n@Collectable\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\nrepeat until <touching (player v)?>\n    change y by ((0.5) * ([sin v] of (frame) ))\n    change [frame v] by (5)\nend\nset [my scene v] to []\nif < (costume [name v]) contains [apple]?> then\n    start sound [Chomp v]\n    change [apples v] by (1)\nelse\n    if < (costume [name v]) contains [Green Flag]?> then\n        start sound [Wand v]\n        set [spawn scene v] to (SCENE #)\n        set [spawn x v] to ([x position v] of [player v])\n        set [spawn y v] to ([y position v] of [player v])\n    else\n        start sound [Wand v]\n        add (costume [name v]) to [collected v]\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 [apples v] to [0]\nset [frame v] to [0]\nhide\nPlace [apple] scene [1] xy [155] [-25]\nPlace [apple] scene [1] xy [-27] [43]\nPlace [apple] scene [2] xy [16] [-65]\nPlace [apple] scene [103] xy [-132] [48]\nPlace [Key 1] scene [103] xy [192] [4]\nPlace [apple big] scene [5] xy [100] [-4]\nPlace [apple big] scene [102] xy [-80] [-4]\nPlace [apple big] scene [105] xy [-170] [135]\nPlace [apple] scene [105] xy [120] [-20]\nPlace [Green Flag] scene [2] xy [160] [-50]\nPlace [Green Flag] scene [4] xy [138] [57]\nPlace [apple big] scene [106] xy [-40] [90]\nPlace [apple] scene [107] xy [40] [35]\nPlace [apple] scene [108] xy [40] [35]\nPlace [apple] scene [108] xy [60] [35]\nPlace [apple] scene [108] xy [20] [35]\nPlace [apple] scene [109] xy [-60] [25]\nPlace [apple] scene [109] xy [-24] [25]\nPlace [apple] scene [109] xy [55] [25]\nPlace [apple] scene [109] xy [18] [25]\nPlace [apple] scene [109] xy [-60] [63]\nPlace [apple] scene [109] xy [-24] [63]\nPlace [apple] scene [109] xy [55] [63]\nPlace [apple] scene [109] xy [18] [63]\nPlace [apple] scene [109] xy [-60] [120]\nPlace [apple] scene [109] xy [-24] [120]\nPlace [apple] scene [109] xy [55] [120]\nPlace [apple] scene [109] xy [18] [120]\nPlace [apple big] scene [109] xy [140] [108]\nPlace [apple big] scene [6] xy [-82] [95]\nPlace [apple big] scene [6] xy [25] [95]\nPlace [apple big] scene [6] xy [155] [95]\nPlace [apple] scene [6] xy [-120] [-67]\nPlace [apple] scene [6] xy [-120] [-40]\nPlace [apple] scene [6] xy [-80] [-40]\nPlace [apple] scene [6] xy [-40] [-40]\nPlace [apple] scene [6] xy [-223] [-67]\nPlace [apple] scene [6] xy [0] [-40]\nPlace [apple] scene [6] xy [40] [-40]\nPlace [apple] scene [6] xy [80] [-40]\nPlace [apple] scene [6] xy [120] [-40]\nPlace [apple] scene [6] xy [-120] [-67]\nPlace [apple] scene [6] xy [-80] [-67]\nPlace [apple] scene [6] xy [-40] [-67]\nPlace [apple] scene [6] xy [0] [-67]\nPlace [apple] scene [6] xy [40] [-67]\nPlace [apple] scene [6] xy [80] [-67]\nPlace [apple] scene [6] xy [120] [-67]\n\ndefine Place (costume name) scene (scene) xy (x) (y)\nset [my scene v] to (scene)\nswitch costume to (costume name)\ngo to x: (x) y: (y)\ncreate clone of (_myself_ v)\nset [my scene v] to []\n\nwhen I receive [reset game v]\ndelete this clone\n\n@splash screen\n\nwhen flag clicked\ngo to x: (0) y: (0)\nswitch costume to (main menu v)\ngo to [front v] layer\nshow\n\nwhen I receive [agme over v]\ngo to x: (0) y: (0)\nswitch costume to (game over v)\ngo to [front v] layer\nshow\nbroadcast (main menu v)\n\nwhen I receive [start game v]\nhide\n\n@main menu\n\nwhen I receive [main menu v]\nwait (0.1) seconds\ngo to [front v] layer\nshow\nset size to (10) %\ngo to x: (0) y: (-78)\nforever\n    if <touching (mouse-pointer v)?> then\n        change size by ((0.2) * ((110) - (size)))\n    else\n        change size by ((0.2) * ((100) - (size)))\n    end\nend\n\nwhen flag clicked\nhide\n\nwhen this sprite clicked\nhide\nbroadcast (start game v)\n\n@enemy\n\nwhen flag clicked\nset rotation style [left-right v]\nhide\n\nwhen I receive [change scene v]\nif <(SCENE #) = [1]> then\n    if <([x position v] of [player v]) < [0]> then\n        spawn [red] at [210] [-76] dir [-90]\n    else\n        spawn [red] at [87] [-76] dir [90]\n    end\n    spawn [red] at [6] [34] dir [90]\nend\nif <(SCENE #) = [103]> then\n    spawn [red] at [110] [-2] dir [90]\nend\nif <(SCENE #) = [4]> then\n    spawn [red] at [58] [24] dir [90]\nend\nif <(SCENE #) = [104]> then\n    spawn [red] at [130] [-105] dir [90]\nend\nif <(SCENE #) = [105]> then\n    spawn [red] at [-112] [86] dir [90]\nend\nif <(SCENE #) = [106]> then\n    spawn [red] at [-20] [30] dir [90]\nend\n\nwhen I start as a clone\nshow\nset [speed y v] to [0]\nset [frame v] to [0]\nforever\n    switch costume to (red1 v)\n    set rotation style [don't rotate v]\n    move - left and right\n    move - down\n    change [frame v] by (0.33)\n    switch costume to ((1) + (([floor v] of (frame) ) mod (4)))\n    set rotation style [left-right v]\n    if <<(invulnerable) = [0]> and <touching (player v)?>> then\n        touch player\n    end\nend\n\nwhen I receive [about to leave scene v]\ndelete this clone\n\ndefine move - down\nchange [speed y v] by (-1)\nchange y by (speed y)\nrepeat until <not <touching (level v)?>>\n    change y by (1)\n    set [speed y v] to [0]\nend\n\ndefine move - left and right\nmove (2) steps\nrepeat (4)\n    if <not <<touching (level v)?> or <touching (_edge_ v)?>>> then\n        if <not <<touching (safe zone v)?> or <touching (platform v)?>>> then\n            stop [this script v]\n        end\n    end\n    change y by (1)\nend\nchange y by (-4)\nmove (-2) steps\nturn right (180) degrees\n\ndefine touch player\nif <([speed y v] of [player v]) < [-3]> then\n    broadcast (player bounce v)\n    start sound [Squish Pop v]\n    switch costume to (red squish v)\n    wait (0.3) seconds\n    repeat (10)\n        change [ghost v] effect by (10)\n    end\n    delete this clone\nend\nbroadcast (Lose Life v)\n\ndefine spawn (type) at (x) (y) dir (dir)\ngo to x: (x) y: (y)\npoint in direction (dir)\ncreate clone of (_myself_ v)\n\n@safe zone\n\nwhen flag clicked\ngo to x: (0) y: (0)\ngo to [back v] layer\nset [ghost v] effect to (100)\n\nwhen I receive [change scene v]\nswitch costume to (blank v)\nswitch costume to (join [Scene] (SCENE #))\nif <(SCENE #) = [3]> then\n    forever\n        set y to ((3) * ([sin v] of ((timer) * (180)) ))\n    end\nend\n\n@Sprite1\n\nwhen flag clicked\nhide\nforever\n    if <(SCENE #) = [108]> then\n        switch costume to (costume1 v)\n        show\n    else\n        if <(SCENE #) = [5]> then\n            go to x: (0) y: (0)\n            switch costume to (scene5 v)\n            show\n        else\n            hide\n        end\n    end\nend\n\n@Control stick\n\nwhen flag clicked\nset [myself v] to [Control stick]\nset [max stick v] to [25]\nswitch costume to (base v)\nset [ghost v] effect to (60)\ncreate clone of (_myself_ v)\n\nwhen I start as a clone\nswitch costume to (stick v)\ngo [forward v] (1) layers\nset [ghost v] effect to (25)\nforever\n    set [stick power v] to []\n    set [stick x v] to [0]\n    set [stick y v] to [0]\n    repeat until <mouse down?>\n        go to (myself)\n    end\n    if <(distance to [mouse-pointer v]) < ((size) * (0.6))> then\n        Stick Drag\n    else\n        wait until <not <mouse down?>>\n    end\nend\n\ndefine Stick Drag\nrepeat until <not <mouse down?>>\n    go to (mouse-pointer v)\n    point towards (myself)\n    turn right (180) degrees\n    set [stick direction v] to (direction)\n    set [stick power v] to (distance to [(myself) v])\n    if <(STICK POWER) > (max stick)> then\n        move ((max stick) - (STICK POWER)) steps\n        set [stick power v] to (max stick)\n    end\n    set [stick power v] to ((STICK POWER) / (max stick))\n    set [stick x v] to ((STICK POWER) * ([sin v] of (STICK DIRECTION) ))\n    set [stick y v] to ((STICK POWER) * ([cos v] of (STICK DIRECTION) ))\nend\n\n