Tile Scrolling Platformer | 8. Enemy Clones

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello fellow scratchers have your levels been feeling rather bereft of life well it's time to liven things up as we add adorable yet temptingly squishable enemies into our platformer i'm fifth patch and welcome back to part 8 of our series on creating a tiled scrolling platformer in scratch before diving into the code for those at the cutting edge following the tutorial as i'm sharing it you may not have enemy sprites and costumes in your project well do not fear i have updated my starter project on my griff patch tutor account with the new sprite and costumes and there is of course a link to it under the video if i jump there now you can see the new enemy sprite here contains a whole raft of exciting mario enemy costumes so drag the whole sprite into your backpack and then we are ready to begin so open your project from where we left off in episode 7 and drag the enemy sprite out of the backpack into the sprite panel i'm going to reorder it to be the third sprite now if you're creating the sprite yourself then that's fine too it only contains costumes after all in this episode we are going to create the classic mario enemy goomba you'll need to be able to fall and move around colliding with the tiled level click into the mario sprite we put a lot of time into making these scripts and we certainly don't want to have to do it all over again for goomba luckily many of our scripts are perfectly reusable for those of you lucky enough to have my scratch browser extension installed scratch dev tools you can use the find bar to locate the scripts there's a link under the video but otherwise let's find them by hand firstly we want the define get tile xy script this is key to detecting collisions so drag it carefully until your mouse cursor is directly over the enemy sprite and drop it there next find the define fix collisions at point script we'll drag that over to being careful to let go directly over the enemy's sprite it can be quite tricky to see behind these huge scripts then we want the define fix collisions in direction script man i'm glad we can reuse all these and lastly we want the define move spritex script and the define movesprite y script awesome that covers a lot of the required sprite movement so let's click back into the enemy sprite and observe the dog's dinner scratch is made of laying out our copied scripts not the best so right click the scripting area and clean up the blocks there at least now they're readable let's just check they're all here we have the get tile at xy fix collisions at point fix collision in direction and move sprite x and move sprite y that's all of them great and look it's worth noting that scratch has kindly recreated all the variables that were used by these scripts specific to mario that's going to be very handy so we can get right down to business we intend to have many enemies on a level all at the same time so we of course will be using clones we therefore have to set things up with that in mind when green flag clicked set size to 200 as we have been doing throughout and then hide this sprite this original sprite will be a dummy sprite from which all our other clones will be made now to identify this dummy sprite and what sort of enemy each other clone will be make a new variable named type for this sprite only and set it to the empty value this indicates that this is the dummy enemy we'll also set both speed x and speed y to zero now looking ahead to position goomba on the screen we'll need a new custom block named paint sprite run without screen refresh we code this in the usual way by first switching the costume too big to allow it to move off screen position the sprite using the go to xy of x minus camera x and y minus camera y then we'll switch the costume to the correct costume which we don't yet know so make a new variable named costume to hold the costume name or number then we can switch costume to that variable to trigger this paint sprite use a when i receive position tiles but to rule out the dummy sprite check if type is greater than the empty value only then do we paint the sprite oh man this is exciting i really want to test things but we haven't yet cloned any goombas now obviously we are going to want to work towards adding the goombas through the level editor but we are better off starting simple we'll add a when g key pressed g is obviously for gumba and then if type equals the empty value then create a clone of myself this is an important if condition because we only want the dummy enemy sprite to spawn the new clones this g key press will always trigger for each clone on the enemy sprite 2 and they would be quite happy to create their own clones this leads to the common bug where clones create new clones and wow do they quickly multiply out of control this if will stop all of that so when i start as a clone set the clone's type to goomba also set the costume to 2. this is goomba's first costume number then set width and height to 14 so that the collision scripts we copied over from mario know how large goomba should be for now we'll position this new goomba right at the same location as mario simply set x to the x of mario the off block is in the sensing category do make sure not to use x position of mario but just the plane x of mario instead we do the same for the y variable and bring in a point in direction direction of mario so that goomba faces the same way as us finish the clone off by using the paint sprite to set its position and costume and then use a show block oh thank goodness the moment has arrived we can run the project without waiting let's smash that g key to khloe nagumba and yay there he is right on cue let's drop a few more this is fun notice how the goombas can be happily scrolled off screen and when we return they are waiting for us exactly where we left them it's neat at least it's neat if you like legions of floating goombas personally i favor my goombas to be a little more lively perhaps we should proceed to get them to obey the law of gravity now before we can bring movement to our enemies our main mario game loop is in need of a minor upgrade click into the mario sprite and find the define game loop script now you're going to like this a number of people have asked how come my scratch has some extra features that you don't see in regular scratch well that's because i have the scratch add-ons and my scratch devtools extension installed and here's an option i added just two days ago that lets me add extra space around a script that i'm about to work on however just to be clear you do not need these tools to follow or complete any of my tutorials okay so we are going to create two new broadcast events the first we'll name move enemy and the second move player after enemy drop these in after the broadcast move player and before position tiles the order of these broadcasts is crucial for our program to run correctly this lets us move the player then move all the enemies and then perform more player movements and checks all in the same frame of our gameplay but across multiple sprites this was a real game changer when i learnt this trick we will use it now to split off the camera movement and final sprite positioning of the player and place it under the new when i receive new player after enemy like this enemies can now safely run their scripts before the level is scrolled just as the player already does this will help no end when we want to do sprite collisions later on i'll just test that things are still working after those game loop changes good let's switch back to the enemy sprite now and we'll look at making goomba fall to the ground find some free space and then drop in a new when i receive move enemy event receiver this sprite may or may not be a goomba so let's check if type equals goomba now make a new custom block naming it tick goomba run without screen refresh and drop the new block in the if followed by a stop this script so gravity we know what to do there change speed y by minus -1 and then drop in our very own move sprite y block we can do that you betcha we can run the project and press g oh wow did you see what i saw i think you might have missed it hold on let me get more height oh yeah these goombas are hugging the floor like little floor hugging floor huggers that is most satisfying to see and a testament to reusable code so can goomba walking be as simple we want those little critters to have a bit of acceleration to their walk we'll assume that they are facing the direction they want to walk in so using an if else check if direction is greater than zero facing right if goomba's speed x is less than one one will be his top speed yep goomba is dog slow but they're cool don't worry then we change speed x by 0.1 otherwise they must be facing left so if speed x is greater than minus 1 negative 1 then change speed x by negative 0.1 nice now bring in our move sprite x block to handle the movement and tile collisions for us oh yes we can run the project again pressing g to drop a goomba and off he toddles if i drop another one while facing left he goes off that way too so what if i drop one by a wall well he stops that's not quite right but he does look good falling off a cliff i like that hold on a sec i just noticed my mario sprite is behind the goomer sprite we can fix that at any time by going back into the project editor and dragging mario on the stage good so let's ensure that goomba does not get stuck while walking into walls instead we want him to change direction click back into the enemy's sprite and locate the define move sprite x we're going to have to make a minor change to this copied script specific to the enemy sprite this is where we have detected a collision where a solid is greater than zero we can simply add a set turn clockwise by 180 after setting the speed x to zero this will turn goomba right around testing this simple change i'll run over here and spawn a gumba expecting them to now turn around at the wall yup perfect and look all his friends want to join in the fun too i'm not surprised next up we will work on goomba's walking animation find the define tick goomba script and make a new variable named frame for this sprite only after the goomba moving code we change frame by 0.25 that will mean it takes 4 frames to change costume umber only has two frames in his walk cycle set the costume variable to two goomba's first costume is number two and then add the floor of frame mod 2. the mod 2 keeps us within goomba's two frames of animation and the floor rounds down to the whole number run the project and we can confirm that it's looking good yep goomba is doing the goomba shuffle excellent but you know one better than the goomba shuffle is the goomba squish yep it's time to get mario some exercise and jump on a few heads switch to the mario sprite and find the when i receive move player script before we move mario right at the start of a game frame we want to check whether mario is primed to squish enemies in general this is when we are moving downwards add in an if else block and check if falling is greater than two that occurs as soon as we leave the ground and also then check that speed y is less than negative one this is triggering on downward movements only make a new variable named bop y for this sprite only and set bot y to y otherwise if we are not falling down set bop y to the empty value a quick test shows how bop y is set only when we are traveling downwards now so why do we set this at the start of the game frame before mario moves the reason is that after mario falls down it's possible that we will actually impact the ground before we check the collisions with goomba we really don't want that so with that setup switch back into the enemy sprite and scroll to the bottom of the define tick goomba script now we check if goomba is touching mario we must then decide if it is mario or goomba who is to lose a life add in an if else and check if bop y of mario is greater than the empty value this represents a mario win so goomba should be squished start by pointing in direction 90. set frame to 0 and i'll set type to squish all squished enemies will be identified as such now set the costume variable to the words goomba squish we can look up this costume name in the costumes list and just copy it from there to make sure it's exactly right okay we are not going to handle mario losing a life in this episode so let's just leave the else empty for now we'll test the scripts as they are drop a goomba and jump squish excellent and now a few more to squish squish squish squish working great i'm leaving rather a trail of destruction behind me these goomba remains should not be left to linger this long move over to the when i receive move enemy script and if type equals squish then we change frame by one and then once frame is greater than 10 then delete this clone we can test this and when we dispatch a goomba he now only hangs around for a very short period of time before leaving this world for a better place to finish off this episode we should get mario to bounce off goomba's head that gives the player a nice feedback that we've pulled off a successful move back in the define tick goomba script we can create a new variable named bounce player for all sprites and when we squish goomba we also set bounce player to five we're going to use this to automatically simulate the player jumping for 5 frames after hitting the goomba now click into the mario sprite and under the when i receive move player after enemy we insert an if before the move camera block check if player bounce is greater than zero if it is then we change bounce by -1 and then trigger a jump by firstly setting speed y to 13 also we set falling to 2 so that we're counted as off the ground and then lastly set jumping to 1. keeping the jumping variable low will allow us to continue jumping up higher if we hold the jump key down after bouncing on goomba's head right that's it we're ready to test run the project drop a goomba and jump on his head yep did you see us bounce cool how about jumping high off a goomba woohoo that was higher hold on let me find somewhere i can test this extra height yeah this platform is too high for a standard jump but now if i spawn a goomba and then jumping on his head now brings the platform into reach that's great it opens up exciting level design especially for reaching secret passages well things are looking great we've managed to spawn many simple enemies in our game but we still have some work ahead of us in the next exciting episode we will work on bringing the enemies into the level editor and hopefully how to also add them to our save codes we also really need to balance things out for poor goomba and allow mario to lose a life too so please do keep me updated with how your projects are coming along and what features you are most looking forward to covering in future episodes if you've enjoyed this tutorial then please smash the like button and don't forget to subscribe to the channel to avoid missing my next exciting video thank you for watching and scratch on guys you
Info
Channel: griffpatch
Views: 54,984
Rating: undefined out of 5
Keywords:
Id: Lzdz-lVQpcQ
Channel Id: undefined
Length: 18min 15sec (1095 seconds)
Published: Mon Apr 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.