Skip to content

Commit

Permalink
pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
jashmenn committed Jun 3, 2014
1 parent 0360240 commit 733d3eb
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 18 deletions.
118 changes: 100 additions & 18 deletions FlappyBird/GameScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,74 @@
import SpriteKit

class GameScene: SKScene {
var bird: SKSpriteNode?
var skyColor: SKColor?
var bird = SKSpriteNode()
var skyColor = SKColor()
var verticalPipeGap = 100.0
var pipeTextureUp = SKTexture()
var pipeTextureDown = SKTexture()
var movePipesAndRemove = SKAction()

override func didMoveToView(view: SKView) {
// setup physics
self.physicsWorld.gravity = CGVectorMake( 0.0, -5.0 )

// setup background color
skyColor = SKColor(red: 113.0/255.0, green: 197.0/255.0, blue: 207.0/255.0, alpha: 1.0)
skyColor = SKColor(red: 81.0/255.0, green: 192.0/255.0, blue: 201.0/255.0, alpha: 1.0)
self.backgroundColor = skyColor

// ground
var groundTexture = SKTexture(imageNamed: "land")
groundTexture.filteringMode = SKTextureFilteringMode.Nearest

for var i = 0; CGFloat(i) < (2.0 + self.frame.size.width) / ( groundTexture.size().width * 2.0 ); ++i {
var moveGroundSprite = SKAction.moveByX(-groundTexture.size().width * 2.0, y: 0, duration: NSTimeInterval(0.02 * groundTexture.size().width * 2.0))
var resetGroundSprite = SKAction.moveByX(groundTexture.size().width * 2.0, y: 0, duration: 0.0)
var moveGroundSpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveGroundSprite,resetGroundSprite]))


for var i = 0; Float(i) < 2.0 + self.frame.size.width / ( groundTexture.size().width * 2.0 ); ++i {
var sprite = SKSpriteNode(texture: groundTexture)
sprite.setScale(2.0)
sprite.position = CGPointMake(CGFloat(i) * sprite.size.width, sprite.size.height / 2.0)
sprite.runAction(moveGroundSpritesForever)
self.addChild(sprite)
}

// skyline
var skyTexture = SKTexture(imageNamed: "sky")
skyTexture.filteringMode = SKTextureFilteringMode.Nearest

for var i = 0; CGFloat(i) < (2.0 + self.frame.size.width) / ( skyTexture.size().width * 2.0 ); ++i {
var moveSkySprite = SKAction.moveByX(-skyTexture.size().width * 2.0, y: 0, duration: NSTimeInterval(0.1 * skyTexture.size().width * 2.0))
var resetSkySprite = SKAction.moveByX(skyTexture.size().width * 2.0, y: 0, duration: 0.0)
var moveSkySpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveSkySprite,resetSkySprite]))

for var i = 0; Float(i) < 2.0 + self.frame.size.width / ( skyTexture.size().width * 2.0 ); ++i {
var sprite = SKSpriteNode(texture: skyTexture)
sprite.setScale(2.0)
sprite.zPosition = -20;
sprite.position = CGPointMake(CGFloat(i) * sprite.size.width, sprite.size.height / 2.0 + groundTexture.size().height * 2.0)
sprite.runAction(moveSkySpritesForever)
self.addChild(sprite)
}

// create the pipes textures
pipeTextureUp = SKTexture(imageNamed: "PipeUp")
pipeTextureUp.filteringMode = SKTextureFilteringMode.Nearest
pipeTextureDown = SKTexture(imageNamed: "PipeDown")
pipeTextureDown.filteringMode = SKTextureFilteringMode.Nearest

// create the pipes movement actions
var distanceToMove = Float(self.frame.size.width + 2.0 * pipeTextureUp.size().width);
var movePipes = SKAction.moveByX(-distanceToMove, y:0.0, duration:NSTimeInterval(0.01 * distanceToMove));
var removePipes = SKAction.removeFromParent();
movePipesAndRemove = SKAction.sequence([movePipes, removePipes]);

// spawn the pipes
var spawn = SKAction.runBlock({() in self.spawnPipes()})
var delay = SKAction.waitForDuration(NSTimeInterval(2.0))
var spawnThenDelay = SKAction.sequence([spawn, delay])
var spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
self.runAction(spawnThenDelayForever)

// setup our bird
var birdTexture1 = SKTexture(imageNamed: "bird-01")
birdTexture1.filteringMode = SKTextureFilteringMode.Nearest
Expand All @@ -50,13 +87,54 @@ class GameScene: SKScene {
var flap = SKAction.repeatActionForever(anim)

bird = SKSpriteNode(texture: birdTexture1)
bird!.setScale(2.0)
bird!.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
bird!.runAction(flap)
bird.setScale(2.0)
bird.position = CGPoint(x: self.frame.size.width * 0.35, y:self.frame.size.height * 0.6)
bird.runAction(flap)


bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height / 2.0)
bird.physicsBody.dynamic = true
bird.physicsBody.allowsRotation = false

self.addChild(bird)

// create the ground
var ground = SKNode()
ground.position = CGPointMake(0, groundTexture.size().height)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height * 2.0))
ground.physicsBody.dynamic = false
self.addChild(ground)


}

func spawnPipes() {
var pipePair = SKNode()
pipePair.position = CGPointMake( self.frame.size.width + pipeTextureUp.size().width * 2, 0 );
pipePair.zPosition = -10;

var y = Float(arc4random()) % Float(NSInteger( self.frame.size.height / 3 ));

var pipeDown = SKSpriteNode(texture: pipeTextureDown)
pipeDown.setScale(2.0)
pipeDown.position = CGPointMake(0.0, y)
pipeDown.physicsBody = SKPhysicsBody(rectangleOfSize: pipeDown.size)
pipeDown.physicsBody.dynamic = false
pipePair.addChild(pipeDown)

var pipeUp = SKSpriteNode(texture: pipeTextureUp)
pipeUp.setScale(2.0)
pipeUp.position = CGPointMake(0.0, y + Float(pipeUp.size.height) + Float(verticalPipeGap))
pipeUp.physicsBody = SKPhysicsBody(rectangleOfSize: pipeUp.size)
pipeUp.physicsBody.dynamic = false
pipePair.addChild(pipeUp)

pipePair.runAction(movePipesAndRemove);

// var movePipes = SKAction.repeatActionForever(SKAction.moveByX(-1, y: 0, duration: 0.02))
// pipePair.runAction(movePipes)

self.addChild(pipePair)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
Expand All @@ -65,21 +143,25 @@ class GameScene: SKScene {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)

let sprite = SKSpriteNode(imageNamed:"Spaceship")

sprite.xScale = 0.5
sprite.yScale = 0.5
sprite.position = location

let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)
bird.physicsBody.velocity = CGVectorMake(0, 0)
bird.physicsBody.applyImpulse(CGVectorMake(0, 30))

sprite.runAction(SKAction.repeatActionForever(action))

self.addChild(sprite)
}
}

func clamp(min: CGFloat, max: CGFloat, value: CGFloat) -> CGFloat {
if( value > max ) {
return max;
} else if( value < min ) {
return min;
} else {
return value;
}
}


override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
bird.zRotation = self.clamp( -1, max: 0.5, value: bird.physicsBody.velocity.dy * ( bird.physicsBody.velocity.dy < 0 ? 0.003 : 0.001 ) );
}
}
14 changes: 14 additions & 0 deletions FlappyBird/Images.xcassets/PipeDown.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x",

"filename" : "PipeDown.png"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions FlappyBird/Images.xcassets/PipeUp.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x",
"filename" : "PipeUp.png"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 733d3eb

Please sign in to comment.