Skip to content
Swift version Tetris game.
Swift
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Swiftris.xcodeproj
Swiftris
SwiftrisTests
.gitignore
LICENSE
README.md

README.md

icon

Swiftris

  • Swiftris is a swift version of Tetris game.
  • It has been developed for the study.
  • It's simple but impact. enjoy the game.

1. Swiftris Video

movie

2.Control of Game

  • Play: Touch a Play button.
  • Pause: Touch a Pause button.
  • Stop: Touch a Stop button.

play    pause

  • Move Left: Touch the left side of the brick.
  • Move Right: Touch the right side of the brick.
  • Rotate : Touch the top side of the brick.
  • Drop: Long touch the bottom side of the brick.

3. Architecture

  • ViewController.swift
  • Swiftris.swift
  • GameView.swift
    • GameBoard.swift
    • GameScore.swift
    • NextBrick.swift
    • Brick.swift
  • GameTimer.swift
  • SoundManager.swift

GameBoard

  • GameBoard is 22 rows by 10 columns, Two-dimensional array of UIColor.
class GameBoard: UIView {
	static let rows = 22
    static let cols = 10
    
    ...
	var board = [[UIColor]]()
	...
}
  

gameboard

Brick

  • 7 different types of bricks. I, J, L, T, Z, S, O.
  • Brick has a unique color.
enum BrickType {
    case I(UIColor)
    case J(UIColor)
    case L(UIColor)
    case T(UIColor)
    case Z(UIColor)
    case S(UIColor)
    case O(UIColor)
}  
  
class Brick: NSObject {
    
	...
    static var bricks = [
        BrickType.I(UIColor(red:0.40, green:0.64, blue:0.93, alpha:1.0)),
        BrickType.J(UIColor(red:0.31, green:0.42, blue:0.80, alpha:1.0)),
        BrickType.L(UIColor(red:0.81, green:0.47, blue:0.19, alpha:1.0)),
        BrickType.T(UIColor(red:0.67, green:0.45, blue:0.78, alpha:1.0)),
        BrickType.Z(UIColor(red:0.80, green:0.31, blue:0.38, alpha:1.0)),
        BrickType.S(UIColor(red:0.61, green:0.75, blue:0.31, alpha:1.0)),
        BrickType.O(UIColor(red:0.88, green:0.69, blue:0.25, alpha:1.0))
    ]
	...    
}
  

bricks

Swiftris

  • Process game logic and Interaction.
class Swiftris: NSObject {

	...
	
 	// Move the brick to the left, right and Rotate the brick.
    func touch(touch:UITouch!) {
        guard self.gameState == GameState.PLAY else { return }
        guard let curretBrick = self.gameView.gameBoard.currentBrick else { return }
        
        let p = touch.locationInView(self.gameView.gameBoard)
        
        let half = self.gameView.gameBoard.centerX
        
        let top  = curretBrick.top()
        let topY = CGFloat( (Int(top.y) + curretBrick.ty) * GameBoard.brickSize )

        if p.y > topY  {
            if p.x > half {
                self.gameView.gameBoard.updateX(1)
            } else if p.x < half {
                self.gameView.gameBoard.updateX(-1)
            }
        } else   {
            self.gameView.gameBoard.rotateBrick()
        }
    }
	
	// Drop the brick.
 	func longPressed(longpressGesture:UILongPressGestureRecognizer!) {
        if self.gameState == GameState.PLAY {

            if longpressGesture.state == UIGestureRecognizerState.Began {
                self.gameView.gameBoard.dropBrick()
            }
        }
    }

}
  

GameScore

  • Game score is depending on number of lines are cleared. 10, 30, 60, 100 points.
  • This game did not implement the feature level. please implement your own.

class GameScore: UIView {

	...
    var scores = [0, 10, 30, 60, 100]  
    
    func lineClear(noti:NSNotification!) {
        if let userInfo = noti.userInfo as? [String:NSNumber] {
            if let lineCount = userInfo["lineCount"] {
                self.lineClearCount += lineCount.integerValue
                self.gameScore += self.scores[lineCount.integerValue]
                self.update()
            }
        }
    }
    
    ...
}
    
  

scores

NextBrick

  • You can see the next three bricks that can be used to play in advance.

nextbricks

class Brick: NSObject {
	...
	static var nextBricks = [Brick]()
    static var nextBrickCount = 3
    
    // use the first brick from next brick queue and fill three.
    static func generate() -> Brick! {
        while self.nextBricks.count < self.nextBrickCount {
            self.nextBricks.append(self.newBrick())
        }
        let brick = self.nextBricks.removeAtIndex(0)
        self.nextBricks.append(self.newBrick())
        return brick
    }
    ...

}
  

SoundManager

  • Provides some sound effects .
  • Background Music, Falling brick sound, Game over sound.
class SoundManager: NSObject {
   
    var bgmPlayer:AVAudioPlayer?
    var effectPlayer:AVAudioPlayer?
    var gameOverPlayer:AVAudioPlayer?
    ...
}
  

4. Feedback

5. License

Swiftris is released under the MIT license. See LICENSE for details.

You can’t perform that action at this time.