In this patch, we build create a virtual invisible maze of 10*10 fields. A player can move through the maze using the arrow keys. We use the table object to save the positions of the walls and treasures and the playsf~ object to play back the sounds.
We can imagine the invisible soundscape like this:
We give every virtual field a number, so we easily can easily specify moving in it. A bit like in a chessboard, however, no letters, only numbers:
It is clear, if we would like to move in such a field, a step to the right would equal “+1″, a move to the top would equal “+10″. Similarly, moving down would equal “-10″ and moving left “-1″. We can move in our imagined but agreed upon space like this:
Due to the constraints of our field, we would have to make sure, the player stays within the the boundaries of 0 to 99.
This has some problems. If I am at position 97 and I move up, I jump to 99. Also, on a spot like 19, the player can not move to 20, as he/she is standing on an edge. Let’s try simulating this with Pd. We can use % (modulo) for this. Whenever we are a field with a “field number%10=9″ we do not allow to move to the right. Similarly, every field with a “field number%10=0″ does not allow for left movement. We also have to make sure, we allow upwards movement only on fields with a number smaller than 90 and allow downwards movement only for fields with a number bigger than 9. We use spigot to allow or not allow for the movements…
We now can move through our 10*10 field. Let’s place some walls which they user can not cross or move upon.
Let’s put the walls like this (grey) and define 0 as start and 99 target position.
We can for example save the walls in a table object. We can add a table with a size of 99. Then we define for all spots on the maze if there is nothing special (in this case we put a 0 at this index) or if there is a wall (then we put in a -1 at this index) or if it is the target point (we put in a 1 at this index).
Let’s have a look inside:
Now, with every move, we have to check whether there is a wall in the way. If there isn’t, we make the move, if there is one, we don’t and print “Outch!”
It is now time to have some auditive feedback played with every successful step and some hit-the-wall-sound with every the player hits the wall. We use the [readsf~] object for this:
And the complete patch: