Game Development with Construct 2

From Design to Realization

Lee Stemkoski Evan Leider Game Development with Construct 2: From Design to Realization Lee Stemkoski Evan Leider Garden City, New York NY USA USA ISBN-13 (pbk): 978-1-4842-2783-1 ISBN-13 (electronic): 978-1-4842-2784-8 DOI 10.1007/978-1-4842-2784-8 Copyright © 2017 by Lee Stemkoski and Evan Leider This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Editorial Director: Todd Green Acquisitions Editor: Pramila Balan Development Editor: Laura Berendson Technical Reviewer: Julien Kyatric Fantoni Coordinating Editor: Prachi Mehta Copy Editor: Kim Wimpsett Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Cover image designed by Freepik Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit www.apress. com/rights-permissions. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook ­versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at www.apress.com/bulk-sales. Any code or other supplementary material referenced by the author in this book is avail- able to readers on GitHub via the book’s product page, located at www.apress.com/978-1-4842- 2783-1. For more detailed information, please visit www.apress.com/source-code. Printed on acid-free paper Contents at a Glance

About the Authors ��������������������������������������������������������������������������� xiii About the Technical Reviewer ��������������������������������������������������������� xv Acknowledgments ������������������������������������������������������������������������� xvii Foreword ���������������������������������������������������������������������������������������� xix Introduction ����������������������������������������������������������������������������������� xxi

■■Chapter 1: Getting Started with Construct 2 ���������������������������������� 1 ■■Chapter 2: Starfish Collector ���������������������������������������������������������� 9 ■■Chapter 3: Space Rocks ���������������������������������������������������������������� 27 ■■Chapter 4: Cleanup Challenge ������������������������������������������������������ 49 ■■Chapter 5: Adding Polish to Your Game ���������������������������������������� 65 ■■Chapter 6: Plane Dodger ��������������������������������������������������������������� 79 ■■Chapter 7: Racecar 500 ���������������������������������������������������������������� 89 ■■Chapter 8: Rectangle Destroyer �������������������������������������������������� 103 ■■Chapter 9: Spell Shooter ������������������������������������������������������������� 115 ■■Chapter 10: Airplane Assault ������������������������������������������������������ 131 ■■Chapter 11: Tower Defenders ����������������������������������������������������� 141 ■■Chapter 12: Maze Runman ��������������������������������������������������������� 159 ■■Chapter 13: Jumping Jack ��������������������������������������������������������� 175

iii ■ Contents at a Glance

■■Chapter 14: Treasure Quest �������������������������������������������������������� 197 ■■Chapter 15: The Journey Continues ������������������������������������������� 223 ■■Appendix A: Game Design Documentation ��������������������������������� 231

Index ���������������������������������������������������������������������������������������������� 237

iv Contents

About the Authors ��������������������������������������������������������������������������� xiii About the Technical Reviewer ��������������������������������������������������������� xv Acknowledgments ������������������������������������������������������������������������� xvii Foreword ���������������������������������������������������������������������������������������� xix Introduction ����������������������������������������������������������������������������������� xxi

■■Chapter 1: Getting Started with Construct 2 ���������������������������������� 1 About the Construct 2 ������������������������������������������������������� 2 Downloading and Installing ��������������������������������������������������������������������� 2 The User Interface ����������������������������������������������������������������������������������� 3 Saving, Previewing, and Exporting Games ���������������������������������������������� 7 Summary ������������������������������������������������������������������������������������������������� 8 ■■Chapter 2: Starfish Collector ���������������������������������������������������������� 9 Project Setup ������������������������������������������������������������������������������������������� 9 Sprites ��������������������������������������������������������������������������������������������������� 13 Events ���������������������������������������������������������������������������������������������������� 16 Behaviors ���������������������������������������������������������������������������������������������� 18 Ending the Game ����������������������������������������������������������������������������������� 21 Side Quests ������������������������������������������������������������������������������������������� 22 Solid Objects �����������������������������������������������������������������������������������������������������������22 Value-Based Animations �����������������������������������������������������������������������������������������23 On Your Own �����������������������������������������������������������������������������������������������������������25 Summary ����������������������������������������������������������������������������������������������� 25 v ■ Contents

■■Chapter 3: Space Rocks ���������������������������������������������������������������� 27 Introduction ������������������������������������������������������������������������������������������� 27 Spaceship Movement ���������������������������������������������������������������������������� 29 Lasers and Rocks ���������������������������������������������������������������������������������� 32 Thrusters and Explosions ���������������������������������������������������������������������� 34 Teleportation ����������������������������������������������������������������������������������������� 38 Winning or Losing the Game ����������������������������������������������������������������� 40 Side Quests ������������������������������������������������������������������������������������������� 41 Shields ��������������������������������������������������������������������������������������������������������������������42 UFOs �����������������������������������������������������������������������������������������������������������������������43 On Your Own �����������������������������������������������������������������������������������������������������������46 Summary ����������������������������������������������������������������������������������������������� 47 ■■Chapter 4: Cleanup Challenge ������������������������������������������������������ 49 Backgrounds ����������������������������������������������������������������������������������������� 50 Animating the Player ����������������������������������������������������������������������������� 52 Cars ������������������������������������������������������������������������������������������������������� 54 Displaying Messages with Text Objects ������������������������������������������������ 57 Keeping Score with Global Variables ����������������������������������������������������� 60 Side Quests ������������������������������������������������������������������������������������������� 62 Randomization ��������������������������������������������������������������������������������������������������������62 On Your Own �����������������������������������������������������������������������������������������������������������63 Summary ����������������������������������������������������������������������������������������������� 64 ■■Chapter 5: Adding Polish to Your Game ���������������������������������������� 65 Adding Animation and Text �������������������������������������������������������������������� 66 Mouse Input and Buttons ���������������������������������������������������������������������� 68 Audio ����������������������������������������������������������������������������������������������������� 69

vi ■ Contents

Menus ��������������������������������������������������������������������������������������������������� 72 Alternative Controls ������������������������������������������������������������������������������� 73 Changing Default Controls ��������������������������������������������������������������������������������������74 Gamepad Controllers ����������������������������������������������������������������������������������������������74 Touchscreen Input ��������������������������������������������������������������������������������������������������76 Summary ����������������������������������������������������������������������������������������������� 77 ■■Chapter 6: Plane Dodger ��������������������������������������������������������������� 79 Background Effects ������������������������������������������������������������������������������� 80 The Player’s Plane ��������������������������������������������������������������������������������� 82 Stars and Score ������������������������������������������������������������������������������������� 83 Enemy Planes ���������������������������������������������������������������������������������������� 85 Side Quests ������������������������������������������������������������������������������������������� 87 Summary ����������������������������������������������������������������������������������������������� 88 ■■Chapter 7: Racecar 500 ���������������������������������������������������������������� 89 Introduction ������������������������������������������������������������������������������������������� 89 Tilemaps and Level Design ������������������������������������������������������������������� 90 Car Mechanics ��������������������������������������������������������������������������������������� 94 Race Timer �������������������������������������������������������������������������������������������� 95 Side Quests ������������������������������������������������������������������������������������������� 98 Scenery ������������������������������������������������������������������������������������������������������������������98 Obstacles ����������������������������������������������������������������������������������������������������������������99 On Your Own ���������������������������������������������������������������������������������������������������������100 Summary ��������������������������������������������������������������������������������������������� 101 ■■Chapter 8: Rectangle Destroyer �������������������������������������������������� 103 Introduction ����������������������������������������������������������������������������������������� 103 Paddle, Walls, Bricks, and Balls ����������������������������������������������������������� 104 Game Start and End ���������������������������������������������������������������������������� 106

vii ■ Contents

Items ��������������������������������������������������������������������������������������������������� 108 Items Affecting the Ball ����������������������������������������������������������������������������������������109 Items Affecting the Paddle �����������������������������������������������������������������������������������111 Side Quests ����������������������������������������������������������������������������������������� 113 Summary ��������������������������������������������������������������������������������������������� 114 ■■Chapter 9: Spell Shooter ������������������������������������������������������������� 115 Introduction ����������������������������������������������������������������������������������������� 115 Player Setup and Mouselook ��������������������������������������������������������������� 117 Creatures and Vortices ������������������������������������������������������������������������ 118 Instance Variables and Waypoint Logic ����������������������������������������������� 119 Spell Shooting ������������������������������������������������������������������������������������� 123 Spell Charge and User Interface ���������������������������������������������������������� 124 Score and Game Over �������������������������������������������������������������������������� 126 Side Quests ����������������������������������������������������������������������������������������� 127 Adding a Radar �����������������������������������������������������������������������������������������������������127 On Your Own ���������������������������������������������������������������������������������������������������������128 Summary ��������������������������������������������������������������������������������������������� 129 ■■Chapter 10: Airplane Assault ������������������������������������������������������ 131 Player, Waypoint, and Enemy Setup ���������������������������������������������������� 132 Shooting and Spawning Enemies �������������������������������������������������������� 134 Score, Health, Invincibility, and Game Over ����������������������������������������� 136 Side Quests ����������������������������������������������������������������������������������������� 138 Endless Vertical Scrolling �������������������������������������������������������������������������������������138 On Your Own ���������������������������������������������������������������������������������������������������������140 Summary ��������������������������������������������������������������������������������������������� 140

viii ■ Contents

■■Chapter 11: Tower Defenders ����������������������������������������������������� 141 Level Setup ������������������������������������������������������������������������������������������ 142 Enemy Movement �������������������������������������������������������������������������������� 144 Cannons and Bullets ���������������������������������������������������������������������������� 144 Earning Cash ��������������������������������������������������������������������������������������� 147 Cannon Purchase and Placement �������������������������������������������������������� 148 Game Ending and Difficulty Ramp ������������������������������������������������������� 152 Side Quests ����������������������������������������������������������������������������������������� 154 Additional Enemy Types ����������������������������������������������������������������������������������������154 Additional Cannon Types ���������������������������������������������������������������������������������������155 Time Speed Control ����������������������������������������������������������������������������������������������155 On Your Own ���������������������������������������������������������������������������������������������������������156 Summary ��������������������������������������������������������������������������������������������� 156 ■■Chapter 12: Maze Runman ��������������������������������������������������������� 159 Player Setup and Grid-Based Movement �������������������������������������������� 161 Enemies and Intelligent Movement ����������������������������������������������������� 166 Collecting Coins ����������������������������������������������������������������������������������������������������170 Game End �������������������������������������������������������������������������������������������������������������172 Side Quests ����������������������������������������������������������������������������������������� 173 Adding a Jewel Bonus Item ����������������������������������������������������������������������������������173 On Your Own ���������������������������������������������������������������������������������������������������������174 Summary ��������������������������������������������������������������������������������������������� 174 ■■Chapter 13: Jumping Jack ��������������������������������������������������������� 175 Level Design ���������������������������������������������������������������������������������������� 176 Player Setup ���������������������������������������������������������������������������������������� 177 Ladders and Climbing ������������������������������������������������������������������������� 180

ix ■ Contents

Additional Game Objects ��������������������������������������������������������������������� 185 Goal Flag ���������������������������������������������������������������������������������������������������������������185 Jump-Through Platforms ��������������������������������������������������������������������������������������186 Springboards ��������������������������������������������������������������������������������������������������������186 Breakable Bricks ��������������������������������������������������������������������������������������������������187 Coins ���������������������������������������������������������������������������������������������������������������������189 Keys and Locked Blocks ���������������������������������������������������������������������������������������190 Enemies ����������������������������������������������������������������������������������������������� 191 Side Quests ����������������������������������������������������������������������������������������� 195 Summary ��������������������������������������������������������������������������������������������� 196 ■■Chapter 14: Treasure Quest �������������������������������������������������������� 197 Level Design ���������������������������������������������������������������������������������������� 198 Hero Setup ������������������������������������������������������������������������������������������ 201 Sword-Fighting Mechanics �����������������������������������������������������������������������������������203 Multiple Levels ������������������������������������������������������������������������������������ 205 User Interface Design �������������������������������������������������������������������������� 208 Status Display �������������������������������������������������������������������������������������������������������209 Sign Mechanics ����������������������������������������������������������������������������������������������������209 Items ��������������������������������������������������������������������������������������������������� 211 Hearts �������������������������������������������������������������������������������������������������������������������212 Coins ���������������������������������������������������������������������������������������������������������������������212 Bombs ������������������������������������������������������������������������������������������������������������������214 The Treasure Chest �����������������������������������������������������������������������������������������������216 Enemies ����������������������������������������������������������������������������������������������� 217 Side Quests ����������������������������������������������������������������������������������������� 220 Summary ��������������������������������������������������������������������������������������������� 221

x ■ Contents

■■Chapter 15: The Journey Continues ������������������������������������������� 223 Continue Developing ��������������������������������������������������������������������������� 223 Working on Projects ���������������������������������������������������������������������������������������������223 Obtaining Art Resources ���������������������������������������������������������������������������������������224 Participating in Game Jams ����������������������������������������������������������������������������������225 Overcoming Difficulties ����������������������������������������������������������������������������������������225 Broadening Your Horizons ������������������������������������������������������������������� 226 Playing Different Games ���������������������������������������������������������������������������������������226 Increasing Your Skill Set ���������������������������������������������������������������������������������������227 Recommended Reading ���������������������������������������������������������������������������������������227 Sharing Your Games ���������������������������������������������������������������������������� 228 ■■Appendix A: Game Design Documentation ��������������������������������� 231

Index ���������������������������������������������������������������������������������������������� 237

xi About the Authors

Lee Stemkoski is a professor of computer science and mathematics. He earned his Ph.D. in mathematics from Dartmouth College in 2006. He has been teaching at the college level since, with an emphasis on Java programming, computer graphics, and video game development for the past six years. Lee particularly enjoys playing classic games released for the and Super Nintendo Entertainment System consoles. He has written another book, Beginning Game Development with LibGDX, in addition to many other scholarly articles and game development tutorials.

Evan Leider is currently pursuing a B.S. in computer and management information systems with a specialization in game development, with an expected graduation date of 2018. Since 2012, he has been using Construct 2 to create his own games. Evan has been a teaching assistant in introductory courses for two years, where he helps college and high-school students learn how to use Construct 2. He enjoys playing video games such as Sonic the Hedgehog and Super Smash Brothers.

xiii About the Technical Reviewer

Julien Kyatric Fantoni is a game maker and Construct expert. Originally learning coding with Visual Basic, PHP, C & C++ with the intent to make games it was only when he found Construct that he was able to release his first completed games. Julien’s an early adopter of Construct and early believer in HTML5. He’s been active in Construct 2 community providing tutorials, support and help in the forums and other websites. He’s been working and releasing games made during game jams and professional advert-games ordered by various media companies like CanalSat, France 2 or even the music video game “Lazers from my heart” for the band Birdy Nam Nam. You can find his various works on his website kyatric.com.

xv Acknowledgments

Thanks to the editorial and support staff at Apress, for without their efforts and support this book you are reading would not exist. Thanks to Ashley and Thomas Gullen, the creators of the Construct game engine and the founders of Scirra Ltd., whose dedication to bringing game development to the masses has had a profound impact and continues to inspire and empower individuals across the world. We would particularly like to thank Julien Fantoni, who is an outstanding technical reviewer and made many insightful comments and helpful suggestions on the presentation, style, and content of this book. Julien has been helping countless numbers of aspiring game developers for years (including the authors of this book!) as a moderator on the Scirra online forums. We are honored that he agreed to join us in this endeavor, and we have benefitted greatly from his experience and advice. Finally, a special thanks to our students and readers, past and present, for their continuous and infectious enthusiasm. Your passion for game development is what inspired us to write this book.

xvii Foreword

At Scirra, we’ve always believed the future of software is on the Web. Everyone wants to “put a ding in the universe” (as Steve Jobs said), and using HTML5 to power Construct 2’s games is our way of trying to do that. While Construct 2 lets you export to a variety of platforms, I’ve always thought the Web was the most interesting one: it’s free, it’s open, and there are no gatekeepers who will charge you fees or decline your app. Web technology is also excellent now, having come on in leaps and bounds since we started in 2011. New technologies like WebGL 2 and WebAssembly point to a bright future too. Another goal of mine is to encourage people to be content producers, not just consumers. It’s easy to spend hours clicking or swiping through the Web, just reading and using what other people have made. That can be interesting, but personally I think it’s far more exciting to things. Building software and games has often had a high barrier to entry, and we want to try to make developing and sharing your own games as easy as making a presentation. I hope this book helps get you started on the way. Construct 2 is designed to let your creativity run free. We specifically wanted to avoid being a “cookie-cutter” game engine, which basically gives you premade templates that you lightly modify. Instead, it provides a series of building blocks to start assembling your own unique game from. This book covers a range of game genres and mechanics to help you learn how to build something of your own, all built from the common building blocks of things such as sprites, behaviors, events, and more. If you manage to build something and share it with someone else, then we’ll be very happy to have helped you be a creator. Persistence can get you a long way too; I started with nothing but a laptop in my bedroom, and now we’re a full-fledged business. Who knows where you’ll end up if you keep going? —Ashley Gullen Founder, Scirra Ltd. February 2017

xix Introduction

Welcome to Game Development with Construct 2! In this book, you’ll learn how to create video games using the Construct 2 game engine, an ideal program for aspiring game developers who have no prior experience, as well as experienced game developers looking for a tool to rapidly create prototypes of games. The games you will create in this book are inspired by classic arcade games such as Asteroids, Frogger, Breakout, and PacMan; general genres such as car racing or tower defense games; and console games such as Super Mario Bros. and The Legend of Zelda. Construct 2 is both user-friendly and powerful. The software has been around for more than 5 years, has been downloaded more than 3.5 million times, and has an active user community and responsive development team. Games created with Construct 2 can be exported to run on a variety of platforms and operating systems, such as web browsers (HTML5), Windows, macOS, , Android, and iOS. A free version of Construct 2 is available for download and is sufficient for all the game projects contained in this book. Much like the software itself, this book does not assume you have any prior programming or game development experience. Over the course of the book, you will be guided in creating a series of 12 different video games of increasing complexity that will teach you both the features of the Construct 2 game engine and the game development topics and logical programming concepts that will serve you well for software development in general. Thank you for allowing us to be your guides as you begin your journey as a game developer. We hope that you find this book both informative and enjoyable and that it enables and inspires you to create your own video games to share with the world.

xxi