In the previous section we completed the creation of shape , and now the elements in the game (Blocks,shapes) have been completed and the background is set (array2d), let's start making the rules of the game. First of all, we need to get our shape to fall, remember when we first started with 600 milliseconds to refresh the screen? There's a closure. We don't quite understand what it's for, we'll know right away.
Well, after today's chapter, your program should work like this:
Let's change the code, this time to modify the code is more, and there is no previous chapter as the repetition of the work. Don't worry too much, let's take a step-by-step:
In #1, the function executes with the blockrowcolumnposition relationship of each different shape subclass written in the previous section, determining where each block rotates.
Next, we can simply derive their function from the names of the functions,lowershapebyonerow, each time the shape drops one line, and the exact whereabouts, see the shiftby function in the #2 , This is all very simple, so you don't have to explain it in detail.
The moveto function of #3 is to move the blocks directly to the specified row and column, why does it have this function? Don't worry, we'll see it soon.
#4 , we will randomly generate one of the 7 shapes that were created earlier.
Next, we need a new class Swiftris to manage the game's logic and processes, such as the initialization of the game, the beginning, the end, and so on. The steps to create a new class don't need to be said, let's just write the code.
At present, our Swiftris class is very simple, rest assured, it will become complex.
We notice that in Swiftris we declare 3 variables, one is a two-dimensional array to represent each block location Blockarray, one is nextshape, and the last is Fallingshape , we can easily derive from the name Nextshape is what we use to preview what shape the next is, play the Russian block of children's shoes have come to think of it, and the corresponding,fallingshape Is the shape we are manipulating in the game, we can rotate, move, and so on to manipulate it.
Next the Init function we generate a two-dimensional array of 20 rows and 10 columns to represent the location of the blocks.
In begingame we randomly generate a shape, using the last function we wrote in the Shape class, and put it in the position we set.
In #2 , we have a function that returns a value of fallingshape and Nextshape, and of course fallingshape is the nextshape we've generated before, We used the new Movtto function in the shape class to put him in the middle of our game area and then create a new nextshape. There's no logical problem.
OK, the logical relationship is processed, and then it's our turn to handle the visual effect. You can finally get your hands on the scene!
#1, we define the size of each block,20x20
#2, we define some Sknodes, the bottom of which is Gamelayer, which is shapelayer on the previous layer, and then GameBoard
We have to add that no full gameboardare shown:
Let GameBoard = Skspritenode (Texture:gameboardtexture, Size:cgsizemake (BlockSize * cgfloat (NumColumns), BlockSize * CGF Loat (numrows)))
#3, do not underestimate this function, which is actually the most important function in our whole gamescene,
pointforcolumn (int,int) . He calculates the anchor position of each block based on column and row, so the return is a point coordinate, so that we can place each block on the
Shapelayer based on this coordinate. In fact, it calculates the coordinates of the center point of each block.
#4, we add the newly generated nextshape to the screen, note that one of the parameters of the function is an empty closure, because the function finally has a function to add action runaction, it has a parameter completion, Here we use a () and () closure or a placeholder, if we use, there is no empty. It was the same tick closure we had at the beginning, but soon we could see that the tick was not an empty closure. We'll talk about this later.
Here we put the sktexture object in a dictionary , because each shape will have a lot of blocks, and we are going to reuse these image
#5, here we have used the previously defined pointforcolumn function to precisely add each block to the exact location, we started from row-2 , This will make our animation more smoothly into the picture.
#6, we've added a set of animations, and we've changed the alpha of each block from 0 to 0.7, because it's easier for users to have an animated feel. About the parameters, as well as the length of time, you can manually change them, and then see what changes in effect.
The next two functions ensure that the same skaction moves and re-paints each block of a different shape
Well, our drawing layer has been written, our logic layer has been fixed, then let's user interaction class Gameviewcontroller to associate them together.
#1 we set up a function for our tick closures, see, we can understand that before (), we can't understand that, whenever the screen refreshes, we execute the didtick function, we move the fallingshape down one line, Then redraw its shape on the scene.
OK, let's run the next program and take a look at the dynamics of our Tetris drop.
Step-by-step teaching you to develop Tetris with Swift: No.7 drop mechanism