Coding Platformer Enemies | 11. Scratch Platformer Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello fellow scratchers what better way to enhance our classic platformers than to introduce enemies into the fray you may recognize this little red chomping fellow from my apple game the great thing about enemies is that unlike spikes or lava enemies tend to be a lot more dynamic bringing the level to life and also requiring more skill to avoid or to take out so we'll be adding simple enemy spawning movement gravity incline traversal and finally the ability to squish an enemy by jumping on his head classic stuff indeed so what are we waiting for let's get scratching we begin where we left off in episode 10 saving our project as a new copy for this is episode 11. so i'm thinking to place the first enemy down here on the first scene it'll be best to make a new sprite for the enemy and i'll name it enemy makes sense for fun i'm drawing the little red enemy dude from apple begin by drawing a simple square now we can check the size looks good on the stage view and we can also drag them into a suitable space while we're at it when you design an enemy remember to always draw them facing to the right even if you want them to face left or rotate to fly upwards it really doesn't matter we still draw them facing right always then once you have them drawn just double check that the costume is centered on the canvas i do that by selecting the whole thing and then dragging them to the middle where they should just snap into place there you see if your costume is not centered in the costume editor then its position and collision sensing gets way messed up and in turn that causes bugs later on so you really want to get this centering step mastered right let's give this enemy costume a name red one i use a one because next up we are going to add more costumes for simple enemy animation i want them to open and close their mouth so duplicate red one costume and now we have a red two select the entire top half of the costume rotate it and move it back into place nice we then do the same again duplicating red two and we widen the mouth further still brilliant to complete the animation we need the mouth to begin to close once more simplest way to achieve this is to cheat and duplicate costume 2 again dragging them to the bottom of the list now we have the costumes in animating order from closed to open and then beginning to close once more ready to loop back around to fully closed and then to repeat excellent we just need one more costume and that is for when red gets squashed duplicate costume one and drag it down to the bottom of the list i think we should name it red squish then just flatten the costume by selecting it all and dragging from the top downwards this ensures the bottom won't lift off the ground you might also like to pull out the sides a bit to give that extra squished look if you hold down the alt key while dragging then it sizes around its middle which is extra useful here super right costumes are done it's time to do some coding we've used a number of methods to bring sprites into the game the collectibles were clones that we spawned across the entire level right at the beginning the lifts and danger sprites well they weren't clones but instead have one sprite that we reused on each scene where needed but for enemies we will do something a little different again using clones that would allow us to have more than one enemy per scene i like that but to avoid building up loads of clones and any overheads that that might bring will only spawn them when needed and despawn them again when we leave a scene so as normal when working with clones the original sprite should be hidden when flag clicked hide let's work on enemy spawning they will appear when we enter a scene so when i receive change scene then check which scene we entered if scene equals one okay so the player has entered scene one we want to clone a new red enemy drag in a go to xy block okay hold on i actually want to move them further to the right over here now remove that block and drag in a new one that way it's all set with the x and y values for the new position next up we want to face left with a point in direction negative 90. cool that's the basics so we can go ahead and create a clone of myself that will ensure that the new clone is created positioned and orientated just as we want but just like the original sprite it too will start hidden easy to fix bring in a when i start as clone hat block and show nice and simple let's give it a test just click play oh man what red what are you doing upside down man obviously the rotation mode of this sprite is not set correctly we'll address this under the when green flag clicked hat pop in a set rotation mode to left right that should stop the sprite appearing upside down and limit it to only flipping left and right okay much better the enemy spawning is working but if we move on to the next scene ah the enemy is still here we need to ensure we despawn them again as we leave a scene now we have a number of options of how to do this we already have the when scene changed event that triggers when we move off a scene and enter a new one but to make things really clear and easy to follow i suggest we add a new event that runs before this one for leaving a scene for this we should click into the player sprite and we need to find the define begin scene scripts this is where we are broadcasting the change scene event we now want another broadcast before this one so separate off the stop block duplicate the broadcast and join it all back together next we'll change the first broadcast to send out a new message now in retrospect i think a better name for this might have been leave or exit scene but i gave it the name about to change scene of course it really doesn't matter but do feel free to name it whatever you like okay so back to the enemy sprite we can now bring in a when i receive about to change scene and to remove all the enemy clones is now easy just delete this clone cool now the begin scene event can safely run and everything should be good shall we test yeah the enemy clones are gone so what next well this enemy is looking a bit floaty shall we add in some gravity to bring them down to the ground make sure we are still in the enemy sprite and will continue coding in the when start as clone script they need their own little game loop so bring in a forever block for gravity we need a speed y variable make it for this sprite only as each clone needs its own speed initialize speed y to zero just before the forever loop begins gravity is simply a constant force pulling downwards so we change our speed y by a pull of negative one each game tick then actually move the enemy with a change y by and drop in the speed y variable yeah you'll see this a bazillion times in all our games but this is not enough as if right now the falling enemy falls straight through the level we need to stop them but as lifting them out of the floor requires a bit of extra looping we'll need that custom block magic make a new custom block naming it move down and yes tick the run without screen refresh block we need it okay we will move the gravity code into this new custom block and replace it with a move down great so now if after moving the enemy down here we find we are overlapping that is touching the level we want to move the enemy back up again until they are not touching the level simplest way to do this is with a repeat until block and we repeat until we are not touching the level okay so after we've moved down if they are not touching the level then this repeat doesn't even run but if they are then we repeatedly change y by one to bring them up until they are again no longer touching the level sweet we just need to reset the full speed by setting their speed y to zero and that's it testing the project now shows the enemy is nicely grounded if i go back to the project view then i can drag them around and they always fall back to the ground excellent we are ready to move on in fact moving on is exactly what we want this enemy to do so a new custom block for this to match the move down one name it move left and right tick to run without screen refresh and it would be best to make use of this before the move down block i think so assuming the enemy is facing left or right let's use the move block and move two steps forward then we check for collisions we may have hit a wall right so if touching level then we move back out of the wall with a move minus two steps negative two steps and then we just turn them around to walk back the opposite direction turn clockwise by 180 degrees it's testing time here we go the enemy is coming this way they hit the wall turn around and walk off and oh they appear to be sulking at the edge of the level like one of my pet totes they also bury their heads in the ground and pretend not to be there yep we can see you red well we can choose here to do one of two things either they walk off the screen or they turn around and come back i say we turn them around so instead of just checking touching level we need an ore block and also check for touching edge that is the edge of the screen now when we test the game the enemy also turns when they reach the edge of the screen too perfect this is looking really good but you know often when you draw levels in scratch surfaces are not perfectly straight and if i was to introduce a small bump into the level i think we will find that the enemy can be thwarted all too easily what we need to do is let them have a little bit of lift to get them over these small obstacles but still turn them around when hitting a wall we can start by separating off the if touching script now instead we will give the enemy a number of tries to get out of the floor before giving up and turning around a good block for this is a repeat block and i'm plumbing for a repeat four times but hold on we haven't even asked if we actually have collided yet well no problem we do this first thing inside this repeat loop if not then bring back the touching checks into here if we have moved sideways and are not touching the level or the edge of the screen then we don't want to do anything so just stop this script see it doesn't matter that we have entered this repeat block we will still stop running this move and left block here and all is done but if we are touching the level then we can try moving y by one to get out of the slope and this will now give us four tries if at any point it manages to get out of the level then the script stops and it's all okay no need to turn around it was just a slope but if the enemy is still touching the level after four tries of moving up then we give up this is too much for the enemy to get around we need to ensure we return them back down the four pixels so change y by negative four and then we bring back the move minus two steps and turn 180 degrees that will turn the enemy around as before excellent that makes sense doesn't it let's test oh yes very cool and smooth they are gliding nicely back and forth and that little bump in the road doesn't pose us with any problems great so with the movement done we can focus next on animations now if we were to just begin switching enemy costumes we would instantly begin hitting the same old problem of sprite collisions occurring due to changes in costume size and shape now this is because we are not at present using hitboxes for the enemies so the collisions are done using the enemy's current costume we need to ensure that the collision detection the touching blocks always use the same costume so let's begin by ensuring that this is the case at the start of the forever loop before we move the enemy switch the costume to red one now because our enemy is square we might as well just use this as our hitbox but you could if you like draw a new costume for this now we should also set the rotation mode to don't rotate because if you remember even flipping the direction of the costume can cause the same hitbox changing issues we don't want to leave them always facing right so after the player is moved each frame we reset the costume and rotation mode to how they were before red one and left and right mode good looking sensible so now for the animations since we are always switching costume back to red one for the collision detection we'll need to keep track of the current animation frame in some other way make a new variable named frame for this sprite only we'll initialize frame to zero before our forever loop kicks off and then we'll need to change frame by to animate through the costumes put it just before we switch the costume back to red one here but rather than adding one let's slow down the animation to once every three or so frames by changing frame by one third that's 0.33 around you will probably have seen this math script before in the switch costume we need an addition operator now we are beginning the animation on costume one here and we animate over the four costumes before looping back to costume 1 again so we start on costume 1. stick that in on the left then on the right we need a floor block this rounds numbers down to their nearest whole number so from frame 1.33 down to frame one next we need mod to loop us around each of the four costumes so the four goes in on the right mod gives us the remainder after division now we can place the floor of the frame on the left of the mod ta-da that's it start on frame one and loop around the next four frames over and over again and that's it and there we have it the animation is looking great and the animation speed is working well too so are you ready to get a little more interaction going off here it shouldn't be hard to allow this enemy to hurt us since the danger sprite does this already with just a broadcast of lou's life indeed at the end of the forever loop drop in an if block and look for when the enemy is touching player i don't want to extend this loop with too many scripts so make a new custom block naming it touching player but this time don't tick the run without screen refresh we may want to add some animation into this script after all yeah so make use of the block right away when we know we are touching the player here and for starters just drag in a broadcast lose life we are going down so run the project here we go touching the enemy what was that sound the player death sound got looped over and over in the most horrible way there so we must be triggering the lose life over multiple frames the problem is that the enemy keeps moving after we die which i like and then they are still checking for collisions as we are fading out we need to prevent further checks while the loose life animation is playing okay then so no problem when we check for touching player add in an and block and an equals we'll need a new variable name it invulnerable for all sprites this will indicate that we cannot be killed and we'll check whether it equals zero so if the player is not invulnerable and the enemy is touching the player only then do we trigger the loser life that's all very well and good but we need to now set the invulnerability when we lose a life that will be in the player sprite so click in there and find the when i receive lose life script then right at the top stuff in a set invulnerable to one nice that should prevent the horrible sound but we also need to reset this back to zero when the game begins so find the define reset and begin level script and we'll set vulnerable to zero right at the top excellent let's test that out oh phew that is a relief that is much more bearable good work of course everyone knows though that we should be able to jump on this enemy's head to defeat it that would be a great addition to this project and we'll detect it by looking for when the player is simply moving down from above place an if before the broadcast lose life if the speed y of the player is less than we need to get the speed y from the off block from the sensing category and i'm going to plumb for a speed of minus one we want to be sure they are traveling downwards when they touch the enemy now we should play a squish sound from the sounds tab we can search up one here we go squish pop should be good so in the if start sound squish pop now remember we made a costume for when the enemy was squished switch costume to red squish next we want to wait for a moment perhaps 0.3 seconds before fading away to do that we repeat 10 that is why we didn't tick the run without screen refresh otherwise this would not have animated and we change ghost effect by 10 as ten tens are 100 and it will have totally faded away super we just need to finally delete this clone after the repeat loop has finished off shall we give this a quick test i should be able to jump on his head oh man i just ran into him let's try that again yeah there we go squished him flat but there's something missing here did you see what it was yeah we need the player to bounce up again it just looks wrong with the player landing on the ground after the squish since we can't set the player's speed y from the enemy sprite we need to broadcast right before playing the squish sound and broadcast a new message named player bounce click into the player's sprite and find some space when i receive player bounce we just set speed y to how about 10 we can always change this at a later date if it seems too much or too little you know what to do by now shall we test that out splendid i really like that the little bounce just makes all the difference okay guys i wonder if you have been concerned about where i placed this first enemy did any of you think oh man he's going to regret that when the player tries to come back onto the first scene well if so then you would be quite right see how unfortunate this is in years gone by i have played actual commercial games where this has been a problem we need to be more careful to consider all the directions a player might enter a scene such that they don't stumble onto an enemy spawn zone and get an auto kill however we can be a bit more cunning yet but to do so we should first improve our enemy spawning script click into the enemy sprite and find the when i receive change scene script we'll make a custom block to wrap up all the positioning and cloning here name it spawn add an input of type then a text label of at then two numeric inputs of x and y a text label of der d i r for direction and an input of der2 great run without screen refresh we can move the position and the clone script in there and make use of the block in its place now the type part is not currently used but i find it nice to give it a name so stick in red then we copy the x and y positions and of course the direction of minus 90. coolio we just need to link up the x and y inputs to the go to xy and the input to the point in direction and that is that give it a quick test yeah looking good so what was i saying about a cunning plan right yes so the cunning plan is why do we have to position the enemy in the same place all the time why not change their position if the player comes into the scene from a different direction can we do that sure we can stick in and if else around the spawn block right in here we need to check which side of the screen the player is on stick in a less than comparator and we are looking for the player's x position if x position of player is less than zero then we have entered the scene from the left so we are fine to position the enemy to the far right now let's duplicate the spawn block into the else so where should we spawn the enemy if the player entered from the right of the scene well make the enemy visible on the stage and position them further over to the left away from the auto kill zone for me that gives an x of 87 so i enter that into the new x spawn position also i want the enemy to be facing right so change the direction to 90. finally we can test that the game starts with our enemy on the far right as before so leaping over him we can come in from the right yes we didn't die which is a great sign and yes the enemy now appeared over to the left as we came into the scene brilliant so of course you only need to do this if you want the enemy to behave differently when you enter the scene from left or right otherwise we can just leave a single spawn block for a scene but what if you wanted to make more than one enemy per scene can we do that well yes of course just stop the project we make the enemy sprite visible and position it where you want them to spawn then duplicate a new spawn block within the same if scene equals 1 and fill out the x and y position from the current x and y position on the stage and i'll make them face left okay give that a run through so you see both enemies have spawned at the same time and everything is working great in this game the enemies always respawn once you return to a scene you may or may not want this if there's a great demand then i'll show you other ways that this can work in later tutorials but this keeps the game more lively okay we are close to concluding this episode now but one last thing i want to cover is how we might prevent this top enemy from falling off of the top platform often we want to keep enemies patrolling within certain zones the simplest way to achieve this is by adding safe zones that is areas where enemies can not walk like extra walls that only appear to enemies start by duplicating the danger sprite i chose this because it has the least scripts so we'll need the least cleaning up name it safe zones then in the costume editor delete all but the blank costume now as i want to add safe zones to scene 1 of the game i will click into the level sprite and copy scene 1 costume into the new safe zone sprite cool click back into the safe zone sprite now we can now draw in any enemy blocking walls i want one right here at the edge of this top platform so running the project now you can see the new wall has appeared but it's not doing anything so click into the enemy sprite and find the move left right script as well as colliding with the level we now want to also collide with safe zones but you know this script is getting a bit too wide for my liking so pop in a second if inside the first because these are not touching we can say not touching safe zone here and it works just the same run that now and you should find the player can jump right through these zones but the enemy they cannot that's perfect the only drawback is that we don't really want these to be visible to the player like this the best quick fix is within the safe zone sprite set ghost effect to 100 right at the start up here now if we'd hidden the sprite with a hide that would prevent the enemies from touching it and they would then just walk straight through it but using ghost to fade them away to nothing actually keeps them fully touchable which is very useful to know indeed so test that again and yeah that looks good we have our enemies moving just as we want it so finally before we finish there's one more issue to address come with me to a scene with a moving door i'm going to place a new enemy on the screen to the right of this door stop the project make the enemy visible position them and then in the when i receive change scene script we need a new check for scene 4. i only want to spawn a single enemy so remove all the rest of the spawns fill out the x and y positions from the stage right so what's going to be the problem with this any ideas are you watching ah they can walk through doors or more specifically they can walk through moving platforms so we have another sprite we need to check for enemy collisions against yeah back in the enemy sprite find the move left or right script and introduce a touching platform block we need the or operator again as the enemy is blocked if it is touching a safe zone or a platform so pop that in and here we go yes the enemy is now behaving less like a red ghoul and more like the nice chomping enemy that we wanted that's great to see i'll just go grab the key to this door and now we can let ourselves in [Music] if i wait for this guy i can let them through the door too oh whoops looks like they got caught i guess at this point we could do some extra checks and set them to get squished but i'm not overly bothered by this little quirk maybe we'll revisit this in a later episode too because well this is all we have time for today i only covered one of so many possible enemy types and behaviors but i hope you might try to add all sorts of enemies to your platformers now yourselves when you do please submit them to the studio linked under this video i'm really looking forward to seeing them i'm dead keen to make a video showcasing them but since there's so many i'll be opening first dibs to my channel members so watch out for a member-only post about it in the coming weeks and keep up the great work i hope if you enjoy this video you'll smash the like button and don't forget to subscribe so as not to miss my next exciting video if you are an educator or a super loyal fan then you might consider supporting me further by joining the channel membership there's added perks like channel emoji priority comments early access to videos and for those that want them access to the scratch projects themselves but until next time have a great week ahead and scratch on guys [Applause]
Info
Channel: griffpatch
Views: 448,031
Rating: undefined out of 5
Keywords: scratch coding, scratch programming, scratch game, Enemy AI, scratch ai enemy, scratch 3, griffpatch, scratch coding games, scratch, block coding, Splash Screen, scratch tutorial, how to make a game in scratch, how to make game in scratch, scratch tutorials, scratch games, scratch enemies, Enemy, scratch enemy ai, how to scratch
Id: Qh0yz_qPQeA
Channel Id: undefined
Length: 32min 8sec (1928 seconds)
Published: Thu Dec 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.