

Flappy Bird in Swift - pjvds
https://github.com/fullstackio/FlappySwift

======
gokhan
As a C# developer, I can read and understand the code without any issues.
That's a good thing for Apple. I'm sure Objective-C is great but it's too
foreign for me and didn't want to toy with it for fun, not worth the effort.
But I can write an app or two with this one.

~~~
matthewmacleod
I get where you're coming from - but in my experience, the barrier is totally
psychological. Objective C isn't that unusual a language - the syntax is a bit
foreign, but nothing that takes more than a couple of days to wrap your head
around.

You'll spend far more time getting to grips with Cocoa/Cocoa Touch.

~~~
untog
_the barrier is totally psychological_

Of course it is. It's certainly not a physical barrier. But psychological or
not, other languages don't have a similar barrier.

~~~
robterrell
> other languages don't have a similar barrier

Lisps have the RPN psychological barrier. Python has the significant
whitespace psychological barrier. etc. Any language can present _someone_ with
_some_ psychological barrier.

I think the OP's strongest point was, Objective-C the language is pretty
trivial to learn quickly (after all, it's just C with message passing). You'll
spend far longer trying to learn the Cocoa APIs than you will spend learning
Objective-C-the-language.

For instance, I decided to try to learn swift and SpriteKit at the same time.
I spent far longer looking up methods on SKSpriteNode than I did looking up
language constructs. (i.e. I assumed "let" was the same as in ES6 and couldn't
figure out why the compiler was mad.)

------
pajju
I just started exploring the Book: The Swift Programming Language by Apple
Inc.

Link: [https://itunes.apple.com/us/book/the-swift-programming-
langu...](https://itunes.apple.com/us/book/the-swift-programming-
language/id881256329?mt=11)

Hope it helps.

~~~
damncabbage
And the same, in HTML form:
[https://developer.apple.com/library/prerelease/ios/documenta...](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/index.html#//apple_ref/doc/uid/TP40014097-CH3-XID_0)

~~~
tvon
Note that near the top of "A Swift Tour" there is a link to the playground
which you can download and open in Xcode 6.

~~~
discohead
Wow, that is cool. Learning Swift is going to be fun.

Direct link to the playground file:
[https://developer.apple.com/library/prerelease/ios/documenta...](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.playground.zip)

------
kevinwang
I guess Flappy Bird is now a tier 2 "hello world" :)

~~~
misnome
I was disappointed that they didn't use it as "The simplest possible game" in
the WWDC presentation

~~~
dylz
Copyright...

~~~
zapu
Is it really copyrighted though?

~~~
masukomi
yes. IANAL but... copyright law in the US is such that, in essence, everything
is copyrighted (to some degree) the moment it is created unless you explicitly
state otherwise.

When dealing with anything legally distributed / created in the US there are
many questions, but "is it copywritten" should be one of the last ones,
because the answer is always YES.

------
jashmenn
Author here - I didn't expect to see this here this morning. I'd intended to
write a longer post :)

In any case, here's a few things I learned about swift yesterday building
this. Please note that I have about 4 hours swift experience, so feel free to
correct anything I say that's wrong.

1\. To make properties on a class you simply declare the variable on the class
e.g.:

    
    
        class GameScene: SKScene { 
          var bird = SKSpriteNode()
          // ...
        }
    

2\. The APIs generally have shorter names and it's really nice. E.g.

    
    
        SKTexture* birdTexture1 = [SKTexture textureWithImageNamed:@"Bird1"];
    
    

becomes

    
    
        var birdTexture1 = SKTexture(imageNamed: "Bird1")
    
    

If I understand it correctly, any overloading `inits` basically look like
calling the constructor on the class, whereas any class functions will be
called like this:

    
    
        var flap = SKAction.repeatActionForever(animation)
    
    

3\. You can put inline blocks and it's great

    
    
        var spawn = SKAction.runBlock({() in self.spawnPipes()})
    
    

4\. The typing is really strong - this takes some getting used to. For
instance, `arc4random()` returns a 32 bit unsigned integer. This means before
you can use any operators on it you have to make sure you're using compatible
types. e.g.

    
    
        var quarter = UInt32( self.frame.size.height / 4 )
        var y = arc4random() % quarter + quarter;
    

If we didn't use `UInt32` to convert `quarter` we'd get an error. After you
get the hang of this, it's actually really nice.

5\. I use `var` everywhere and I'm pretty sure I should be using `let` a lot
more. I haven't worked with Swift enough to have a strong intuition about when
to use either.

I should also mention that my code is just converted from Matthias Gall's code
[1].

I also want to put in a shameless plug that the point of making this was to
advertise the "Making Games with Swift" class that auser and I are building.
If you're interested, put in your email here:
[https://fullstackedu.com](https://fullstackedu.com)

I intend to redo this more fully with Playgrounds. I've been looking for a way
to teach kids programming for a while now (if you recall, auser and I built
Choc [2] a few months back). I think Playgrounds in Swift are finally the tool
we've been waiting for.

[1] [http://digitalbreed.com/2014/how-to-build-a-game-like-
flappy...](http://digitalbreed.com/2014/how-to-build-a-game-like-flappy-bird-
with-xcode-and-sprite-kit)

[2] [http://www.fullstack.io/choc/](http://www.fullstack.io/choc/)

EDIT: added choc

~~~
kevin818
I think just the fact that you were able to build a game with just 4 hours of
learning a new language speaks volumes about Swift's barrier to entry. Well
done!

~~~
CmonDev
Any up-to-date C# developer already knows Swift.

~~~
tobinharris
Any up-to-date Java developer already knows Swift :)

~~~
jffry
Any up-to-date Swift developer already knows Swift :-p

------
matthewmacleod
Good work. I'll have to do 2048 instead, I guess :)

I'm quite impressed so far. Having been an Objective C and Ruby engineer, so
far Swift seems to offer the best of both.

That said, OpenGL support doesn't appear to be finished yet.

------
stigi
If you're new to functional programming styles this swift project might be
more relevant:
[https://github.com/maxpow4h/swiftz](https://github.com/maxpow4h/swiftz)

------
diminish
I got mixed feelings about the language at first sight. I guess my mammalian
brain recognizes languages based on the particular combination of the
following naming decisions.

* func, function, fun, defun fu, funct

* CamelCase vs snake_case

* whitespace, semicolon or comma usage

* var, int/integer/uint64/Integer/

* choice of (), {}, [] or better (){a[]}

* import/include/require, class/class, override, self vs this, new vs Class()

PS: next time you design a new language just make a random unique combination
of the above.

~~~
fdej
> PS: next time you design a new language just make a random unique
> combination of the above.

That's my impression whenever I see a new programming language too. Why would
someone switch to your language if the syntax is just the same boring old
syntax they've been using in another language?

~~~
jaxytee
Because in this case, it's an alternative to a language (Objective C) with a
not so boring esoteric syntax.

~~~
annnnd
Not boring at all - but I'm not sure this helps adoption any. :)

------
kclay
Always had a problem with Objective C, could never read it (Android Dev) but
this right here is pretty impressive. I like the mixture of language features.
But my only question, are you still locked to using an mac to develop for iOS.
I guess since the language is closed source it depends on some osx libs at
compile time.

~~~
craigching
Yes, you still need a Mac to develop for iOS. Come to the dark side! :)

~~~
viiralvx
Android developer here with a Macbook Pro. I'm honestly thinking about taking
the leap and getting an iPhone, depending on how the 6 looks like. But Swift
enticed me very much.

~~~
herinkc
You won't regret it

------
LukeB_UK
From the GIF in the readme, it looks like collision detection is broken.

~~~
jashmenn
I'm terrible at flappy bird - I can /sometimes/ get through the first pipe.
Rarely.

I didn't build the collision detection yet just b/c I actually think it's more
fun to play this way.

That said, we'll add it before too long.

------
ajanuary
This being the only code sample of swift I've seen, my overriding takeaway is
that for the basic stuff it's remarkably similar to Objective-C with a lick of
paint.

If people really take to swift, it'll be interesting to see if that's because
it creates a shift in programming style, or because people really are just
afraid of small syntactical differences.

~~~
andybak
Readability matters - maybe the Objective-C verbiage melts away once you're up
to speed, but it certainly puts you off wanting to get there.

~~~
jurre
I find obj-c super readable, other people don't? I mean, take two hours to get
used to the bracket syntax for message sending and it's super readable
although a bit verbose?

~~~
ktran03
Same here.

All you have to do is get over named parameters, square bracket message
passing, and overall verbosity, and you know enough to read/understand
Objective-C code.

If you can get past those three differences, Objective-C is remarkably similar
to Java and other OOP languages.

Personally, I love the verbosity. The code pretty much just explains itself.

Swift is a welcomed change though. I'm very optimistic and excited about the
future of iOS going forward.

~~~
andybak
Please take this in good humour but I couldn't resist:

> All you have to do is get over named parameters, square bracket message
> passing, and overall verbosity

Translation: "All you have to do is get over it's poor readability and it's
quite readable!"

> Objective-C is remarkably similar to Java and other OOP languages.

Translation: "Objective-C is as readable as several other unreadable
languages".

;-)

Readability isn't about familiarity as much as it's about clarity and a lack
of boilerplate. Verbosity is in most cases the antithesis if readability. I
think there can be exceptions (maybe some verbose DSLs that manage to map
cleanly from your brain to the code on the page) but if the verbosity comes
from boilerplate or visual clutter then verbosity is a strong point against.

------
QuadDamaged
I am really intrigued by the obj-c interop capability of swift, namely
interactions between blocks and closures / anonymous functions.

I can see my AFNetworking code becoming much, much more readable now, without
the need to @weakify/@strongify self on both sides of the block, but just add
a blanket '[unowned self] in' inside the closure.

------
barrystaes
Swift is a new programming language. Does this implementation also use Metal?
What is the Scene Kit relation to Metal?

~~~
jemeshsu
SpriteKit and SceneKit is targeting casual games. Metal is an API that is
close to GPU and is meant to replace OpenGL for console level games.

------
k-mcgrady
This may be a stupid question but is the language in some way tailored to game
programming? Apple's examples at WWDC were game companies, their coding demo
was a game, and this is the first project I've seen written in it - and it's a
game.

~~~
ajanuary
Games make easy examples because you can do a simple render loop and input.
Most people understand the concept of sprites and basic animation.

Apple's UI frameworks are all MVC based, which means multiple classes and
apple specific UI terminology.

~~~
eddieroger
Additionally, they're a quick route between code and something visual. If the
demo yesterday had been "look how easy I can make a tableview", no one would
have cared much, but the circus scene had motion, lights, and barely any code.
People love spectacle.

That reads a lot more sarcastic than it should have, but it's unfortunately
true. Swift was announced in front of a room of people who needed to be
convinced to start migrating from something they all know (ObjC) in favor of
something new, so you have to win them over quick.

------
ktg
Flappy Bird in Racket | [https://github.com/soegaard/flappy-
bird](https://github.com/soegaard/flappy-bird)

~~~
dukerutledge
Man, apparently dr racket format looks like a hot mess.

~~~
soegaard
A normal Racket file is a straight forward text file.

In Flappy Bird I used inline images, as in (define bird <the-bird-image-here>
) This makes it simple to distribute the source and images as a single file,
but unfortunately the resulting file is not pretty.

In short: to see the source open it in DrRacket.

------
kayoone
pretty neat! I am not an iOS developer but if i understand correctly this uses
the new Sprite Kit stuff included in iOS8 for 2d rendering right ? Is this a
threat to existing 2d/game engines ? Not sure where SpriteKit integrates into
the existing stack for making a game.

~~~
babuskov
Looks like a good way for Apple to lock the developers to their platform. In
past glory days people used to make games for iOS first and consider porting
to Android much later. These days developers think cross-platform from the
start, esp. since it's much easier to test waters and iterate on Android. I
guess that SpriteKit is an attempt to stop this trend. Smart move by Apple
IMHO, but perhaps a little bit too late.

~~~
chillingeffect
I see your polite, critical, level-headed opinion has been down voted into
gray dust, despite my meager support. I entreat those who pass by here to
drink in my own following opinion: I think you're operating from the
democratic ideal that "all people will opine equally about all topics at all
times."

The departure from this ideal, I've discovered, is that there is a "Practical
Crowd" who will swoop through when questions of practicality, such as cross-
platform compatibility come up. Their opinions will be nearly absolute and
they will be all but intolerant of the "weak" who claim that merely targeting
Windows, Linux and OSX is "enough." They will justify with numerous cherry-
picked examples from history, starkly, if not carefully, considered arguments
toward the future, and with evidence of grit on their fingers from the
present, in which the particular ideal is espoused. At that time, they will
down vote anyone deviant enough to cheer for Swift.

Yet, currently my friend, the Apple Crowd has the floor. Any practical
concerns are scheduled after the celebration. Think of this when you read the
next cross-platform thread and see the Apple fans trying to get a point in
edgewise amongst The Practical. There is very little global reality, mainly
local basins of morality.

------
alexcroox
Well that didn't take long...

------
Zelphyr
If Swift has namespaces why are classes still prefixed? "SKScene" for example.

~~~
rys
Objective-C interop.

------
brador
Does this need IOS8 to run?

Is there a way to get that without being a signed up dev with Apple?

~~~
martingordon
Apple has stated that Swift apps can target iOS 7/8 and Mavericks/Yosemite.
You do need access to the dev program in order to get access to the Xcode 6
beta.

------
seanhandley
Someone needs to add Swift to the linguist gem

------
martinvol
OK, that was fast!

------
joeyspn
2k+ Github stars in less than 24h? OMG

------
hellbreakslose
Swift looks fine, the only thing I don't like its that is for Apple only
products... that kinda defeats the purpose of having a programming language.

~~~
mnem
Good luck compiling Objective-C on Windows :)

~~~
matthewmacleod
Here you go:
[http://www.gnustep.org/experience/Windows.html](http://www.gnustep.org/experience/Windows.html)

~~~
mnem
Ye-es, except:

    
    
      "Apple has recently added new functionality to their runtime, including built-in exception handling, etc. Hopefully these will be ported to the GNU runtime in the future."
    

My general point is that programming languages don't _have_ to be cross
platform to be useful. Certainly handy from a developer's point of view, but
just a convenience.

------
napolux
FAST AS HELL! :P Thanks a lot!

------
supergeek133
Why.

------
nakovet
I was curious when I saw a tests folders, but it was just auto-generated files
with no actual tests. =(

~~~
craigching
Swift hasn't even been out for 24 hours yet, give the guy a break! ;)

------
nicolime
Share the latest about Swift here! Be part of the biggest page for the
language. Looking for admins.
[https://www.facebook.com/swiftofficial](https://www.facebook.com/swiftofficial)

