Godot Engine Game Development Projects
Total Page:16
File Type:pdf, Size:1020Kb
Godot Engine Game Development Projects Build five cross-platform 2D and 3D games with Godot 3.0 Chris Bradfield BIRMINGHAM - MUMBAI Godot Engine Game Development Projects Copyright © 2018 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Acquisition Editor: Nigel Fernandes Content Development Editor: Jason Pereira Technical Editor: Rutuja Vaze Copy Editor: Safis Editing Project Coordinator: Sheejal Shah Proofreader: Safis Editing Indexer: Mariammal Chettiyar Graphics: Disha Haria Production Coordinator: Shraddha Falebhai First published: June 2018 Production reference: 1270618 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78883-150-5 www.packtpub.com To my son, Damian, and my daughter, Nadia, for being a constant source of love and pride. To Priya for being a wonderful wife and mother. mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content PacktPub.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Foreword In December 2014, Godot Engine 1.0 was released. With this first public release, the feature- packed, free, and open source project delivered on its promise of a cross-platform, easy-to- use, and powerful game creation tool. I fell in love with Godot's high-level features and its permissive license, so I jumped right in, with no prior game development experience. I felt a bit lost when I first opened the editor. I moved on to the online documentation and started reading the well-written, step- by-step introduction. It covered basic concepts, such as scenes and nodes and the GDScript programming language, and it showed how to create a Pong clone, and that was basically it, at that time. There were some more tutorials on advanced features, but there was a gap between them and the first guide. As a complete beginner, I was stuck with the following question: how do I make my first game? Yet, I kept exploring, and with the help of the Godot community, I could eventually build a simple 2D game with a local multiplayer. What a feeling to see your first game on the screen! Fast-forward to 3 years from then, Godot has grown a lot as a project, a community, and an engine. Godot 3.0 was released in January 2018 after 18 months of work. It brought this free, community-driven project to the level of its proprietary counterparts for 2D and 3D game development. The beginner game developer that I was became a project manager, helping to organize and focus the work of hundreds of contributors revolving around the engine. Working together with people from all around the world improved all areas of the project: engine features, usability, localization, and, of course, documentation. Thanks to many dedicated contributors, Godot's documentation became one of the most accessible technical resources that I have seen in free and open source projects. This is in great part thanks to Chris Bradfield, the author of this book, who spent countless hours writing new material and reviewing contributions to ensure their accuracy and quality of writing. With this book, Chris goes one step further and provides a thorough answer to that question (how do I make my first game?), shared by many Godot beginners. Indeed, he goes beyond that, as Godot Engine Game Development Projects will lead you through the creation of five full-featured games—four in 2D and one in 3D. Each game introduces new features and concepts of Godot so that you can learn how and when to use them in your own creations. Advanced users with experience in other game development tools (or even Godot itself) will learn how to use Godot's unique architecture and features to write better, more maintainable code through Chris' showcase of best practices. From setting up Godot for the first time on your computer to publishing your own games to a wide range of platforms, Chris runs you through all the steps in a concise yet thorough way. After the first lesson, you should already be able to see how straightforward and accessible game development can be with a tool such as Godot, even if you had no prior experience in game creation. You will also feel a great sense of accomplishment seeing your own creation playing on your computer or smartphone, with the power to implement new gameplay elements with just a few lines of code. I wish you a lot of fun on your game development journey, and a warm welcome to the Godot community. Rémi Verschelde Godot Engine – Project Manager Contributors About the author Chris Bradfield has been working in the internet technology space for more than 25 years. He has worked in the online gaming space for a number of successful MMOs and social gaming publishers in South Korea and the United States. Throughout his game industry career, he has served as a game designer, developer, product manager, and business development executive. In 2012, he discovered his love for teaching and founded KidsCanCode to provide programming instruction and curriculum to middle and high school students. He is also a member of the Godot Engine documentation team and works to provide learning resources for game development students around the world. I would like to expresses my gratitude to Sokoban Pack by Kenney Vleugels, Spaceship art founder skorpio, Sunny Land art by Luis Zuno (@ansimuz), and 3D Minigolf tiles by YeOldeDM for their work. About the reviewer Max Hilbrunner is a Godot Engine contributor and CTO at aiPhilos and works on providing better product search experiences using artificial intelligence, natural language processing, and machine learning in cooperation with the German Research Center for Artificial Intelligence (DFKI). Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Chapter 1: Introduction 5 General advice 5 What is a game engine? 7 What is Godot? 8 Downloading Godot 9 Alternate installation methods 10 Overview of the Godot UI 10 Project Manager 10 Choosing filenames 12 Editor window 12 About nodes and scenes 16 Scripting in Godot 17 About GDScript 18 Summary 19 Chapter 2: Coin Dash 20 Project setup 21 Vectors and 2D coordinate systems 23 Vectors 24 Pixel rendering 24 Part 1 – Player scene 26 Creating the scene 26 Sprite animation 27 Collision shape 29 Scripting the Player 30 Moving the Player 32 About delta 33 Choosing animations 34 Starting and Ending the Player's Movement 34 Preparing for collisions 35 Part 2 – Coin scene 37 Node setup 38 Using groups 38 Script 39 Part 3 – Main scene 39 Node setup 39 Main script 41 Initializing 42 Table of Contents Starting a new game 42 Checking for remaining coins 43 Part 4 – User Interface 44 Node setup 44 Anchors and margins 44 Message label 45 Score and time display 47 Containers 47 Updating UI via GDScript 47 Using buttons 48 Game over 49 Adding the HUD to Main 49 Part 5 – Finishing up 51 Visual effects 51 What is a tween? 51 Sound 53 Powerups 53 Coin animation 55 Obstacles 56 Summary 57 Chapter 3: Escape the Maze 58 Project setup 59 Project organization 62 Inheritance 63 Character scene 64 Animations 66 Collision detection 68 Character script 69 Player scene 71 Player script 73 Enemy scene 75 Optional – turn-based movement 77 Creating the level 77 Items 78 TileSets 80 Creating a TileSet 80 TileMaps 84 Level script 86 Adding more levels 89 Game flow 89 Start and end screens 90 Globals 91 Global script 91 Score 93 Saving the High Score 95 Reading and writing files 95 [ ii ] Table of Contents