Code a Fun Space Shooter Game 🚀 | 1. Move & Shoot | Scratch Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello fellow lemons welcome to another mix it up monday tutorial i'm griff patch and today we are going to start a little project based loosely around the classic game asteroids but well far more over the top as you can see lemons have achieved interstellar capability and are attempting to take over the universe to tackle such a scenario we obviously need to fight back with rapid fire lemon bursting lasers so what's the coolest thing we're going to learn while making this game that would be how to handle loads of rapidly moving projectile clones on enemies and how to robustly handle the collisions between them as a bonus we'll also tack on some cool special effects including damage and explosion flashes and the much sought-after camera shake today we're going to cover the basic player design movement and shooting of laser projectiles towards the mouse i'm also interested to know how many times i can say the word lemon in one episode lemon lemon lemon were you counting okay let's get scratching we'll begin with the player spaceship sprite i'll politely discard my pal scratch cat and create a new sprite in its place we'll need to draw a ship that faces to the right that's the direction we almost always need to make our sprites face i often forget and end up drawing it facing up so we can use a rectangle holding the shift as we draw it to perfectly keep it as a square then switching to the shape tool select one corner node and click delete to make a perfect triangle now using the selection tool again i can rotate the triangle but again holding the shift key while dragging it so as to rotate by exactly 45 degrees okay next trick grab the top resize node and hold down the alt as you drag down this resizes the object around the middle very useful next i drag the triangle until it snaps to the center of the canvas it's important to do this so that the ship will rotate beautifully around its middle point but then once centered i then drag it to the right a little bit again while holding the shift key this lets it move without it allowing to go up and down so it stays snapped to the middle in the vertical okay and there's more tricks yet hold down the alt key again and then left click and drag the triangle to create a perfect duplicate but while still dragging hold down the shift key and power the shape is snapping to stay lined up with the previous shape i'll color it lemon yellow this ship is already looking cool i think i'll drop the border thickness down a touch right i didn't name the sprite so let's name it now player next we can make the background nice and dark by switching to the stage i'll convert it to a bitmap and fill the entire screen with a color that's almost but not quite black okay back to the player sprite and let's fill in some code it's going to be a basic game loop this time we'll set it up with a when green flag clicked bring the player to the front of all the other sprites and go to the center of the screen we'll want our ship to have smooth movement so we'll keep track of its momentum that is its speed make two new variables named speedx for this sprite only and speed y also for this sprite only and will be very good and set them both to zero before the game gets going always good to reset your variables like this at the start of the game now for the game loop bring in a forever loop so what shall we do first how about left and right movement we can do this in so many ways but the pattern is always very similar i'm going to make a new custom block naming it move x and adding in a numeric input of joystick x now people ask why do i use so many of these custom blocks the truth is you don't always need to but one huge advantage is that they break up your code into nice chunks and they give each chunk a rather nice name that can be a lifesaver when you come back to a project later and want to find something it's just so much more readable we'll use the move x block right away and i'm going to use the same key press trick we did in some other tutorials of putting the key press blocks inside a subtract operator like this either we can have right arrow press subtract left arrow pressed or as i'm going to do now key d pressed subtract key a pressed that's the was keys this beautiful little trick gives us a minus one for left a zero for no movement and a one for right that's really useful okay so the joystick x input here will now as we just said b minus one for left or one for right let's use this to change the player's speed x variable specifically by 0.9 multiplied by joystick x now the 0.9 here is the player's acceleration feel free to create a variable to hold the value if you prefer but i'm just being quick today next up we set speed x yeah not change this time set set speed x to 0.9 multiplied by speed x this is not the same 0.9 as the one above this one is for air resistance to slow the player back down with the speed variable now updated we can change x by the speed x to get the player moving on stage let's give it a quick test run the project and press the left and right keys whichever you assigned there that's a nice feel cool we can do the same now for up and down make a new custom block move y with an input of joystick y this code will be just the same as for the left and right change speed y by 0.9 multiplied by joystick y and set speed y to 0.9 times by speed y and finally change y by speed y we can pop the new move y block into the forever loop and pass in the key w or up subtract key s or down let's test that again to check we have full movement now yeah beautiful all we need now is to rotate to point towards the mouse cursor this is going to be quite a fast paced game so i'm not going to limit the speed of turning so it's really easy just drop in a point towards mouse pointer and we're done yeah i like that a lot now we are facing the right direction we need to be able to fire those lemon bursting lasers make a new sprite naming it laser we can start by zooming in and drawing a long rectangle remembering to snap it to the center of the canvas again i think a fill color of white but with a lemon yellow outline looks neat next i'll round the ends of the laser beam with the shape tool just click to create new nodes on the top bottom and mid-right edges like this now we can click on the corner nodes and press the delete key to remove them leaving a not half bad rounded end do the same for the left end to finish it off i'm making the border a bit thicker size two and now to ensure the laser is correctly drawn i'm going to draw a completely transparent rectangle around the entire shape this is not crucial but it helps to ensure the laser's edges are nice and crisp and don't get cut off weirdly i'm sure the scratch bug will get fixed one day okay let's put some scripts together beginning with the initializing scripts when green flag clicked hide the laser only wants to be visible when cloned the original sprite should be hidden and in fact will never be used as a laser sprite at all this is common practice in scratch in fact if you do try to use the main sprite you can get into trouble because unlike all the clone sprites the delete clone will not work on the main sprite that can lead to bugs where one bullet suddenly appears on the screen and won't disappear like the others so yeah hide the main laser sprite we're going to hang all our game loops off of when i start as clone hat block this is very convenient for less complex games like this one as soon as the laser is cloned position it at the player's sprite location we are going to want to send it flying out in the direction the player is facing to match the player's direction we use a point in direction block yeah don't use the point towards player that won't work we could have used the point towards mouse that would surely work as we are present always facing towards the mouse but that may not be the case in the future so point in direction and then find the of block in the sensing category and switch the sprite name to be player now you can select the direction of player neat okay we need to show the cloned laser sprite and then we'll begin a repeat until loop repeating until the laser is touching the edge of the stage within the repeat drop in a move block and i'll change it to be 11 steps the bigger the number the faster the laser will travel so have a play with that this loop will finish when we hit the edge of the stage so then delete this clone to remove it from the game that all sounds good we just need a way to trigger the laser to fire now we may be tempted to use a when space key pressed hat block then we simply can create a clone of self now which of you knows why this is not such a great idea let's give it a test i can press space and a single laser fires out on first inspection this is working very nicely so the cloning and laser movement scripts are great but because i have scratch add-ons installed i can see how many clones are being used as i launch each laser and look at that how is it possible that i'm using all 300 clones at once with so few lasers showing up on screen any ideas okay let me explain this is a super common bug for scratchers who are new to using clones look back at where we added the crate clone block here it is whenever the space key is pressed we create a clone of ourselves seems harmless until you consider that this is being triggered on the laser sprite now tell me how many laser sprites are there at once well there's one sprite to begin with but as soon as i press the space we clone the sprite and then there are two laser sprites so when i then press the space again to fire the next bullet not just one laser sprite will trigger a space press script but two and both sprites the original and the clone will both create new bullet clones of their own of course because the clone creating script places the new bullets at the player all these new bullets appear on top of each other so we only see one even though there are two so that makes four bullet sprites even though we've only pressed space twice so now can you guess what happens if i press space a third time you guessed it four bullets becomes eight bullets and then 16 32 64 128 256 and pal we've exceeded the 300 clone limit it doesn't take long before all the clones are used up disaster because we'll need to keep all these clones available for all those lemons so the mistake we made was to have a key pressed on a sprite that is being cloned always remember if a sprite is cloned that all the clones will be receiving the keypress event ok much easier to trigger the clone event from the player sprite instead make sure to delete this when space key pressed hadlock click into the player sprite we'll make a new custom block named shoot and we'll use it right after pointing towards the mouse pointer down here now i don't want to trigger off the when key pressed because i don't fancy the player having to mash the space key that much instead i want a steady stream of lasers to fire out as soon as the key has held down so add in an if space key pressed or mouse down we might as well allow the mouse to fire too and then we'll use the create clone of and switch it to clone the laser sprite great shall we give that another test mash the green flag oh man yes look at that that's insane firepower lemons don't stand a chance against that yeah so i think we need to rein that back in a touch limit the rate of fire make a new variable to keep track of this naming it fire rate for this sprite only we need an if else block and check if the fire rate variable is larger than zero oh move the key pressed check into the l side we only want to allow firing of the laser when 5 8 has decreased to zero so yeah if the fire rate is greater than zero then change the fire rate by negative one then after creating a new laser set fire rate to 10 this will now not allow the laser to fire again until it has decreased back down to zero let's give that a test oh gosh that feels slow now a regular pulse of just three laser blasts per second that could work for early levels perhaps but it's not going to be a match for the number of lemons i have in mind no i'm counting on there being quite a lot of lemons indeed so perhaps let's try a setting 5 8 to 1. okay that's nice it's perhaps a little quick but i kind of like it it's a nice compromise and i'm feeling it will give us perhaps some chance of warding off even a somewhat excitable lemon invasion right guys this is the end of part one of this tutorial if you want any game assets then i've included a link to a scratch project in the info under this video i think you should take the time though to have fun and make the spaceship costumes yourself but there's also some cool sound effects in there for you to grab so have fun the next episode we'll build on this tutorial and we'll finally get those lemons into the game lots and lots of lemons yeah and that is where the fun really will begin but i hope you've enjoyed this video thus far please do smash the like button and don't forget to subscribe to the channel to avoid missing my next exciting video thank you so much to my new channel members joining the membership is a huge deal because simply put more supporters translate to me being able to make more videos it's that simple that's right you can help make this channel awesome oh and there are some fantastic perks up for grabs too so check that out and that's it thanks for watching and scratch on guys
Info
Channel: griffpatch
Views: 819,982
Rating: undefined out of 5
Keywords: Asteroids scratch, asteroids game, asteroids scratch, coding for kids, griffpatch, how to make a game in scratch, how to make a shooter game in scratch, how to make scratch, lemonoids, programming, retro gaming, scratch, scratch 3, scratch asteroids, scratch coding, scratch coding games, scratch game, scratch game tutorial, scratch programming, scratch shooter, scratch shooting, scratch space game, scratch tutorial, scratch tutorial game, scratch tutorials, shooter game
Id: sqsNb0s7Oq4
Channel Id: undefined
Length: 16min 32sec (992 seconds)
Published: Mon Jun 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.