Advertisement
Izzard

Swift

Jul 7th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  GameScene.swift
  3. //  BreakOut
  4. //
  5. //  Created by Si Brindley on 06/07/2014.
  6. //  Copyright (c) 2014 Si Brindley. All rights reserved.
  7. //
  8.  
  9. import SpriteKit
  10.  
  11. class GameScene: SKScene, SKPhysicsContactDelegate {
  12.    
  13.    
  14.     struct categories {
  15.         static let ballCategory: UInt32    = 1 // 00000000000000000000000000000001
  16.         static let brickCategory: UInt32   = 2 // 00000000000000000000000000000010
  17.         static let paddleCategory: UInt32  = 4 // 00000000000000000000000000000100
  18.         static let edgeCategory: UInt32    = 8 // 00000000000000000000000000001000
  19.         static let bottomEdgeCategory: UInt32 = 16
  20.     }
  21.    
  22.     let paddle = SKSpriteNode(imageNamed: "paddle")
  23.     let playPaddleBlip = SKAction.playSoundFileNamed("blip.caf", waitForCompletion: false)
  24.     let playBrickHit = SKAction.playSoundFileNamed("brickhit.caf", waitForCompletion: false)
  25.    
  26.    
  27.     override func didMoveToView(view: SKView) {
  28.         backgroundColor = SKColor.whiteColor()
  29.         physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame) // bouncy walls
  30.         physicsBody.friction = 0
  31.         physicsBody.categoryBitMask = categories.edgeCategory
  32.         physicsWorld.gravity = CGVectorMake(0, 0) // zero-G
  33.         physicsWorld.contactDelegate = self
  34.         addBall()
  35.         addPlayer()
  36.         addBricks()
  37.         addBottomEdge()
  38.     }
  39.    
  40.    
  41.    
  42.     func didBeginContact(contact: SKPhysicsContact) -> () {
  43.        
  44.         var notTheBall: SKPhysicsBody
  45.        
  46.         if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
  47.             notTheBall = contact.bodyB
  48.         } else {
  49.             notTheBall = contact.bodyA
  50.         }
  51.        
  52.         if notTheBall.categoryBitMask == categories.brickCategory {
  53.             runAction(playBrickHit)
  54.             notTheBall.node.removeFromParent()
  55.         }
  56.        
  57.         if notTheBall.categoryBitMask == categories.paddleCategory {
  58.             runAction(playPaddleBlip)
  59.         }
  60.        
  61.         if notTheBall.categoryBitMask == categories.bottomEdgeCategory {
  62.             let end = EndScene.sceneWithSize(size)
  63.             view.presentScene(end, transition: SKTransition.doorsCloseHorizontalWithDuration(0.5))
  64.         }
  65.        
  66.     }
  67.  
  68.    
  69.     override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
  70.         for touch : AnyObject in touches {
  71.             var location = touch.locationInNode(self)
  72.             var newPosition = CGPointMake(location.x, 100) // paddle moves to horizonal position of touch
  73.             // stop paddle going too far
  74.             if newPosition.x < paddle.size.width / 2 {
  75.                 newPosition.x = paddle.size.width / 2
  76.             }
  77.             if newPosition.x > size.width - paddle.size.width / 2 {
  78.                 newPosition.x = size.width - paddle.size.width / 2
  79.             }
  80.             paddle.position = newPosition
  81.         }
  82.     }
  83.    
  84.    
  85.    
  86.     func addPlayer() -> () {
  87.         paddle.position = CGPointMake(size.width/2, 100)
  88.         paddle.physicsBody = SKPhysicsBody(rectangleOfSize: paddle.frame.size)
  89.         paddle.physicsBody.dynamic = false
  90.         paddle.physicsBody.friction = 0
  91.         paddle.physicsBody.categoryBitMask = categories.paddleCategory
  92.         addChild(paddle)
  93.     }
  94.    
  95.    
  96.    
  97.     func addBall() -> () {
  98.         // Create a new sprite node from an image of a football
  99.         let ball = SKSpriteNode(imageNamed: "ball")
  100.         // Centre it
  101.         ball.position = CGPoint(x: size.width/2, y: size.height/2 - 150)
  102.         // Attach a circular physics volume to it
  103.         ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)
  104.         // Set physical properties for ball
  105.         ball.physicsBody.linearDamping = 0 // glides without losing speed
  106.         ball.physicsBody.restitution = 1 // full bounciness without losing energy
  107.         ball.physicsBody.friction = 0
  108.         ball.physicsBody.categoryBitMask = categories.ballCategory
  109.         // What contacts do we want to be notified about?
  110.         ball.physicsBody.contactTestBitMask = categories.brickCategory | categories.paddleCategory | categories.bottomEdgeCategory
  111.         // Add the football to the scene
  112.         addChild(ball)
  113.         // Kick ball
  114.         var vectorHorizontal = Float(arc4random() % 30)
  115.         vectorHorizontal = 15 - vectorHorizontal
  116.         let kick = CGVectorMake(CGFloat(vectorHorizontal), 10)
  117.         ball.physicsBody.applyImpulse(kick)
  118.     }
  119.    
  120.     func addBricks() -> () {
  121.         for row in 1...6 {
  122.             for i in 1...4 {
  123.                 var brick = SKSpriteNode(imageNamed: "brick")
  124.                 brick.physicsBody = SKPhysicsBody(rectangleOfSize: brick.frame.size)
  125.                 brick.physicsBody.dynamic = false
  126.                 brick.physicsBody.friction = 0
  127.                 brick.physicsBody.categoryBitMask = categories.brickCategory
  128.                 var xPos: Float = Float(size.width) / 5.0 * Float(i)
  129.                 var yPos: Float = Float(size.height) - 35.0 * Float(row)
  130.                 brick.position = CGPointMake(CGFloat(xPos), CGFloat(yPos))
  131.                 addChild(brick)
  132.             }
  133.         }
  134.     }
  135.  
  136.  
  137.     func addBottomEdge() -> () {
  138.         let bottomEdge = SKNode()
  139.         bottomEdge.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointMake(0, 1), toPoint: CGPointMake(size.width, 1))
  140.         bottomEdge.physicsBody.categoryBitMask = categories.bottomEdgeCategory
  141.         addChild(bottomEdge)
  142.     }
  143.    
  144.    
  145.     override func update(currentTime: CFTimeInterval) {
  146.         /* Called before each frame is rendered */
  147.     }
  148.    
  149.    
  150. }
  151.  
  152.  
  153. //
  154. //  EndScene.swift
  155. //  BreakOut
  156. //
  157. //  Created by Si Brindley on 07/07/2014.
  158. //  Copyright (c) 2014 Si Brindley. All rights reserved.
  159. //
  160.  
  161. import SpriteKit
  162.  
  163. class EndScene: SKScene {
  164.    
  165.     override func didMoveToView(view: SKView!) {
  166.        
  167.         // "GAME OVER"
  168.         backgroundColor = SKColor.blackColor()
  169.        
  170.         let play = SKAction.playSoundFileNamed("gameover.caf", waitForCompletion: false)
  171.         runAction(play)
  172.        
  173.         let label = SKLabelNode(fontNamed:"Futura Medium")
  174.         label.text = "GAME OVER"
  175.         label.fontColor = SKColor.whiteColor()
  176.         label.fontSize = 44
  177.         label.position = CGPointMake(size.width/2, size.height/2)
  178.         addChild(label)
  179.    
  180.         // Play again button
  181.         let tryAgain = SKLabelNode(fontNamed:"Futura Medium")
  182.         tryAgain.text = "Tap to play again"
  183.         tryAgain.fontColor = SKColor.whiteColor()
  184.         tryAgain.fontSize = 24
  185.         tryAgain.position = CGPointMake(size.width/2, -50)
  186.        
  187.         let moveLabel = SKAction.moveToY(size.height/2 - 40, duration: 1.0)
  188.         tryAgain.runAction(moveLabel)
  189.        
  190.         addChild(tryAgain)
  191.    
  192.     }
  193.    
  194.     override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
  195.         let firstScene = GameScene.sceneWithSize(size)
  196.         view.presentScene(firstScene, transition: SKTransition.doorsOpenHorizontalWithDuration(1.5))
  197.     }
  198.    
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement