Step by step teach you how to use Swift to develop Tetris: No. 5 Block Party, swiftno.5
I really don't know how to get a nice Chinese name, or use the name in the original tutorial,Block partyRight
If you think the previous tutorials are very simple, it is time to increase the difficulty. In the Tetris game, our main body is those shapes, and each shape is composed of different blocks. Therefore, we need to create a base class, block, to lay the foundation for the display of the upper layer.
First, create a new class namedBlock
Modify your block class as follows
As you may have guessed, this is not all the content of our block class.
This part only definesEnumeration, enumeration type: BlockColorIf you read the resource files we put in, our game has 6 colors of fast
In#1We have defined a variable to indicate the color types that can be displayed: 6
In#2We define an enumeration type, which is an int row, and then it follows a protocolPrintable. When you enter the code, if you do not defineDescription functionSo we can guess that if the protocol printable is followed, the description is required.
This is why you want to manually enter the code, instead of directly copying and pasting the code.
For more information about protocols, click here
In#3We provide all the options that can be enumerated. They start from Blue (0) and end with Yellow (5)
For more enumeration knowledge, click here
In#4We define a computing nature (Computed property),SpriteName, Computed property is similar to a variable, but every time you access it, it will execute a code block inside it. We can originally place a function name inside it, for exampleGetSpriteName ()But obviously, computed property isBetterSelect
We use a switch... case to complete this function.
In#5We use another computed property,DescriptionAs we mentioned earlier, because we have adopted the ProtocolPrintable, So this description is required.
For more information about computed properties, click here
Finally#6We have defined a static function namedRandom (), You can easily know from its name that it has been internally computed. This is a function that returns random colors if your function is inRawValueAn error is prompted, so please upgrade yourXcode to Version 6.1As mentioned above, we have already reminded you in the beginning of the teaching material.
Now, let's continue to complete all the block classes:
In#1, We define a class, which will execute the protocol at the same timePrintableAndHashable, WhereHashableWe will allow our blocks to be stored in Array2D.
In#2, We define our attribute colorLetIt means that once we assign a value to it, we can no longer assign a value to it. The game shows that when a block color is assigned, it cannot be changed to another color.
Want to learn more about var and let? Click here
In#3We define column and row. These two parameters determine the position of the block on the screen.SKSpriteNodeGameScene will paint and animated each block on the screen.
In#4We actually define a snapshot. When we callBlock. spriteNameIn fact, what we call isBlock. color. spriteName
In#5, We have definedHashable ProtocolWe define the number of rows and columns of the current block.Or ValueTo make sure they are bothUniqueTo be hashed out.
In#6We should be familiar with this.PrintableThe content required by the Protocol. Note that we do not need to write a string in the format of @ "... % @..., string ".\()You can easily write the desired content into a string. If your row is 3, column is 8, and color is blue, blue: [] is returned.
The init function is also defined. This function is relatively simple.
Last#7, We have customized a symbol=It has two parameters: lhs and rhs, which return bool values; l and r are actually equal left and right. If the row, column, and color of the block are the same, then I will return true.
========================================================== ==========================================
OK. Here, our basic classBlockSet up!
Is it okay? I suggest you review and absorb the content described in these chapters carefully, and view all the points you have not understood. There are many links in this article, which is equivalent to advanced knowledge, let's take a look.
In addition, I may not be able to update the tutorial this week. For personal reasons, the update will be suspended for 2 days,Next Monday (February 2)I will resume the update, and we will be able to see the preliminary appearance of our Russian square!
If you are more advanced and have some basic knowledge, refer to the original tutorial. Original tutorial address here: https://www.bloc.io/tutorials/swiftris-build-your-first-ios-game-with-swift! /Chapters/681
However, I suggest that you thoroughly understand the current content.ClosureAfter studying it for a whole day, I continued to go through the tutorial. This is indeed helpful for the subsequent understanding. If I just typed the code and ran the program, I would say, "OK, I learned, but it is not responsible, maybe you don't know why to write the code like this. How can you say that you can?
Okay. We will see you next week!