Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // GameScene.swift
- // flappy m8
- //
- // Created by Mitch on 7/12/2014.
- // Copyright (c) 2014 Mitch. All rights reserved.
- //
- import SpriteKit
- class GameScene: SKScene, SKPhysicsContactDelegate {
- var score = 0
- var i = 1
- var startGame = 0
- var scoreLabel = SKLabelNode()
- var gameOverLabel = SKLabelNode()
- var startLabel = SKLabelNode()
- var bird = SKSpriteNode()
- var bg = SKSpriteNode()
- var pipe1 = SKSpriteNode()
- var pipe2 = SKSpriteNode()
- var labelHolder = SKSpriteNode()
- var coin = SKSpriteNode()
- let birdGroup: UInt32 = 1
- let objectGroup: UInt32 = 2
- let gapGroup: UInt32 = 0 << 3
- let gapGroup2: UInt32 = 0 << 4
- let gapGroup3: UInt32 = 0 << 5
- var gameOver = 0
- var movingObjects = SKNode()
- var coinGroup1 = SKNode()
- var coinGroup2 = SKNode()
- var coinGroup3 = SKNode()
- override func didMoveToView(view: SKView) {
- if(startGame == 0){
- startLabel.fontName = "Helvetica"
- startLabel.fontSize = 30
- startLabel.text = "Tap to start"
- startLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) + CGRectGetMidY(self.frame)/2)
- startLabel.zPosition = 13
- self.addChild(startLabel)
- movingObjects.speed = 0
- self.physicsWorld.gravity = CGVectorMake(0, 0)
- coinGroup1.speed = 0
- coinGroup2.speed = 0
- coinGroup3.speed = 0
- }
- self.physicsWorld.contactDelegate = self
- self.addChild(coinGroup1)
- self.addChild(coinGroup2)
- self.addChild(coinGroup3)
- self.addChild(movingObjects)
- self.addChild(labelHolder)
- /********| BG |********/
- createBackground()
- /********| Score |********/
- scoreLabel.fontName = "Helvetica"
- scoreLabel.fontSize = 60
- scoreLabel.text = "0"
- scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height - 70)
- scoreLabel.zPosition = 9
- self.addChild(scoreLabel)
- /********| Bird |********/
- var birdTexture1 = SKTexture(imageNamed: "img/flappy1.png")
- var birdTexture2 = SKTexture(imageNamed: "img/flappy2.png")
- var animateBird = SKAction.animateWithTextures([birdTexture1, birdTexture2], timePerFrame: 0.1)
- var animateBirdForever = SKAction.repeatActionForever(animateBird)
- bird = SKSpriteNode(texture: birdTexture1)
- bird.runAction(animateBirdForever)
- bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
- bird.physicsBody = SKPhysicsBody(circleOfRadius: birdTexture1.size().height/2)
- bird.physicsBody?.dynamic = true
- bird.physicsBody?.allowsRotation = false
- bird.physicsBody?.categoryBitMask = birdGroup
- bird.physicsBody?.collisionBitMask = gapGroup
- bird.physicsBody?.collisionBitMask = gapGroup2
- bird.physicsBody?.collisionBitMask = gapGroup3
- bird.physicsBody?.collisionBitMask = objectGroup
- bird.physicsBody?.contactTestBitMask = objectGroup
- bird.zPosition = 10
- self.addChild(bird)
- /********| Ground |********/
- var ground = SKNode()
- ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width
- , 1))
- ground.physicsBody?.dynamic = false
- ground.physicsBody?.categoryBitMask = objectGroup
- self.addChild(ground)
- /********| Pipes |********/
- var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("spawnPipes"), userInfo: nil, repeats: true)
- }
- func createBackground(){
- var bgTexture = SKTexture(imageNamed: "img/bg.png")
- var moveBg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 9)
- var replaceBg = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
- var moveAndReplaceBg = SKAction.repeatActionForever(SKAction.sequence([moveBg, replaceBg]))
- for var i:CGFloat = 0; i < 3; ++i{
- bg = SKSpriteNode(texture: bgTexture)
- bg.size.height = self.frame.size.height
- bg.position = CGPoint(x: CGRectGetMidX(self.frame) + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
- bg.runAction(moveAndReplaceBg)
- movingObjects.addChild(bg)
- }
- }
- func spawnPipes(){
- i++
- let pipeGap = bird.size.height * 2
- var pipeOffset = arc4random() % UInt32(self.frame.size.height/2)
- var movementAmount = CGFloat(pipeOffset) - self.frame.size.height / 4
- var pipeOffset2 = arc4random() % UInt32(self.frame.size.height/2)
- var movementAmount2 = CGFloat(pipeOffset2) - self.frame.size.height / 4
- var movePipe = SKAction.moveByX(-self.frame.size.width * 2, y: 0, duration: 9)
- var removePipe = SKAction.removeFromParent()
- var moveAndRemove = SKAction.sequence([movePipe, removePipe])
- var pipe1Texture = SKTexture(imageNamed: "img/pipe1.png")
- pipe1 = SKSpriteNode(texture: pipe1Texture)
- pipe1.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) + pipe1Texture.size().height/2 + pipeGap + movementAmount)
- pipe1.runAction(moveAndRemove)
- pipe1.physicsBody = SKPhysicsBody(rectangleOfSize: pipe1.size)
- pipe1.physicsBody?.dynamic = false
- pipe1.physicsBody?.categoryBitMask = objectGroup
- movingObjects.addChild(pipe1)
- var pipe2Texture = SKTexture(imageNamed: "img/pipe2.png")
- pipe2 = SKSpriteNode(texture: pipe2Texture)
- pipe2.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) - pipe1Texture.size().height/2 - pipeGap + movementAmount)
- pipe2.runAction(moveAndRemove)
- pipe2.physicsBody = SKPhysicsBody(rectangleOfSize: pipe2.size)
- pipe2.physicsBody?.dynamic = false
- pipe2.physicsBody?.categoryBitMask = objectGroup
- movingObjects.addChild(pipe2)
- /*
- var gap = SKNode()
- gap.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) + movementAmount)
- gap.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(pipe1.size.width
- , pipeGap))
- gap.runAction(moveAndRemove)
- gap.physicsBody?.dynamic = false
- gap.physicsBody?.collisionBitMask = gapGroup
- gap.physicsBody?.categoryBitMask = gapGroup
- gap.physicsBody?.contactTestBitMask = birdGroup
- movingObjects.addChild(gap)
- */
- var coinTexture = SKTexture(imageNamed: "coin.png")
- coin = SKSpriteNode(texture: coinTexture)
- coin.size.height = bird.size.height
- coin.size.width = bird.size.width
- coin.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width * 1.33, y: CGRectGetMidY(self.frame) + movementAmount2)
- coin.physicsBody = SKPhysicsBody(circleOfRadius: coin.size.height / 2)
- coin.physicsBody?.dynamic = false
- coin.runAction(moveAndRemove)
- coin.physicsBody?.contactTestBitMask = birdGroup
- if(i == 1){
- coin.physicsBody?.categoryBitMask = gapGroup
- coin.physicsBody?.collisionBitMask = gapGroup
- coinGroup1.addChild(coin)
- }else if(i == 2){
- coin.physicsBody?.categoryBitMask = gapGroup2
- coin.physicsBody?.collisionBitMask = gapGroup2
- coinGroup2.addChild(coin)
- }else if(i == 3){
- coin.physicsBody?.categoryBitMask = gapGroup3
- coin.physicsBody?.collisionBitMask = gapGroup3
- coinGroup3.addChild(coin)
- }else{
- i = 1
- }
- }
- func didBeginContact(contact: SKPhysicsContact) {
- if(gameOver == 0){
- 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)){
- score++
- scoreLabel.text = "\(score)"
- if(contact.bodyA.categoryBitMask == gapGroup || contact.bodyB.categoryBitMask == gapGroup){
- coinGroup1.removeAllChildren()
- }else if(contact.bodyA.categoryBitMask == gapGroup2 || contact.bodyB.categoryBitMask == gapGroup2){
- coinGroup2.removeAllChildren()
- }else if(contact.bodyA.categoryBitMask == gapGroup3 || contact.bodyB.categoryBitMask == gapGroup3){
- coinGroup3.removeAllChildren()
- }
- //bird.removeFromParent()
- // SKAction.removeFromParent(coin)
- // movingObjects.removeAllChildren()
- //coin.removeFromParent()
- }else{
- gameOver = 1
- movingObjects.speed = 0
- coinGroup1.speed = 0
- coinGroup2.speed = 0
- coinGroup3.speed = 0
- coinGroup1.removeAllChildren()
- coinGroup2.removeAllChildren()
- coinGroup3.removeAllChildren()
- gameOverLabel.fontName = "Helvetica"
- gameOverLabel.fontSize = 30
- gameOverLabel.text = "Game Over! Tapy to play again."
- gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
- gameOverLabel.zPosition = 11
- labelHolder.addChild(gameOverLabel)
- }
- }
- }
- override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
- if(startGame == 0){
- startGame = 1
- movingObjects.speed = 1
- coinGroup1.speed = 1
- coinGroup2.speed = 1
- coinGroup3.speed = 1
- startLabel.text = ""
- self.physicsWorld.gravity = CGVectorMake(0, -5)
- }
- if (gameOver == 0){
- bird.physicsBody?.velocity = CGVectorMake(0, 0)
- bird.physicsBody?.applyImpulse(CGVectorMake(0, 50))
- }else{
- score = 0
- scoreLabel.text = "0"
- movingObjects.removeAllChildren()
- createBackground()
- bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
- labelHolder.removeAllChildren()
- bird.physicsBody?.velocity = CGVectorMake(0, 0)
- gameOver = 0
- movingObjects.speed = 1
- coinGroup1.speed = 1
- coinGroup2.speed = 1
- coinGroup3.speed = 1
- }
- }
- override func update(currentTime: CFTimeInterval) {
- /* Called before each frame is rendered */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement