Swift learns the first practice--a Flappybird game implemented with Swift

Source: Internet
Author: User

Flappybird mini-game with Swift

With Apple's deep-dive approach to swift, Swift's quick fire and Swift's streamlined syntax and powerful features, it's also necessary to learn about Swift for developers who use OBJECT-C to develop iOS. This blog skips Swift's dry grammar and starts using swift directly from a small game project, summarizing the gains as follows:

Flappybird is a small game that was very hot in the previous period, by tapping the screen by finger to balance the bird through the barrier. I was the previous OC version of the project to change to Swift, so the overall idea is OC's development ideas.

First, I need to define two macros, one to simulate the gravitational acceleration g, one for easy access to the device screen size. Because this game is very simple, it only takes a few hours to develop, so we just need to write the code in one file: Viewcontroller.swift.

There is no macro definition in the general language in swift, but you can implement the effect of a macro by defining a constant:

A macro in the form of a constant proxy OC defines let g:float=9.8let screen_size = Uiscreen.mainscreen (). Bounds

we need to define some member variables, as follows:

Class Viewcontroller:uiviewcontroller {var timer:nstimer?//background move timer var i:int=0//background move speed var timer2:nstimer?//column    Sub and ground moving timer var timer3:nstimer?//bird Mobile timer var bird:uiimageview? var t:float=0.0//bird falling speed var isdowm:bool=false//flag whether the bird is in the drop Var isgameover:bool=false//mark whether the game is over}

for? And! Understanding of the Internet, a lot of concepts, simple to understand the declaration of variables if the system is not initialized to the variable will not be assigned nil, error,? The function is to tell the system if it is nil without initialization. Similarly, when using this kind of variable, also need to add? Unpacking, if added! is to force the unpacking, it can be understood to let the system think that this variable must not nil.

For some functions such as UI creation and OC, just call the way slightly different, the following will be attached to the source code.

Part of the code to control the whereabouts of the bird is as follows, one thing to note that there is no hermit conversion concept in Swift, such as you want to use int A + float b you must manually convert int to float: (float) (a) +b

Func birdmove () {        if !isdowm{             if bird?. frame.origin.y < screen_size.height-100{                 var rant:cgrect=bird!. frame                 rant.origin.y +=  (CGFloat) (g* (T*T/2))                  bird?. frame=rant                t+= 0.025            }         }else{            if t< 0.24{                var rant = bird?. Frame                rant?. origin.y -= 4.9-(CGFloat) (G*T*T/2)                  bird?. frame=rant!                t+= 0.025            }else{                 isDowm=false             }        }     }

The game is as follows:


I believe that practice is the way to learn, hope and like-minded friends, progress together.

Project GitHub Address: Https://github.com/ZYHshao/swiftFlappyBird

Swift learns the first practice--a Flappybird game implemented with Swift

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.