The focus of today’s lesson is to show you how to add the Knights attack function, defense function, and defense break function. I will explain all of these functions/moves as I code them.
First up we have the attack function. This function is pretty simple but you have to get your sprite. I chose a sword that looked like it could have been used by a medieval knight.
Unfortunately as the sword moves I could not get a picture of it and its normal size but I was able to get a picture of its code file that showed what it looks like. Now that you have your sprite it is time to give it all of its code. This code is definitely more complicated then previous codes in this game but it shouldn’t be a problem.
The first piece of code we have to add is the size and position of the sprite as well as how we duplicate it when we press a certain key on our keyboards.
I personally chose the k key on my keyboard to duplicate the sword however you can choose whichever key you like. I also set the size and position of the sprite based on my needs you can adjust these however you would like. Now that we have this step done when you press the green flag your sword should be on top of the knight and invisible and when you press k it should shoot out an invisible clone which we will obviously have to make visible. Also keep in mind the sound is optional but I decided to add a sound that I thought sounded like someone unsheathing a sword.
The next step is to add code to the clone because all we tell it right now aside from creating itself, is to start a sound, and wait 1.5 seconds. So to add a couple of commands specific to the clone we will need a special block that is called “when I start as a clone” and I will explain how it is used.
Once again this code may look complicated but it really is not too difficult. You probably see some new blocks such as the: point towards block , touching ? block, and the change variable block. But all these blocks will make sense as you get into the next couple of steps for this game. But now when our clone starts out it shows itself after which it glides to where the warlock is. If it hits him then it will change his health variable by -1 and it will wait 0.5 seconds then it will be deleted. But if the warlock uses his block function called the magic shield the blade will be neutralized instantly and it will not affect the Warlocks health.
Now that we have our attack function we can play the game. But it will be pretty basic as the Warlock will not even be able to attack. All he will be able to do is dodge. But we have one final step and this step is a sort of fail safe just in case our sword doesn’t delete itself which would be a problem later in our game.
This code is extremely simple but it is a fail safe in case our “delete this clone code” does not work . In this case this block automatically sends the delete message which then allows these code blocks to activate and they immediately delete the sword.
That’s going to be it for this step of the 2 player battle simulator thank you all for reading my blog and I hope it has helped you so far.