Game Development Reference
In-Depth Information
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 funcion to deine this repeated collision check. Regardless of the key
pressed or the direcion 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 direcional key, we point the cat in the right direcion as follows:
When the cat is facing the correct direcion, we can make it try to take a step forward. We
create a funcion to describe this move atempt. 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
buton.
3. Name the new funcion as
takeStep
and click on
OK
without selecing opions.
Another staring block will appear called
define takeStep
. This is the start of the script where
we can write all the steps the funcion should take. Perform the following steps:
1. To the
define takeStep
block, add set the
replace item <1> of <xySave> with x
posiion
block (the built-in variable).
2. Then, add the same for the y posiion; by seing the
replace item <2> of <xySave>
with y posiion
block (the built-in variable).
3. Then make the cat move twenty steps by seing the
move (20) steps
block. This
corresponds to one ile segment, as we calculated earlier when planning the maze
iles.
4.
Add an
if () else ()
statement to check for collision and decide what acion to take
depending on the result.