 If blocks, repeat blocks and clone blocks are you here because you're wondering how to use those scratch control blocks? We'll stick around because in this scratch games tutorial for beginners We'll be making a super simple shooter game that will use all these blocks coming up Hello world serving scratchy here teacher surfer programmer bringing you the goodness of learning to code through video tutorials If it sounds like something that you're into then consider hitting that subscribe button Check out the show notes below biking that show more button as a link to a starter project that you'll need for this tutorial All right. How about we gain some control over those blocks? All right, so I click the green flag and it shows that we're in space There's a laser down the bottom here that shoots some laser beams now Our job is to hit these space objects while avoiding these asteroids that are being flung down of us in asteroid collectors Well game over let's jump inside first thing you'll need is to create these space objects And if you can't be bothered doing that then check out the starter project in the description below Bit of housekeeping the only thing that we want to split on the screen is our laser So I'm going to go ahead and hide these other four sprites here rather than using code to do that I'm just going to flick the hidden switch to signal the start of our game all these stars appear on the screen So let's jump over and do that hit into your star sprite jump over to the events category first and grab out At when the green flag is click block now. Let's start exploring the control category We want heaps of stars to come out on the screen So what we're going to do is uses the repeat block and wonder repeat block does is it executes all these instructions that are inside Of it for this number of times. So if I connect it to this green flag click and I press it It will repeat nothing 10 times. Let's do something a little bit more interesting Let's repeat and grab our next control block creating a clone of itself So what this does is it's going to create a clone of the star a copy by click the green flag and nothing happens Still we need the next control block. So when we start as a clone So when we create a clone we can signal for this right to start a sequence of instructions when the clone is created We don't see any clones on the screen. So let's go show it jump over to the looks and find the show block Press the green flag and you'll see that one dot appeared on the screen The reason for that is that all the stars are currently stacked on top of each other We need to change that jump over to the motion category drag out the stack lock that is go to a random position We'll click green flag and now we've got 10 stars that have appeared in our screen We're gonna update this value to 50 press the green flag and now we've got 50 stars in our screen All right, here. I am the laser sprite and I've just dragged out of when the green flag is clicked block What you want to do now is get our laser moving across the screen to do that We want to sense if the left or the right arrow has been pressed So we've got a control block that can help us out What this will do is if this boolean condition is met then we'll execute some code So what is this condition? We'll jump over into the sensing and drag out the key pressed boolean block and change it to a right arrow and then slot it inside the if statement there Basically if this is true then execute the code that's inside of this C block head over to the motion And just grab a move 10 steps and we're just about there You might be thinking if we connect this this will work, but it won't I'm pressing the right key and nothing's happening Why is that? This is a common issue for new scratches. This only executes when the green flag is clicked So what we need to do is wrap this in another control block and that block is a forever block a forever block It's just like the repeat block, but instead doing it 10 times it never ends I click the green flag now and I can move right, but I can't move left so what we need to do is right click on this if statement duplicate it and Put it below and instead of the right arrow Let's go to the left arrow and let's move back 10 steps click the green flag and boom jump over to your beam Sprite and head over to the events category and look for the when the space keys press if you don't see space there Just hit the drop down and select it What we want to do is we want to create a clone of the beam whenever we create a clone We want to do something when we start as a clone first thing we should do we start as a clone actually show ourselves So head over to looks and drag out the show block and hit the space bar and look there is our beam But it's no good over there. We need to place it where our laser is So also when we start as a clone head over to your motion and we want to not go to a random position But go to the position of the laser gonna press the space bar and boom We've got a beam coming out of our laser We need to now get this beam moving across the screen to do that head back over to the control category I'm gonna learn about our next control block the repeat until so remember that these repeat blocks they loop it just loops over and over again and Instead of doing it a set number of times. We're gonna repeat until this condition becomes true What is it that we want to repeat? We want to move our beam across the screen So let's change the y value by 10 a press the space bar now. You'll see our beam go across the screen I think we should move our beam across the screen until it hits the edge So jump across into the sensing and until we are touching the edge press the space bar and then Okay, after we've finished executing the script. We want to delete the clone So head back down into the control and find the block that deletes the clone Just click a green flag and I hit the space bar and you'll see our beam disappears as it hits the edge of the screen I'm now in the spaceship sprite And I've just dragged out another when the green flag is click block drag out a forever block and you also want to drag out Another create clone of the spaceship, but we don't want to do this immediately We're gonna learn about our next block and that is this weight stack block So we want to wait one second and then we will create a copy of the spaceship This can also be a decimal value drag out another when I start as a clone haplock And we also want to show the spaceship jump back over to your control category and drag out this gnarly Double C block here got another video that will explain this in a little bit more detail But I'll give you a snippet of it now think of this block as a light switch if the light switch is on then execute this code here Otherwise if it's off do this the reason we're gonna be using this double C block is because we want to decide whether to start the spaceship on the left side of the screen or on the right side of the screen so to do that Head over to your operators and find the reporter block that picks a random number We're gonna pick a random number between zero and one if I just click this block a few times it'll pop up with a value There we go. It's gone to one. It's gone to zero. It's gone to one one. There we go So what we're gonna do is we're gonna check this random number if it's equal to zero or one to do that We need to get the boolean block that has the equal sign Let's put that reporter block inside the start connect it all up now It's never gonna be equal to 50 we need to make it equal to zero or one What this says if this random number is equal to one then we're gonna start our spaceship on one side of the screen Otherwise, let's put it on the other head over your motion to find the set X block Okay, I'm gonna click the green flag and every second it's either gonna start on the right side of the screen or the left Let's get that spaceship moving with a glide to random position block and you'll see our spaceships now starts to take off But you'll see they're also hanging around the screen So we need to jump back into our control blocks and delete them after they've glided to that spot Press the green flag spaceships glide across the screen and disappear I'm actually just gonna scroll up and grab this code here right at the top Drag it across into the sprite pane on top of the asteroid and drop it I'm gonna go inside the asteroid and you'll see that that code is just being copied across to it The only thing I'm gonna change is one second to two seconds Go ahead and grab another when I start as a clone haplock. All right when the asteroid starts as a clone We're going to set its X value. So it's horizontal value to a random position We're gonna set it to a position between negative two hundred and two hundred It's gonna click the green flag here and reveal our asteroids You're gonna see some asteroids pop up here on the screen in random X positions Head on over to the sensing category and and find the reporter block that says distance to We're gonna go the distance to the laser But this block does is it draws a line from the laser to the asteroid and works out a value So if I press it currently it's around 296 now if I move the laser and I press this again You'll see the value has updated to 341 if I move the laser directly under the asteroid again, and I press this you'll see that it's around about 295 so just under 300 we want this asteroid hurtling towards the laser So what I'm thinking is when the distance is under 300 between these two then start moving the Asteroid in the direction of the laser don't drag out another block that we haven't talked about This is wait until block so we're gonna wait until the distance to the laser is under 300 head over to your operators and Grab a less than Boolean block and drag into that reporter block and change the value to 300 So what this is saying is we're going to wait until the distance of the straight line Between the asteroid and the laser is less than 300 So this is the flag to start moving our asteroid now Let's move the asteroid grab out another repeat until block We're gonna repeat until we touch the edge and we're going to change the y by negative 15 We're also going to turn clockwise by 15 degrees The last thing we want to do is delete the clone when we're finished So if I click the green flag and wait for an asteroid to appear if I move beneath it It starts to hurdle towards us The last thing we want to do for our asteroid is sense if the asteroid actually touches our laser So if our asteroid is touching the laser then we want to jump back into our control and stop everything So this will stop every single script in the project Including deleting all the clones that have been created and also just stop the scripts in this set of instructions Or you can stop the other scripts that are in this sprite We just need to tidy up a couple of aspects of our game First let's hide the asteroid when it starts as a clone And then after the distance is under 300 we need to show the asteroid head over to the spaceship sprite We need to detect when a beam has hit a spaceship when I start as a clone forever If touching beam wait 0.05 seconds Then you want to delete the clone press the green flag and now we should see our spaceships disappear when we hit Them some beams. Okay, the last thing we want to do is jump into our beam sprite and we want to Continue moving our beam until it hits the edge or it hits a spaceship because currently our beams can pass through spaceships If I just demonstrate this here the beams continue to go So jump into the operators grab an ore block duplicate this touching block And hit the drop down menu and find spaceship Grab that sensing boolean block and place it inside the ore boolean block and put them both Inside that conditional zoom out so you can see them both and there we go If I press the green flag now And we hit a spaceship The beam should also disappear and it does make sure you go ahead and you hide all the switches again Cool. Congratulations on making through the scratch control box tutorial You've now got a starter project a starter gang that you can build upon you can make lives You can create a timer. You can create a score with like some hits of these spaceships now It's sort of endless So I encourage you to go ahead and see if you can tinker with this game to create something awesome It's time for a scratchy question. Now a game isn't a game without some kind of score So how could you remix this project to include some kind of scoring system? Share a link to your remix project below in the comments or tell me how you go about it Hey, thanks for checking out this tutorial on scratch control blocks like subscribe ring that bell If you're new around here and have a scout some of the other content on your screen right now If you want to show your support for surfing scratcher, then you can check out my patreon page link below in the description But until then I'm off to go find a wave. I'll catch you in the next one