Let's Get Moving!Now that we have a piece to play with, we need to do just that, play with it. Get your minds out of the gutter! Anyway, here is the code These two functions Rotate_Shape() and Move_Shape() are pretty big, so it is good they are twins. Yet, what they do, is fairly cut and dried. Let's cover, just in general, what it is they do. The rotate function first decided if it is at the last frame. If so, it has code to wrap it around for all of the test, otherwise it just uses the next frame. Then, it loops through all of the bits, finding the valid ones, just like Test_Collision(). If there is already a bit set in the place the shape would be at then it is not allowed to move and the call fails. And ... that is that. NOTE: Code is not in there to check for out of bounds on the grid. So, if you rotate at a corner, you may slide out of the grid and into the background area. This has been left as AN EXERCISE FOR YOU. I wanted to see something interactive come out of this article series, and I decided this would be as good of a place as any to start asking for it. I will present my solution in the next article. Compare yours to mine at that time. Back to the code at hand: Move_Shape(). This function will move the shape to the left, or to the right, depending on the value passed into it. It merely tests the bits once again. Only this time we have to find the leftmost, or rightmost, valid bit in each row. Then, we check the grid block to the left or right and see if it is empty. Accordingly, either we move it, or we don't, and then return to the caller. There isn't much else to talk about in this section. You have seen how to access everything many times now. The only thing that changes is the things we need to access, or the order in which we test stuff. These are the kinds of things that need to be resolved at design time.
|
|