Creating a character

To make use of our generated maze, we will need to include a character the player can control. We'll use the default Scratch cat as the protagonist and add all the required control script to make it move through the maze.

Prepare for lift off

If we haven't left it on the stage from the start, we have to add the Scratch cat as a new sprite as shown in the following steps:

  1. Click on the Choose sprite from the library icon.
  2. Search for the Scratch cat option, select it, and click on OK.

The Scratch cat will be loaded for use in the game. Next, we will add a script to it to make it interactively controlled by the player.

Engage thrusters

Let's first set up the broadcasts that will trigger the cat sprite to take proper actions. We need two of those events. The first one is very simple and will be repeated in all game objects as follows:

  1. Start a new script with when I receive <createMaze>.
  2. To this, add the hide block to hide the sprite as shown in the following screenshot:
    Engage thrusters

    This makes sure all other sprites are hidden when the tileGenerator sprite runs. They will remain hidden until we call them to do something later. The cat sprite will be activated right after the maze is created.

  3. Create a second broadcast script with when I receive <startGame>.
  4. After this, we will place a function block. So now is a good time to introduce functions.

    With a function, you can describe a certain operation that is repeated many times during the program. Instead of describing the steps required each time they are needed, just point to the function and tell the computer to perform those actions. Using a function can save space in the running program, because we set specific sets of actions aside as separate scripts. This will also make the general flow of the program easier to understand.

  5. Click on the More Blocks category. This is where we can define and use functions.
  6. Click on the Make a Block button, as shown in the following screenshot:
    Engage thrusters
  7. A pop up will show where we can name the new function block. Let's name it findStartPosition.
  8. We don't need options. So just click on OK to create the new function block.

Note how a new starting block (with a curved top) will appear called findStartPosition. This is the start of the script where we can write all the steps the function should take as shown in the following screenshot:

Engage thrusters

We want to place the cat in an open hallway and not in a wall. To test if the cat can move to a certain position, we will move it and check if it then collides with a wall. If it does, we will move it another tile segment and test again. We repeat this process until we can place the sprite. Now perform the following steps:

  1. Make a list for this sprite, called xySave, to store the coordinates. This list will be used later to help move the sprite.
    Engage thrusters
  2. Between the startGame receiver and the findStartPosition block, make sure to create a delete <all> of <xySave> block to start with an empty list, as shown in the following screenshot:
    Engage thrusters
  3. Continue to define the function we just created. Such a definition is just like a normal stack of script blocks. It can be called whenever we use the function block.
  4. Our first addition to the define findStartPosition script is to create the set size to (15) % block to make the sprite fit inside the maze.
  5. Then, move it to the upper-left corner with a go to x: (-230) y: (170) block.
  6. Add a repeat until block to check for collisions with the walls.
  7. In the condition slot, place a combination of not touching color <black> ?. Use the eyedropper option to pick the color.
  8. The action to repeat will be to use the change x by (20) block, making the cat step to the right.
  9. After the cat has found its starting position, use the add x position to <xySave> (the built-in variable).
  10. Then, set the add y position to <xySave> (the built-in variable).
  11. Make the sprite go to front to make sure it isn't covered by other sprites.
  12. Finally, make the show sprite.
    Engage thrusters

We can then add the basic movement controls. As we have to keep in mind that the cat shouldn't be allowed to walk through walls, we have to perform a check before each move. This check will be the same for each step we take.

We will create another function to define this repeated collision check. Regardless of the key pressed or the direction the cat will move in, the check will stay the same.

To set up the keyboard controls, we create four-key pressed script, as we have done in earlier examples. For each directional key, we point the cat in the right direction as follows:

Engage thrusters

When the cat is facing the correct direction, we can make it try to take a step forward. We create a function to describe this move attempt. If the way is clear, the cat will move. If it's blocked by a wall, the cat will stay where it is. Perform the following steps:

  1. Click on the More Blocks category.
  2. Then, click on the Make a Block button.
  3. Name the new function as takeStep and click on OK without selecting options.

Another starting block will appear called define takeStep. This is the start of the script where we can write all the steps the function should take. Perform the following steps:

  1. To the define takeStep block, add set the replace item <1> of <xySave> with x position block (the built-in variable).
  2. Then, add the same for the y position; by setting the replace item <2> of <xySave> with y position block (the built-in variable).
  3. Then make the cat move twenty steps by setting the move (20) steps block. This corresponds to one tile segment, as we calculated earlier when planning the maze tiles.
  4. Add an if () else () statement to check for collision and decide what action to take depending on the result.
  5. In the if condition, check for touching color <black> ? or touching <edge> ? then block.
  6. If the cat does collide with either one of those, reset it to its original position with a go to x:() y:() block.
  7. Fill the slots with the item <1> of <xySave> and item <2> of <xySave> block with x and y positions respectively.
  8. If the cat didn't collide with a wall, we can save the new coordinates.
  9. Set the replace item <1> of <xySave> with x position block.
  10. Set the replace item <2> of <xySave> with y position block.

We could leave it at that point, because the cat is now in a new valid position. However, the movement will look very jumpy this way. So we will improve that by adding a few scripts to create fluid movement which are as follows:

  1. Reset the cat to its previous position by setting the move (-20) steps block.
  2. Then, we will make the glide (0.4) secs to x:() y:() block.
  3. In the slots, we will use the freshly saved coordinates, item <1> of <xySave> and item <2> of <xySave>:
    Engage thrusters

That completes our main player control scripts. The cat sprite can be moved freely through the corridors of the maze but it won't be able to cross a wall.

Objective complete – mini debriefing

With this step, we have probably completed the most difficult part of the script. We used the new Make a Block option to create functions that make script a little easier to read and can save work or excessive copy-pasting when creating complex operations.

The beginnings of a game can be visible now; the cat can be moved through the maze.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.31.163