Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions FlappyBird/GameScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate{
let verticalPipeGap = 150.0

var verticalPipeGap = 200.0
var level:Int = 0
var bird:SKSpriteNode!
var skyColor:SKColor!
var pipeTextureUp:SKTexture!
Expand All @@ -21,7 +21,8 @@ class GameScene: SKScene, SKPhysicsContactDelegate{
var canRestart = Bool()
var scoreLabelNode:SKLabelNode!
var score = NSInteger()

var stageScore:Int = 0;
var levelLabelNode:SKLabelNode!;
let birdCategory: UInt32 = 1 << 0
let worldCategory: UInt32 = 1 << 1
let pipeCategory: UInt32 = 1 << 2
Expand All @@ -32,7 +33,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate{
canRestart = true

// setup physics
self.physicsWorld.gravity = CGVector( dx: 0.0, dy: -5.0 )
self.physicsWorld.gravity = CGVector( dx: 0.0, dy: -6.0 )
self.physicsWorld.contactDelegate = self

// setup background color
Expand Down Expand Up @@ -137,7 +138,13 @@ class GameScene: SKScene, SKPhysicsContactDelegate{
scoreLabelNode.position = CGPoint( x: self.frame.midX, y: 3 * self.frame.size.height / 4 )
scoreLabelNode.zPosition = 100
scoreLabelNode.text = String(score)

levelLabelNode = SKLabelNode(fontNamed:"MarkerFelt-Wide")
levelLabelNode.position = CGPoint( x: self.frame.midX, y: 1 * self.frame.size.height / 4 )
levelLabelNode.zPosition = 10
levelLabelNode.text = "lvl:" + String(stageScore)
self.addChild(scoreLabelNode)
self.addChild(levelLabelNode)

}

Expand Down Expand Up @@ -199,16 +206,20 @@ class GameScene: SKScene, SKPhysicsContactDelegate{

// Reset score
score = 0
stageScore = 0
scoreLabelNode.text = String(score)

levelLabelNode.text = "lvl:" + String(stageScore)
verticalPipeGap = 200.0

// Restart animation
moving.speed = 1
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if moving.speed > 0 {
for _ in touches { // do we need all touches?
bird.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
bird.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30))
bird.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 22))
}
} else if canRestart {
self.resetScene()
Expand All @@ -227,6 +238,13 @@ class GameScene: SKScene, SKPhysicsContactDelegate{
// Bird has contact with score entity
score += 1
scoreLabelNode.text = String(score)
//Update level and diffeculty
if score == stageScore + 8{
stageScore += 8
levelLabelNode.text = "lvl:" + String(stageScore/8)
speed += 0.1
verticalPipeGap = verticalPipeGap - 20.0
}

// Add a little visual feedback for the score increment
scoreLabelNode.run(SKAction.sequence([SKAction.scale(to: 1.5, duration:TimeInterval(0.1)), SKAction.scale(to: 1.0, duration:TimeInterval(0.1))]))
Expand Down