O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Swift Game Programming for Absolute Beginners

Book Description

Swift Game Programming for Absolute Beginners teaches Apple’s Swift language in the context of four, fun and colorful games. Learn the Swift 2.0 language, and learn to create game apps for iOS at the same time – a double win!

The four games you’ll develop while reading this book are:

  • Painter
  • Tut’s Tomb
  • Penguin Pairs
  • Tick Tick
  • These four games are casual, arcade-style games representing the aim-and-shoot, casual, puzzle, and platform styles of game play. Professionally developed game assets form part of the book download. You’ll get professionally drawn sprites and imagery that’ll have you proud to show your learning to friends and family.

    The approach in Swift Game Programming for Absolute Beginners follows the structure of a game rather than the syntax of a language. You’ll learn to create game worlds, manage game objects and game states, define levels for players to pass through, implement animations based upon realistic physics, and much more. Along the way you’ll learn the language, but always in the context of fun and games.

    Swift is Apple’s new programming language introduced in 2014 to replace Objective-C as the main programming language for iOS devices and Mac OS X. Swift is a must learn language for anyone targeting Apple devices, and Swift Game Programming for Absolute Beginners provides the most fun you’ll ever have in stepping over the threshold toward eventual mastery of the language.

    Table of Contents

    1. Cover
    2. Title
    3. Copyright
    4. Dedication
    5. Contents at a Glance
    6. Contents
    7. About the Author
    8. About the Technical Reviewer
    9. Acknowledgments
    10. Introduction
    11. Part I: Getting Acquainted
      1. Chapter 1: The Swift Language
        1. Computers and Programs
          1. Processors and Memory
          2. Programs
          3. Programming Languages
        2. Programming Games
        3. Developing Games
          1. Small Scale: Edit-Compile-Run
          2. Large Scale: Design-Specify-Implement
        4. Building Your First Swift Program
        5. Building Your First Swift Game
        6. A Few Observations
        7. What You Have Learned
      2. Chapter 2: Game Programming Basics
        1. Building Blocks of a Game
        2. The Game World
          1. The Game Loop
          2. The Game Loop in Swift
        3. The Structure of a Program
          1. Types of Applications
          2. Functions
          3. Syntax Diagrams
          4. Calling a Function
        4. Program Layout
          2. Instructions vs. Lines
          3. Whitespace and Indentation
        5. What You Have Learned
      3. Chapter 3: Creating a Game World
        1. Basic Types and Variables
          1. Types
          2. Declaration and Assignment of Variables
          3. Instructions and Expressions
        2. Operators and More Complex Expressions
          1. Arithmetic Operators
          2. Priority of Operators
        3. Other Numeric Types
        4. The DiscoWorld Game
        5. Scope of Variables
        6. What You Have Learned
      4. Chapter 4: Game Assets
        1. Locating Sprites
        2. Loading and Drawing Sprites
        3. Resolutions and Aspect Ratios
        4. Moving Sprites
        5. Loading and Drawing Multiple Sprites
        6. Configuring the Device Orientation
        7. Music and Sounds
        8. What You Have Learned
    12. Part II: Getting Acquainted
      1. Chapter 5: Reacting to Player Input
        1. Dealing With Touch Input
        2. Using the Touch Location to Change the Game World
        3. Conditional Execution Based on Touch
        4. Testing for Alternatives
        5. Comparison Operators
        6. Logic Operators
        7. The Boolean Type
        8. Changing the Color of the Cannon
        9. A Few Final Remarks
        10. What You Have Learned
      2. Chapter 6: A Flying Ball
        1. Methods
        2. Parameter Names and Labels
        3. Default Parameter Values
        4. Reorganizing Instructions into Methods
        5. Moving Between Local and World Coordinates
        6. Adding a Ball to the Game World
          1. Shooting the Ball
          2. Updating the Ball Position
        7. Fixed Timestep vs. Variable Timestep
        8. Updating the Ball Color
        9. What You Have Learned
      3. Chapter 7: Game Object Types
        1. Creating Multiple Objects of the Same Type
        2. Classes as Types
        3. Input Handling in a Separate Class
        4. Initializing Objects
        5. The Self Keyword
        6. Accessing Other Objects Using Static Variables
        7. The Double Role of Classes
        8. Writing a Class with Multiple Instances
        9. Dealing with Randomness in Games
        10. Calculating a Random Velocity and Color
        11. Updating the Paint Can
        12. What You Have Learned
      4. Chapter 8: Colors and Collisions
        1. A Different Way to Represent Colors
        2. Controlled Data Access for Objects
        3. Adding a Computed Property to a Class
        4. Handling Collisions Between the Ball and the Cans
        5. Values and References
        6. Structs
        7. What You Have Learned
      5. Chapter 9: Limited Lives
        1. Maintaining the Number of Lives
        2. Indicating the Number of Lives to the Player
        3. Executing Instructions Multiple Times
        4. A Shorter Notation for Incrementing Counters
        5. A More Compact Looping Syntax
        6. A Few Special Cases
          1. No Repeat at All
          2. Infinite Repeat
          3. Nested Repeats
        7. Restarting the Game
        8. What You Have Learned
      6. Chapter 10: Organizing Game Objects
        1. Similarities Between Game Objects
        2. Inheritance
        3. Game Objects and Inheritance
        4. Cannon as a Subclass of ThreeColorGameObject
        5. The Ball Class
        6. The PaintCan Class
        7. Polymorphism
        8. Inheriting from Existing Classes
        9. Hierarchies of Classes
        10. What You Have Learned
      7. Chapter 11: Finishing the Painter Game
        1. Adding Motion Effects
        2. Adding Sounds and Music
        3. Maintaining a Score
        4. Characters and Strings
          1. Special Characters
        5. Adding App Icons
        6. A Few Final Remarks
        7. What You Have Learned
    13. Part III: Getting Acquainted
      1. Chapter 12: Advanced Input Handling
        1. Creating a Touch Object
        2. Arrays
        3. Dictionaries
        4. Optionals
        5. Storing Multiple Touches
        6. Making Dealing with Touch Input Easier
        7. Linking Touch Events to the Input Helper Class
        8. Dragging Sprites
        9. What You Have Learned
      2. Chapter 13: Game Physics
        1. A Basic Class for Game Objects
        2. Game Object Subclasses
        3. Adding Physics to Game Objects
        4. Interaction
        5. What You Have Learned
      3. Chapter 14: Gameplay Programming
        1. Game Object Behavior
        2. Actions
        3. Using Actions to Drop Treasures
        4. Special Types of Treasure
        5. Turning Treasures Into Rocks
        6. Handling Physics Contacts
        7. What You Have Learned
      4. Chapter 15: Game States
        1. Better Layer Handling
        2. Adding a Title Screen
        3. Adding a Button to Show a Help Frame
        4. Overlays
        5. What You Have Learned
      5. Chapter 16: Finishing the Tut’s Tomb Game
        1. Adding a Score
        2. Controlling Access
        3. Using a Custom Font
        4. Adding Glitter
        5. Adding More Glitter
        6. Adding Music and Sound Effects
        7. What You Have Learned
    14. Part IV: Getting Acquainted
      1. Chapter 17: Menus and Grids
        1. Game Objects in a Grid Layout
        2. Class Extensions
        3. Setting Up a Menu
        4. Adding an On/off Button
        5. Defining a Slider Button
        6. Calculating World Positions of Game Objects
        7. Completing the Slider Class
        8. What You Have Learned
      2. Chapter 18: Game State Management
        1. Basics of Managing Game States
        2. The Game State Manager
        3. Assigning Names to Nodes
        4. Adding States and Switching Between Them
        5. The Level Menu State
        6. What You Have Learned
      3. Chapter 19: Storing and Recalling Game Data
        1. Structure of a Level
        2. Reading Data From Files
        3. The Tile Class
        4. The Level State
          1. Using switch to Handle Alternatives
          2. Loading Different Kinds of Tiles
        5. Maintaining the Player’s Progress
        6. What You Have Learned
      4. Chapter 20: Interaction Between Game Objects
        1. Defining Operators
        2. Selecting Penguins
        3. Updating Animals
        4. Meeting Other Game Objects
        5. Maintaining the Number of Pairs
        6. What You Have Learned
      5. Chapter 21: Finishing the Penguin Pairs Game
        1. Showing Hints
        2. Showing a Help Frame
        3. Resetting the Level
        4. Moving to the Next Level
        5. Adding Sound Effects
        6. A Few Final Notes
        7. What You Have Learned
    15. Part V: Getting Acquainted
      1. Chapter 22: The Main Game Structure
        1. Overview of the Game Structure
        2. The Structure of a Level
        3. Water Drops
        4. The Tile Class
        5. The LevelState Class
        6. What You Have Learned
      2. Chapter 23: Animation
        1. What Is Animation?
        2. Texture Atlases
        3. The Animation Class
        4. Supporting Multiple Animations
        5. The Player Class
        6. What You Have Learned
      3. Chapter 24: Platform Game Physics
        1. Locking the Character in the Game World
        2. Setting the Character at the Right Position
        3. Jumping …
        4. … And Falling
        5. Collision Detection
        6. Retrieving Bounding Boxes
        7. Handling Character-Tile Collisions
        8. Dealing with the Collision
        9. What You Have Learned
      4. Chapter 25: Intelligent Enemies
        1. The Rocket
          1. Creating and Resetting the Rocket
          2. Programming the Rocket Behavior
        2. A Patrolling Enemy
          1. The Basic PatrollingEnemy Class
          2. Different Types of Enemies
        3. Other Types of Enemies
        4. Loading the Different Types of Enemies
        5. What You Have Learned
      5. Chapter 26: Adding Player Interaction
        1. Collecting Water Drops
        2. Ice Blocks
        3. Enemies Colliding With the Player
        4. Adding Horizontal and Vertical Scrolling
        5. What You Have Learned
      6. Chapter 27: Finishing the Tick Tick Game
        1. Adding a Timer
          1. Making the Timer Go Faster or Slower
          2. When the Timer Reaches Zero
        2. Adding Mountains and Clouds
        3. Showing the Help Frame
        4. Finalizing the Level Progression
        5. What You Have Learned
    16. Index