Swift game Development Tetris: No.8 Game rules

Source: Internet
Author: User

Each game has its own rules, the rules of our Tetris are obvious, the shape falls at the bottom, and then the next shape starts to fall; When any point blocks the falling shape, the whole shape is considered to be the end; When a row fills all blocks , this line is eliminated, then all down the line, and so on.

So we're going to start making our game rules right now, and after this section, our program runs like this:


We started with a custom protocol (protocol), before we had contacted two Swift-brought protocols,hashable and printable. We first add our own protocol to the Swiftris class.


Our program will take the trial and error method (Trial-and-error) as the basic principle, our user interface Gameviewcontroller ask swiftris to move left to right down , if this directive is lawful, we will execute it.

When is it "illegal"? Let's define an illegal situation.



Here we create a new function in the #2 to determine if shape is in an illegal position, because we use the trial-and-error method to test whether it is in a legal place, so Our shape may be in all sorts of strange places, no matter.

Next we add some functions to rotate and move our shape



In #1 We added two functions to allow shape to rotate clockwise and counterclockwise, and then we added a function that allows shape to move up or down.

Next, let's really make these functions work:




#1 the Dropshape function, we move the shape down one line at a time, if it's not in an illegal position, loop through it, know it's in an illegal position, because it's already in an illegal position, so we need to restore it to the last legal position, So we need to move it up one line.

#2 We define the function that every tick will be called, and don't worry about the settle function we don't see, we'll refine it later.

#3 We can make our shape spin in the process of falling.

Let's fix the error that exists now


The Settleshape function in the #1 is called when shape is no longer down, we write all the blocks in the current shape into the Blockarray, and tell the program that the shape has landed successfully, Whether it is a real landing or landing on a different shape head, the function in#2 is to complete such a test;

At the same time Fallingshape is set to nil, so Swiftris will start a new fallingshape.

Scoring mechanism

We should remember that most of the game has scores, let's improve the scoring mechanism:






We need a set of variables score and level to represent scores and levels

When we get rid of a whole line, we score, and the whole game relies on that to score, right.



This is a long, long code, but there should be nothing particularly difficult to understand, and the whole logic seems clear. Returns two arrays,linesremoved and fallenblocks

Well, we've gone for a long time, take a break:


Then there's a long way to go .........

Swiftris is about to finish, and we need a function that allows the user to remove all blocks at once.


It's time to link our previously customized protocols with Gameviewcontroller .





Well, run your program and see if it looks like it?



Swift game Development Tetris: No.8 Game rules

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.