 Welcome back to the Letty Bird tutorial. This is part two. If you have to part one, there's card in the top hand corner. Go check out now. Currently in our project we can illuminate specific columns and have a gap here. We call these obstacles and we can also move the position of our Letty Bird up and down the first column here. There's a few more things that we need to do. We need to send our obstacles across the micro bit from right to left. We also need to add a score here so we can increment by one when we pass through a gap. We also need to have some hit detection so when the bird and the obstacles overlap on the same LED we need to end the game. Let's go start with moving our obstacles from right to left. To do this we need to create a new list and we're going to call it obstacles. Just going to go full screen for a moment to describe how this is going to work. We're going to have five items for our obstacles list and each item is going to represent a column here so 1, 2, 3, 4, 5 and the value of each column is going to be negative one if we do not want a column illuminated. So that's just here where all of our columns are not illuminated but if we have a value of three in item five of our list well we want that obstacle illuminated so that will refer to the row the gap that we don't want and that'll look something like this. See here that we've got the third row that isn't illuminated. The idea is then we can pass that three down the list. You can see here that column four now contains the three and that's represented here on the simulator. That row three of column four has the gap. So we can just keep sending this three down to one and then delete it when it gets to the end. Let's go code this. Currently our obstacles have no items and just like we did for our leads let's go ahead and create a custom block to reset our obstacles. First we want to delete all the obstacles in the list. Repeat five times adding negative one to our obstacles. Let's click it. Okay now we want to add some obstacles to this list make new block call it add obstacle. We now need to pick a random row between one and five to add to our list. To do that head over to operators and grab the pick random block. Going to pick random between one and five then grab the add block and we're adding two obstacles and you want to put that pick random block inside the add block. When we click this you'll see that to the end of our list here we've got the number three added but we also need to remove the first item because there is no column six. So we need to delete the first item of obstacles. So if I click this now we get the three in the fifth column. If I click it a few times we can see that random numbers are being populated in our list but we're no longer getting any negative ones. We could put negative one in here but that means we'll also get some zeros and we could have long phases of no obstacles at all. So let's make it a little bit more regulated. To do this we'll need a new variable and we're going to call it obstacle delay. When the green flag is clicked we want to set obstacle delay to zero. Our obstacle delay is going to work like a counter. If our counter is equal to a certain number then let's add an obstacle. I've gone for two here so if our obstacle delay is equal to two then we want add an obstacle. Grab an if then else block and slot that boolean block into the if statement and drag out that delete block right down to the end. So what this will do now if obstacle delay is equal to then we're going to add an obstacle to the list. We also want to delete it for every scenario. If obstacle delay is not equal to two well we no longer want to add a random number we just want to add negative one to the list. Hold your horses Freddy this ain't going to work just yet because our obstacle delay is always set to zero. We need to go and change that. So we need to change the obstacle delay by one every time we call add obstacle and if it is equal to two then we want to reset obstacle delay back to zero. All right I'm going to press add obstacle a few times. Let's watch the obstacle delay and the items that go into our list. So pressing it once obstacle delay is one we've added a no column here we go again no column and boom we've got the third row that we're going to blank out for our obstacle. All right I'm getting itchy I want to see these obstacles displayed on the mark bit. Go find that draw leds function that we created in the last tutorial and we're also going to be using that block that we created in the last tutorial the illuminate column block. We're going to need five of these blocks go ahead and put the numbers from five down to one in the column number because this is going to represent every single column that we've got. Go ahead and find the item number of obstacles and we're going to use this reporter block and stick it inside our row gap duplicate this reporter block for each column and connect these up to our draw leds function. So if we look here at our obstacles list the third item which has a value of five refers to the fifth row of the third column and that means it will have a gap for that obstacle. We've got negative ones we don't want to illuminate that. We're not currently handling this in our illuminate column function let's go change it. Here I am back at the illuminate column number function what we need to do now is when this row gap is equal to negative one we don't actually want to do anything here. So basically when the row number is not equal to negative one then we want to go about our business. We're going to need an if block for our boolean block to find a little home and there we go. I've just dragged out this render block and I'm looking at the micro bit and once we click it we should see the bird appear and also our obstacle. So you can see that the fifth row is not illuminated of the third column. Just got this repeat block out here we're going to add an obstacle and render the leads 50 times. We're going to press it and check out what happens on the micro bit. Okay it's gone really quickly so what we're going to do is add in a way block and let's wait for half a second before we add in an obstacle and that's much better. So this is going to form the basis of our Lady Bird game. You would have noticed that our obstacles were colliding with our bird so it's time for some hit detection. We detect a collision in Lady Bird if the index of an obstacle overlaps with the index of the bird. So if this obstacle were to move into column one we wouldn't have a collision but if our Lady Bird was in position six we would expect a collision to occur. There's a small problem with our code because the bird position will always be illuminated because we add it to our list of leads. So what we need to do is create another list and this would be called a hit list. Now this will mimic all the obstacles that are currently illuminated in the leads but it will be separate from the leads. So that way we can see if an index of the hit list overlaps with the bird position. Create a new list and call it hit list. Go ahead and find the reset leads and the illuminate column functions. In the reset leads function go ahead and delete all the items of our hit list and we also want to add zeros to our hit list. In the illuminate column function all you want to do is duplicate this replace block, this replace item and instead of leads we want to replace the hit list. Cool now that we have an invisible copy of our obstacles we're ready to do some hit detection. We're going to need these three blocks to detect our collision. The way this is going to work we're going to check our bird position which is currently one and we're also going to check this position of the hit list. So currently this will say item one of the hit list. Now if that item is equal to one it means it's turned on and if it's turned on well our bird is in that position as well so there's a collision. So if our bird position of the hit list is equal to one we've got a collision. If I move the bird position to six and click this it's false. What I'm going to do is make a variable then called game over with a question mark and this is just going to be a boolean zero meaning no and one meaning yes. We can set our game over variable to this condition. We're going to put this block inside a custom block called detect collision. Make sure you leave run without screen refresh unchecked for our detect collision function. Now we want to do this forever but it will stop and it's going to stop if it's game over. That's when game over is equal to one. We're going to stop this script. We can't call this function directly because we'll never get past this forever loop so we need an event to help us out. Drag out a when I receive event block and create a new event called start collision detection then grab that detect collision block and attach it to it. Oh we're getting so close now. Now that we have this little neat game over condition let's duplicate it and stick it inside a repeat until block. We don't need to repeat arbitrarily for 50 times now we can repeat until the game is over. This repeat until code is a great candidate for a start game custom block. Go ahead and make the start game block and connect that repeat until code to it. So just before we jump into the repeat block let's broadcast the start collision detection and start checking for our collision. We also want to do a couple of other things when we start the game set the burr position to 11 set the obstacle delay to zero or on the obstacles we want to reset them to we want to reset the lead and finally render. There's also a slight chance that game over could be set to true so we're just going to wait a tenth of a second before we get into the game. Okay that was quite a bit I've got the micro bit open here let's go ahead and press start game and give it a little spin. There is our lady bird at position 11 let's go to the top to avoid the first obstacle we can go down and get to the next obstacle and let's let this one collide with us and boom this group stops game over. We need to add a score variable. We need to set score to zero when we start the game and the best place to increment our score is in here that I'm just going to jump over to the simulator to talk about it. Now the best time to increment the score is when there is an obstacle in the first column we're already checking for the condition when we collide with the obstacle but if there's an obstacle in column one and we're here we're not colliding with it so we may as well increment the score. The way we'll do this in code is by checking the first item of the obstacles list. If the first item is not equal to negative one well then there's an obstacle in place and if there's an obstacle in place and we're not colliding with it well let's update the score by one. We can create that logic with these blocks down here so if the item one of obstacles is equal to negative one well we don't want to do that so when it's not equal to negative one we want to change the score by one we can get all this and wrap it inside of an if statement just like that we get this if statement and wrap it inside a custom block called update score. Now that we have that update score function we can put that in before we add a new obstacle. Okay let's give that a spin on our microbit. Let's hit start game and check out the stage and when I pass through an obstacle you'll start to see the score update. We're on two boom and now game over. Create this custom block here called end game. We want to end the game when it's game over. I'd like to do two things in end game. I'd like to give the user some feedback the game has ended. The first thing I want to do is display a sad face on the microbit. I've just created an extra list here called sad face check the card in the top corner to see how I did that. Okay let's wait half a second and then we want to display the text score then we'll wait a second and clear the display. One of the last things we need to do is set up the conditions to start our game because I keep pressing it at the moment. Find the when the green flag is clicked hat block and let's get rid of these two set blocks. I reckon we should get our game started when these two buttons are pressed and it's game over. So when the A and the B button are pressed simultaneously and it's game over. Wrap that inside of an if block wrap that if block inside of a forever block connect that up to our hat block and drag out the start game and if you want to you can display a countdown before the game begins. Okay I'm just going to demonstrate our game in action here. I'm going to press the A and B button. We're counting down and we're away. All right let's get to the top of that obstacle and down we go all the way down the bottom. Oh yeah that one's easy. Am I going to make it to the top? I'm just going to die. There's our sad face and the score of four. Game over. Yeehaw you made it to the end. You are awesome. Great job on sticking through the tutorial. I'm going to cut it short there but there's a couple of other things that we could do. The first is that you can update the speed of the obstacles as the game progresses so as the score gets higher and there's also a bug when we move the bird where you can move the bird after a game over. Drop a comment down below if you want me to make a video on these features and bug fixes. It's time for a scratchy question and I want to know do you prefer long tutorials like these to be chopped up into multiple parts or would you prefer one long video? Share your answer in the comment section below. Thanks for checking out this Flappy Bird tutorial. Like, subscribe and ring the bell if you're new around here and have a scout something out of the content on your screen right now. Show your support for Surfing Scratcher by checking out exclusive content on my Patreon page, my funky red bubble cheese, all by joining the mailing list. All links below in the description. But until then, I'm off to go find a way. I'll catch you in the next one.