![]() ![]() ![]() The ground lift variable starts at 0 and changes by 1 each time the repeat until loop lifts the Cat sprite’s y position by 1. The code in the repeat until loop uses the ground lift variable to determine if the slope is a walkable slope or a wall that should block the Cat sprite’s progress. This makes the code more compact, because we can use the same script for moving the cat to the left (with the walk -6 block) or to the right (with the walk 6 block). In the define walk block, the steps input block is used in the change x by blocks. We want the cat to walk six units, as we did earlier, so we use -6 and 6 in the walking script when we call walk. An input is somewhat like a variable, but you can only use it in the custom block’s define block. Let’s call it walk and give this new custom block an input called steps. Instead of simply changing the x position by a particular value, we’ll use a new custom block. Now we need to edit the Cat sprite’s walking code and add some new code, too. So, right-click on the Scripts Area, and select clean up to reorganize the scripts into neat rows. To do this, we’ll make a small change to the walking code blocks.Īt this point, the sprites are becoming overcrowded with lots of different scripts. We want the steep slope to block the cat. But now the problem is that the Cat sprite can walk up the steep slope on the left as easily as it can walk up the gentle slope on the right. This is a big improvement for the player compared to just walking on the bottom of the Stage, as in the Basketball game. The Ground sprite has hills and slopes that the cat can walk on, and you can change the Ground sprite to pretty much any shape in the Paint Editor. If you’re lost, open platformer2.sb in the resources ZIP file and continue reading from this point. Then click the red stop sign and save your program. Now the Cat sprite should never sink into the ground. Make the cat walk around or use the mouse to drop the cat from the top of the Stage as before. Check the checkbox next to Run without screen refresh.Ĭlick the green flag to test the code so far. Name the block handle ground, and then click the gray Options triangle. Go to the More Blocks category, and click the Make a Block button. The dark purple custom blocks can help us do this. ![]() This code needs to run so fast that the player only sees the sprite on top of the ground, not in it. The big problem with the code right now is that the Cat sprite is lifted from inside the ground to on top of it very slowly. Click File Upload from your computer in the Scratch editor to load the file, and continue reading from this point. If your program isn’t working and you can’t figure out why, load the project file platformer1.sb2 from the resources ZIP file. This platformer has many scripts, so you might get lost if you get confused. If the Cat sprite walks off the edge of the Ground sprite and falls to the bottom of the Stage, the Cat sprite should reappear at the top. ![]() Press the A and D keys to make the cat walk up and down the slopes. Coding is a bit trickier, because we want the ground to have hills and eventually platforms!Ĭlick the green flag to test the code so far. The important difference is that in the platform game, the cat lands when it touches a ground sprite rather than the bottom of the Stage. In the first part, we’ll add gravity, falling, and landing code, similar to the Basketball game in Chapter 4. The skeleton project file has all the sprites already loaded, so you’ll only need to drag the code blocks into each sprite. Go to and download the ZIP file to your computer by right-clicking the link and selecting Save link as or Save target as. If you want to save time, you can start from the skeleton project file, named platformer-skeleton.sb2, in the resources ZIP file. This platform game is the most ambitious one in the book, but anyone can code it if they follow the steps in this chapter. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |