Skip to content

Commit

Permalink
Merge pull request #6 from JarWarren/develop
Browse files Browse the repository at this point in the history
prevent flapping when offscreen
  • Loading branch information
JarWarren authored Oct 2, 2020
2 parents 3babf96 + 5c05e39 commit 7882608
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 12 deletions.
4 changes: 4 additions & 0 deletions FlappyBird.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
7E04B6832526FF2900EA1A47 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E04B6822526FF2900EA1A47 /* Constants.swift */; };
7EBCD035251ADA7800EAE987 /* SKScene+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7EBCD034251ADA7800EAE987 /* SKScene+Extension.swift */; };
7EBCD04D251B173100EAE987 /* point.wav in Resources */ = {isa = PBXBuildFile; fileRef = 7EBCD048251B173000EAE987 /* point.wav */; };
7EBCD04E251B173100EAE987 /* hit.wav in Resources */ = {isa = PBXBuildFile; fileRef = 7EBCD049251B173100EAE987 /* hit.wav */; };
Expand All @@ -28,6 +29,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
7E04B6822526FF2900EA1A47 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
7EBCD034251ADA7800EAE987 /* SKScene+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SKScene+Extension.swift"; sourceTree = "<group>"; };
7EBCD048251B173000EAE987 /* point.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = point.wav; sourceTree = "<group>"; };
7EBCD049251B173100EAE987 /* hit.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = hit.wav; sourceTree = "<group>"; };
Expand Down Expand Up @@ -106,6 +108,7 @@
7EBCD047251B171700EAE987 /* Audio */,
7EC572FA2518689000BE760C /* AppDelegate.swift */,
7EC573072518689100BE760C /* Assets.xcassets */,
7E04B6822526FF2900EA1A47 /* Constants.swift */,
7EC573092518689100BE760C /* Info.plist */,
7EBCD053251B177500EAE987 /* Launch.storyboard */,
);
Expand Down Expand Up @@ -246,6 +249,7 @@
7EC573012518689000BE760C /* GameScene.swift in Sources */,
7EC573032518689000BE760C /* GameViewController.swift in Sources */,
7EBCD035251ADA7800EAE987 /* SKScene+Extension.swift in Sources */,
7E04B6832526FF2900EA1A47 /* Constants.swift in Sources */,
7EC572FB2518689000BE760C /* AppDelegate.swift in Sources */,
7EC5733925188B3700BE760C /* Bird.swift in Sources */,
7EC5737525196A4F00BE760C /* Score.swift in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions FlappyBird/Classes/Bird.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import SpriteKit

class Bird: SKSpriteNode {

private var flapSound: SKAction { .playSoundFileNamed("wing.wav", waitForCompletion: false) }
private var deathSound: SKAction { .playSoundFileNamed("die.wav", waitForCompletion: false) }
private var flapSound: SKAction { .playSoundFileNamed(Constants.sounds.flap, waitForCompletion: false) }
private var deathSound: SKAction { .playSoundFileNamed(Constants.sounds.death, waitForCompletion: false) }
private var maximumUpwardVelocity: CGFloat = 18
private var upwardVelocity: CGFloat = 0
private var maximumAngularVelocity: CGFloat = 1
Expand All @@ -20,7 +20,7 @@ class Bird: SKSpriteNode {
private var state: GameState = .ready

func flap() {
guard isAlive else { return }
guard isAlive, position.y < Constants.gameplay.birdUpperLimit else { return }
removeAllActions()
upwardVelocity = maximumUpwardVelocity
zRotation = 0.7
Expand Down
2 changes: 1 addition & 1 deletion FlappyBird/Classes/Ground.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension Ground: GameObject {

func update() {
guard shouldUpdate else { return }
segments.forEach { $0.position.x -= 4 }
segments.forEach { $0.position.x -= Constants.gameplay.gameSpeed }
if segments[leadSegment].position.x <= -820 {
segments[leadSegment].position.x += segments[leadSegment].size.width * 3
updateLeadSegment()
Expand Down
12 changes: 7 additions & 5 deletions FlappyBird/Classes/Pipe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class Pipe: SKNode {
weak var delegate: PipeDelegate?

private func reset() {
position.x += 1080
position.y = CGFloat.random(in: -80...240)
if position.x < Constants.gameplay.pipeLimitRight {
position.x += 1080
}
position.y = CGFloat.random(in: Constants.gameplay.pipeSafeRange)
hasScored = false
}
}
Expand All @@ -30,14 +32,14 @@ extension Pipe: GameObject {

func update() {
guard shouldUpdate else { return }
position.x -= 4
position.x -= Constants.gameplay.gameSpeed

if !hasScored && position.x <= -160 {
if !hasScored && position.x <= Constants.gameplay.birdPosition {
hasScored = true
delegate?.pipeDidScore()
}

if position.x <= -640 {
if position.x <= Constants.gameplay.pipeLimitLeft {
reset()
}
}
Expand Down
Binary file modified FlappyBird/GameScene/GameScene.sks
Binary file not shown.
6 changes: 3 additions & 3 deletions FlappyBird/GameScene/GameScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ protocol GameObject: AnyObject {
class GameScene: SKScene {

private var onCooldown = false
private var collisionSound: SKAction { .playSoundFileNamed("hit.wav", waitForCompletion: false) }
private var scoreSound: SKAction { .playSoundFileNamed("point.wav", waitForCompletion: false) }
private var swooshSound: SKAction { .playSoundFileNamed("swoosh.wav", waitForCompletion: false) }
private var collisionSound: SKAction { .playSoundFileNamed(Constants.sounds.collision, waitForCompletion: false) }
private var scoreSound: SKAction { .playSoundFileNamed(Constants.sounds.score, waitForCompletion: false) }
private var swooshSound: SKAction { .playSoundFileNamed(Constants.sounds.swoosh, waitForCompletion: false) }
private lazy var background = childNode(withName: "Background") as! Background
private lazy var readyMessage = childNode(withName: "Message") as! SKSpriteNode
private lazy var gameOverMessage = childNode(withName: "GameOver") as! SKSpriteNode
Expand Down
32 changes: 32 additions & 0 deletions FlappyBird/Resources/Constants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Constants.swift
// FlappyBird
//
// Created by Jared Warren on 10/2/20.
//

import CoreGraphics

enum Constants {

static let sounds = SoundConstants()
static let gameplay = GameplayConstants()

}

struct SoundConstants {
let collision = "hit.wav"
let death = "die.wav"
let flap = "wing.wav"
let score = "point.wav"
let swoosh = "swoosh.wav"
}

struct GameplayConstants {
let birdPosition: CGFloat = -160
let birdUpperLimit: CGFloat = 600
let gameSpeed: CGFloat = 4
let pipeLimitLeft: CGFloat = -640
let pipeLimitRight: CGFloat = 2000
let pipeSafeRange: ClosedRange<CGFloat> = -80...240
}

0 comments on commit 7882608

Please sign in to comment.