Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flappyBird_version1 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions FlappySwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
873BE2A61D06195B00A69FF5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 873BE2A51D06195B00A69FF5 /* Assets.xcassets */; };
873BE2A91D06195B00A69FF5 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 873BE2A71D06195B00A69FF5 /* LaunchScreen.storyboard */; };
87AA11DC1D0B4D6E006C614F /* bird.atlas in Resources */ = {isa = PBXBuildFile; fileRef = 87AA11DB1D0B4D6E006C614F /* bird.atlas */; };
C2B44A101E9F7A9300CD4B11 /* FlyScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2B44A0F1E9F7A9300CD4B11 /* FlyScene.swift */; };
C2B44A121E9F7B0800CD4B11 /* FlyScene.sks in Resources */ = {isa = PBXBuildFile; fileRef = C2B44A111E9F7B0800CD4B11 /* FlyScene.sks */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -24,6 +26,8 @@
873BE2A81D06195B00A69FF5 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
873BE2AA1D06195B00A69FF5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
87AA11DB1D0B4D6E006C614F /* bird.atlas */ = {isa = PBXFileReference; lastKnownFileType = folder.skatlas; path = bird.atlas; sourceTree = "<group>"; };
C2B44A0F1E9F7A9300CD4B11 /* FlyScene.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FlyScene.swift; path = ../FlyScene.swift; sourceTree = "<group>"; };
C2B44A111E9F7B0800CD4B11 /* FlyScene.sks */ = {isa = PBXFileReference; lastKnownFileType = file.sks; path = FlyScene.sks; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -58,6 +62,8 @@
children = (
873BE29A1D06195B00A69FF5 /* AppDelegate.swift */,
873BE2A21D06195B00A69FF5 /* Main.storyboard */,
C2B44A0F1E9F7A9300CD4B11 /* FlyScene.swift */,
C2B44A111E9F7B0800CD4B11 /* FlyScene.sks */,
873BE2A01D06195B00A69FF5 /* ViewController.swift */,
873BE2A51D06195B00A69FF5 /* Assets.xcassets */,
87AA11DB1D0B4D6E006C614F /* bird.atlas */,
Expand Down Expand Up @@ -137,6 +143,7 @@
873BE2A91D06195B00A69FF5 /* LaunchScreen.storyboard in Resources */,
873BE2A61D06195B00A69FF5 /* Assets.xcassets in Resources */,
87AA11DC1D0B4D6E006C614F /* bird.atlas in Resources */,
C2B44A121E9F7B0800CD4B11 /* FlyScene.sks in Resources */,
873BE2A41D06195B00A69FF5 /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -149,6 +156,7 @@
buildActionMask = 2147483647;
files = (
873BE2A11D06195B00A69FF5 /* ViewController.swift in Sources */,
C2B44A101E9F7A9300CD4B11 /* FlyScene.swift in Sources */,
873BE29B1D06195B00A69FF5 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
14 changes: 9 additions & 5 deletions FlappySwift/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BV1-FR-VrT">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BV1-FR-VrT">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12086"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -14,9 +18,9 @@
<viewControllerLayoutGuide type="bottom" id="qHh-Mt-9TT"/>
</layoutGuides>
<view key="view" multipleTouchEnabled="YES" contentMode="scaleToFill" id="3se-qz-xqx" customClass="SKView">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="SZV-WD-TEh" sceneMemberID="firstResponder"/>
Expand Down
Binary file added FlappySwift/FlyScene.sks
Binary file not shown.
11 changes: 11 additions & 0 deletions FlappySwift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

guard let scene = FlyScene(fileNamed:
"FlyScene") else {
return
}

scene.size = self.view.bounds.size

sceneView.presentScene(scene)



// Configure scene view
sceneView.showsFPS = true
sceneView.showsNodeCount = true
Expand Down
105 changes: 105 additions & 0 deletions FlyScene.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// FlyScene.swift
// FlappySwift
//
// Created by Katja D on 13.04.17.
// Copyright © 2017 iOS Dev Kurs Universität Heidelberg. All rights reserved.
//

import UIKit
import SpriteKit

let worldCollisionCategory: UInt32 = 1 << 1
let obstacleCollisonCategory: UInt32 = 1 << 2
let birdCollisionCategory: UInt32 = 1 << 3

class FlyScene: SKScene, SKPhysicsContactDelegate {

let impulseOnFlap: CGFloat = 500

let bird: SKSpriteNode = {
let spriteNode = SKSpriteNode(imageNamed: "bird-01")
let physicsBody = SKPhysicsBody(circleOfRadius: spriteNode.size.width / 2)
physicsBody.mass = 1
physicsBody.linearDamping = 0
physicsBody.restitution = 1
physicsBody.collisionBitMask = worldCollisionCategory |
obstacleCollisonCategory
physicsBody.categoryBitMask = birdCollisionCategory
physicsBody.contactTestBitMask = obstacleCollisonCategory
spriteNode.physicsBody = physicsBody
return spriteNode

}()

let obstacles: SKNode = {
let node = SKNode()
return node
}()

override func didMove(to view: SKView) {
super.didMove(to: view)

self.physicsWorld.contactDelegate = self

self.addChild(bird)
bird.position = CGPoint(x: 0, y: 0)
let physicsBody = SKPhysicsBody(edgeLoopFrom: CGRect(x: -self.size.width / 2, y: -self.size.height / 2, width:
self.size.width, height: self.size.height))
physicsBody.collisionBitMask = worldCollisionCategory
self.physicsBody = physicsBody

self.addChild(obstacles)

self.obstacles.run(.repeatForever(.sequence([
.wait(forDuration: 2),
self.spawnObstacle,
])))

}

lazy var spawnObstacle: SKAction = {
let action = SKAction.run {
let upperObstacle = SKSpriteNode(imageNamed: "PipeDown")
let upperPhysicsBody = SKPhysicsBody(edgeLoopFrom:upperObstacle.frame)
upperPhysicsBody.categoryBitMask = obstacleCollisonCategory
upperObstacle.physicsBody = upperPhysicsBody
self.obstacles.addChild(upperObstacle)
upperObstacle.run(self.moveObstacle)

}
return action

}()

lazy var moveObstacle: SKAction = {
let distance: CGFloat = self.size.width
return .sequence([
.move(to: CGPoint(x: self.size.width / 2, y: 0), duration: 0),
.move(by: CGVector(dx: -distance, dy: 0), duration: 3),
.removeFromParent()
])
}()

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

if self.speed == 0 {
self.speed = 1
self.physicsWorld.speed = 1
bird.position = CGPoint(x: -100, y: -100)
bird.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
for obstacle in obstacles.children {
obstacle.removeFromParent()
}

}

bird.physicsBody?.applyImpulse(CGVector(dx: 0, dy: impulseOnFlap))
}

func didBegin(_ contact: SKPhysicsContact) {
print("Game over!")
self.speed = 0
self.physicsWorld.speed = 0
}
}