7 Days Swift Challenge - Day 5: Class in Swift and Object Oriented Programming - How to Make an App
Today I'll share with you a new programming paradigm (a way, method, discipline, practice of programming) called Object Oriented Programming.
Your Code Challenge:
This is exciting! When we have Object Oriented Programming in our tool belt, we can do a lot more with programming!
Let's create the Flappy Bird character from the silly challenging game 👍
We will need to know the location of the bird on the screen so let's create a class called point (remember to use UpperCamelCase).
This class has two properties called x and y of type integer. Initialize their values to be 0 both.
Now let's create another class called flappy bird. Again, don't forget to use UpperCamelCasing!
The first property the bird has is its position which is a variable of type Point! Aha, we can actually have an instance of a class inside another class!
Initialize the property called position with Point().
Next, let's have another property called color of type string with the initial value of yellow.
Finally, its last property is isWearingHat to know if it's wearing a hat or not. It's type if Bool and the initial value is false.
Now let's put these two classes in play. Create an instance of the point class called pos and change its x and y to 10 and 20 respectively.
Create an instance (which is an object) of class FlappyBird and make its position to be the pos object we just had in step 8.
Change its color to red.
Change its isWearingHat to true.
Now the bird is moving forward so we need to change its x position to 19.
The user just makes the bird fly up, we need to change its y coordinate to 25.
This will be a little bit more challenging so please take your time.
Please try at least for 2 hours before watching my solution video!
Trust me, taking the time to building your programming foundation now will pay off tremendously later.
- Duc