Advertisement
Guest User

Flappy bird with collectables

a guest
Dec 10th, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.79 KB | None | 0 0
  1. //
  2. // GameScene.swift
  3. // flappy m8
  4. //
  5. // Created by Mitch on 7/12/2014.
  6. // Copyright (c) 2014 Mitch. All rights reserved.
  7. //
  8.  
  9. import SpriteKit
  10.  
  11. class GameScene: SKScene, SKPhysicsContactDelegate {
  12.  
  13. var score = 0
  14. var i = 1
  15. var startGame = 0
  16. var scoreLabel = SKLabelNode()
  17. var gameOverLabel = SKLabelNode()
  18. var startLabel = SKLabelNode()
  19.  
  20. var bird = SKSpriteNode()
  21. var bg = SKSpriteNode()
  22. var pipe1 = SKSpriteNode()
  23. var pipe2 = SKSpriteNode()
  24. var labelHolder = SKSpriteNode()
  25. var coin = SKSpriteNode()
  26.  
  27. let birdGroup: UInt32 = 1
  28. let objectGroup: UInt32 = 2
  29. let gapGroup: UInt32 = 0 << 3
  30. let gapGroup2: UInt32 = 0 << 4
  31. let gapGroup3: UInt32 = 0 << 5
  32.  
  33. var gameOver = 0
  34.  
  35. var movingObjects = SKNode()
  36.  
  37. var coinGroup1 = SKNode()
  38. var coinGroup2 = SKNode()
  39. var coinGroup3 = SKNode()
  40.  
  41. override func didMoveToView(view: SKView) {
  42.  
  43.  
  44. if(startGame == 0){
  45.  
  46. startLabel.fontName = "Helvetica"
  47. startLabel.fontSize = 30
  48. startLabel.text = "Tap to start"
  49. startLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) + CGRectGetMidY(self.frame)/2)
  50. startLabel.zPosition = 13
  51.  
  52. self.addChild(startLabel)
  53.  
  54. movingObjects.speed = 0
  55. self.physicsWorld.gravity = CGVectorMake(0, 0)
  56.  
  57. coinGroup1.speed = 0
  58. coinGroup2.speed = 0
  59. coinGroup3.speed = 0
  60.  
  61. }
  62.  
  63.  
  64.  
  65. self.physicsWorld.contactDelegate = self
  66.  
  67. self.addChild(coinGroup1)
  68. self.addChild(coinGroup2)
  69. self.addChild(coinGroup3)
  70. self.addChild(movingObjects)
  71. self.addChild(labelHolder)
  72.  
  73. /********| BG |********/
  74.  
  75. createBackground()
  76.  
  77.  
  78. /********| Score |********/
  79.  
  80. scoreLabel.fontName = "Helvetica"
  81. scoreLabel.fontSize = 60
  82. scoreLabel.text = "0"
  83. scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height - 70)
  84. scoreLabel.zPosition = 9
  85.  
  86. self.addChild(scoreLabel)
  87.  
  88.  
  89. /********| Bird |********/
  90.  
  91. var birdTexture1 = SKTexture(imageNamed: "img/flappy1.png")
  92. var birdTexture2 = SKTexture(imageNamed: "img/flappy2.png")
  93.  
  94. var animateBird = SKAction.animateWithTextures([birdTexture1, birdTexture2], timePerFrame: 0.1)
  95. var animateBirdForever = SKAction.repeatActionForever(animateBird)
  96.  
  97. bird = SKSpriteNode(texture: birdTexture1)
  98.  
  99. bird.runAction(animateBirdForever)
  100.  
  101. bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
  102. bird.physicsBody = SKPhysicsBody(circleOfRadius: birdTexture1.size().height/2)
  103. bird.physicsBody?.dynamic = true
  104. bird.physicsBody?.allowsRotation = false
  105.  
  106.  
  107.  
  108.  
  109. bird.physicsBody?.categoryBitMask = birdGroup
  110. bird.physicsBody?.collisionBitMask = gapGroup
  111. bird.physicsBody?.collisionBitMask = gapGroup2
  112. bird.physicsBody?.collisionBitMask = gapGroup3
  113. bird.physicsBody?.collisionBitMask = objectGroup
  114. bird.physicsBody?.contactTestBitMask = objectGroup
  115.  
  116. bird.zPosition = 10
  117.  
  118. self.addChild(bird)
  119.  
  120. /********| Ground |********/
  121.  
  122. var ground = SKNode()
  123. ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width
  124. , 1))
  125. ground.physicsBody?.dynamic = false
  126. ground.physicsBody?.categoryBitMask = objectGroup
  127. self.addChild(ground)
  128.  
  129. /********| Pipes |********/
  130.  
  131. var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("spawnPipes"), userInfo: nil, repeats: true)
  132.  
  133.  
  134. }
  135.  
  136. func createBackground(){
  137. var bgTexture = SKTexture(imageNamed: "img/bg.png")
  138.  
  139.  
  140. var moveBg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 9)
  141. var replaceBg = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
  142. var moveAndReplaceBg = SKAction.repeatActionForever(SKAction.sequence([moveBg, replaceBg]))
  143.  
  144. for var i:CGFloat = 0; i < 3; ++i{
  145.  
  146. bg = SKSpriteNode(texture: bgTexture)
  147. bg.size.height = self.frame.size.height
  148.  
  149. bg.position = CGPoint(x: CGRectGetMidX(self.frame) + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
  150.  
  151. bg.runAction(moveAndReplaceBg)
  152.  
  153. movingObjects.addChild(bg)
  154.  
  155. }
  156. }
  157.  
  158. func spawnPipes(){
  159. i++
  160. let pipeGap = bird.size.height * 2
  161. var pipeOffset = arc4random() % UInt32(self.frame.size.height/2)
  162. var movementAmount = CGFloat(pipeOffset) - self.frame.size.height / 4
  163.  
  164. var pipeOffset2 = arc4random() % UInt32(self.frame.size.height/2)
  165. var movementAmount2 = CGFloat(pipeOffset2) - self.frame.size.height / 4
  166.  
  167. var movePipe = SKAction.moveByX(-self.frame.size.width * 2, y: 0, duration: 9)
  168. var removePipe = SKAction.removeFromParent()
  169. var moveAndRemove = SKAction.sequence([movePipe, removePipe])
  170.  
  171. var pipe1Texture = SKTexture(imageNamed: "img/pipe1.png")
  172. pipe1 = SKSpriteNode(texture: pipe1Texture)
  173. pipe1.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) + pipe1Texture.size().height/2 + pipeGap + movementAmount)
  174. pipe1.runAction(moveAndRemove)
  175. pipe1.physicsBody = SKPhysicsBody(rectangleOfSize: pipe1.size)
  176. pipe1.physicsBody?.dynamic = false
  177. pipe1.physicsBody?.categoryBitMask = objectGroup
  178. movingObjects.addChild(pipe1)
  179.  
  180. var pipe2Texture = SKTexture(imageNamed: "img/pipe2.png")
  181. pipe2 = SKSpriteNode(texture: pipe2Texture)
  182. pipe2.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) - pipe1Texture.size().height/2 - pipeGap + movementAmount)
  183. pipe2.runAction(moveAndRemove)
  184. pipe2.physicsBody = SKPhysicsBody(rectangleOfSize: pipe2.size)
  185. pipe2.physicsBody?.dynamic = false
  186. pipe2.physicsBody?.categoryBitMask = objectGroup
  187. movingObjects.addChild(pipe2)
  188.  
  189. /*
  190. var gap = SKNode()
  191. gap.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) + movementAmount)
  192. gap.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(pipe1.size.width
  193. , pipeGap))
  194. gap.runAction(moveAndRemove)
  195. gap.physicsBody?.dynamic = false
  196. gap.physicsBody?.collisionBitMask = gapGroup
  197. gap.physicsBody?.categoryBitMask = gapGroup
  198. gap.physicsBody?.contactTestBitMask = birdGroup
  199.  
  200. movingObjects.addChild(gap)
  201. */
  202.  
  203.  
  204. var coinTexture = SKTexture(imageNamed: "coin.png")
  205. coin = SKSpriteNode(texture: coinTexture)
  206. coin.size.height = bird.size.height
  207. coin.size.width = bird.size.width
  208. coin.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width * 1.33, y: CGRectGetMidY(self.frame) + movementAmount2)
  209. coin.physicsBody = SKPhysicsBody(circleOfRadius: coin.size.height / 2)
  210. coin.physicsBody?.dynamic = false
  211. coin.runAction(moveAndRemove)
  212.  
  213.  
  214. coin.physicsBody?.contactTestBitMask = birdGroup
  215.  
  216.  
  217. if(i == 1){
  218. coin.physicsBody?.categoryBitMask = gapGroup
  219. coin.physicsBody?.collisionBitMask = gapGroup
  220. coinGroup1.addChild(coin)
  221.  
  222. }else if(i == 2){
  223. coin.physicsBody?.categoryBitMask = gapGroup2
  224. coin.physicsBody?.collisionBitMask = gapGroup2
  225. coinGroup2.addChild(coin)
  226.  
  227. }else if(i == 3){
  228. coin.physicsBody?.categoryBitMask = gapGroup3
  229. coin.physicsBody?.collisionBitMask = gapGroup3
  230. coinGroup3.addChild(coin)
  231.  
  232. }else{
  233. i = 1
  234.  
  235.  
  236. }
  237.  
  238. }
  239.  
  240.  
  241. func didBeginContact(contact: SKPhysicsContact) {
  242.  
  243. if(gameOver == 0){
  244. if((contact.bodyA.categoryBitMask == gapGroup || contact.bodyB.categoryBitMask == gapGroup) || (contact.bodyA.categoryBitMask == gapGroup2 || contact.bodyB.categoryBitMask == gapGroup2) || (contact.bodyA.categoryBitMask == gapGroup3 || contact.bodyB.categoryBitMask == gapGroup3)){
  245.  
  246. score++
  247.  
  248. scoreLabel.text = "\(score)"
  249. if(contact.bodyA.categoryBitMask == gapGroup || contact.bodyB.categoryBitMask == gapGroup){
  250. coinGroup1.removeAllChildren()
  251. }else if(contact.bodyA.categoryBitMask == gapGroup2 || contact.bodyB.categoryBitMask == gapGroup2){
  252. coinGroup2.removeAllChildren()
  253. }else if(contact.bodyA.categoryBitMask == gapGroup3 || contact.bodyB.categoryBitMask == gapGroup3){
  254. coinGroup3.removeAllChildren()
  255. }
  256. //bird.removeFromParent()
  257.  
  258.  
  259. // SKAction.removeFromParent(coin)
  260. // movingObjects.removeAllChildren()
  261. //coin.removeFromParent()
  262.  
  263.  
  264.  
  265.  
  266.  
  267. }else{
  268.  
  269. gameOver = 1
  270.  
  271. movingObjects.speed = 0
  272.  
  273.  
  274. coinGroup1.speed = 0
  275. coinGroup2.speed = 0
  276. coinGroup3.speed = 0
  277. coinGroup1.removeAllChildren()
  278. coinGroup2.removeAllChildren()
  279. coinGroup3.removeAllChildren()
  280.  
  281. gameOverLabel.fontName = "Helvetica"
  282. gameOverLabel.fontSize = 30
  283. gameOverLabel.text = "Game Over! Tapy to play again."
  284. gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
  285. gameOverLabel.zPosition = 11
  286.  
  287. labelHolder.addChild(gameOverLabel)
  288.  
  289. }
  290. }
  291. }
  292.  
  293. override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
  294. if(startGame == 0){
  295. startGame = 1
  296. movingObjects.speed = 1
  297. coinGroup1.speed = 1
  298. coinGroup2.speed = 1
  299. coinGroup3.speed = 1
  300. startLabel.text = ""
  301.  
  302. self.physicsWorld.gravity = CGVectorMake(0, -5)
  303. }
  304.  
  305. if (gameOver == 0){
  306. bird.physicsBody?.velocity = CGVectorMake(0, 0)
  307. bird.physicsBody?.applyImpulse(CGVectorMake(0, 50))
  308. }else{
  309.  
  310. score = 0
  311. scoreLabel.text = "0"
  312.  
  313. movingObjects.removeAllChildren()
  314. createBackground()
  315.  
  316. bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
  317.  
  318. labelHolder.removeAllChildren()
  319.  
  320. bird.physicsBody?.velocity = CGVectorMake(0, 0)
  321.  
  322. gameOver = 0
  323.  
  324. movingObjects.speed = 1
  325. coinGroup1.speed = 1
  326. coinGroup2.speed = 1
  327. coinGroup3.speed = 1
  328.  
  329. }
  330. }
  331.  
  332. override func update(currentTime: CFTimeInterval) {
  333. /* Called before each frame is rendered */
  334. }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement