A step-by-step teach you to develop Tetris with Swift: No.5 block Party

Source: Internet
Author: User
Tags hashable

I really can't think of how to get a good Chinese name, or use the original tutorial name,block Party Bar

If you think the previous tutorials are simple, it's time to start making it harder. In Tetris Games, our bodies are those shapes, and each shape is made up of different blocks. So, we need to create a base class, block, to lay the groundwork for our higher-level display.

First, follow the steps to establish a two-dimensional array array2d to create a new class named Block

Modify your block class as follows


As you may have guessed, this is not the entire contents of our block class.

This part just defines a enumeration, enum type: Blockcolor , if you look at the resource files we put in before, our game has a total of 6 colors of fast


In #1 We define a variable that indicates the kind of color that can be displayed: 6


In #2 We define an enumeration type, which is an int line, and then it follows a protocol printable. When you actually enter the code, if you do not define the description function , then you will always be prompted to printable is wrong, so we can guess if the protocol printable, then description is necessary.

That's why you want people to manually enter the code instead of just copying and pasting the past.


For more knowledge about the protocol, click here


In #3 We provide all the options that can be enumerated, starting with blue (0) and ending at Yellow (5)


For more enumeration knowledge, click here


In #4 We define a property that can be computed (computedproperties),spritename, computed is similar to a variable, but each time it is accessed, It executes the block of code inside it, and we could have placed a function name inside it, such as getspritename (), but obviously the computed property was a better choice.

We use a switch...case to do this function.


In #5 We used another computed property,description, as we mentioned earlier, because we used the protocol printable, So this description is a must.


For more computed properties knowledge, click here


At the end of the #6 We define a static function, called random (), and you can easily see from its name already its internal calculations that this is a function that returns a random color, if your function is in rawValue there is an error here, so please upgrade your Xcode to version 6.1 above, we have already reminded you at the beginning of the textbook.


OK, let's go ahead and finish all of block class:




In #1, we define a class that will execute both protocol Printable and hashable, where hashable will allow our block to be stored in the Array2D.


In #2, we define our attribute color as let, which means that once we have assigned it, we can no longer assign a value to it. In the game it is shown that when a block color is assigned, it can no longer be replaced by another color.


Want to gain a deeper understanding of Var and let? Click here


In #3, we define column and row, which will determine where our block is on the screen. Skspritenode will be depicted on the screen when Gamescene is coloring and animating each block.


In #4, we actually define a snapshot, and when we call Block.spritename , we actually call Block.color.spriteName .


In #5, we define what the hashable protocol requires, and we define the value of the number of rows and columns of the current block to ensure that they are unique so that they can be hashed out.


In the #6, this should be more familiar with it, we have to complete the printable agreement needs, note that we do not need @ "...%@..,string" so cumbersome form to write a string, and use \ () you can easily write the content we want to the string, if your row is 3, column is 8, and color is blue, it will return blue:[8,3]

It also defines the INIT function, which is relatively simple.


Finally #7, we customize a symbol = , it has two parameters LHS and RHS, returns the value of type bool, L and R are actually the left and right of the equals sign, if the block's row,column and color are the same, Then I'll go back to ture.


============================================================================

OK, here, our base class Block is set up!

You feeling all right? I suggest that you still carefully review and absorb the content of these chapters, to the point of not understand all of their own view, the text has a lot of expanded links, equivalent to advanced knowledge, everyone may wish to learn.

Also this week I may not be able to update the tutorial, because of personal reasons, suspended updates 2 days , next Monday (February 2) I will resume the update, then will be able to see the initial appearance of our Tetris!

If there is progress faster, and some basic students, you can refer to the original tutorial to learn first. The original tutorial address is here: https://www.bloc.io/tutorials/swiftris-build-your-first-ios-game-with-swift#!/chapters/681

But I still suggest that we thoroughly understand the contents of the present, I studied closure study for a whole day to continue the tutorial, so the understanding of the latter is really helpful. And if just the code to knock up, and then run a program, see the effect is said: Ok I learned, in fact, is very irresponsible, may be you on these code why this write do not understand, how can say oneself will?

All right, we'll see you next Monday!


A step-by-step teach you to develop Tetris with Swift: No.5 block Party

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.