Table Of ContentGame Programming
for Artists
Game Programming
for Artists
by
Jarryd Huntley
and
Hanna Brady
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2018 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Printed on acid-free paper
International Standard Book Number-13: 978-1-1381-0613-0 (Hardback)
International Standard Book Number-13: 978-1-1386-2646-1 (Paperback)
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts
have been made to publish reliable data and information, but the author and publisher cannot assume
responsibility for the validity of all materials or the consequences of their use. The authors and publishers
have attempted to trace the copyright holders of all material reproduced in this publication and apologize to
copyright holders if permission to publish in this form has not been obtained. If any copyright material has
not been acknowledged please write and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmit-
ted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented,
including photocopying, microfilming, and recording, or in any information storage or retrieval system,
without written permission from the publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.
com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood
Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and
registration for a variety of users. For organizations that have been granted a photocopy license by the CCC,
a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used
only for identification and explanation without intent to infringe.
Library of Congress Cataloging‑in‑Publication Data
Names: Huntley, Jarryd, author. | Brady, Hanna, 1987- author.
Title: Game programming for artists / Jarryd Huntley and Hanna Brady.
Description: Boca Raton : CRC Press, [2018] | Includes bibliographical
references.
Identifiers: LCCN 2017016518 | ISBN 9781138106130 (hardback : acid-free paper)
| ISBN 9781138626461 (pbk. : acid-free paper)
Subjects: LCSH: Computer games--Programming. | Computer games--Design.
Classification: LCC QA76.76.C672 .H86 2018 | DDC 794.8/1526--dc23
LC record available at https://lccn.loc.gov/2017016518
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
and the CRC Press Web site at
http://www.crcpress.com
Contents
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
Foreword, xi
© 2018 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
Introduction, xiii
No claim to original U.S. Government works
Printed on acid-free paper
Chapter 1 ◾ Introduction to Programming 1
International Standard Book Number-13: 978-1-1381-0613-0 (Hardback)
A PROGRAM BY ANY OTHER NAME 1
International Standard Book Number-13: 978-1-1386-2646-1 (Paperback)
COMPUTERS ARE DUMB… 3
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts
have been made to publish reliable data and information, but the author and publisher cannot assume
…AND PROGRAMMERS ARE LAZY 3
responsibility for the validity of all materials or the consequences of their use. The authors and publishers
have attempted to trace the copyright holders of all material reproduced in this publication and apologize to
DON’T FEAR THE MATH 4
copyright holders if permission to publish in this form has not been obtained. If any copyright material has
not been acknowledged please write and let us know so we may rectify in any future reprint. PROGRAMMING LANGUAGES 5
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmit-
Interpreted Languages 5
ted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented,
including photocopying, microfilming, and recording, or in any information storage or retrieval system, Compiled Languages 6
without written permission from the publishers.
Syntax 7
For permission to photocopy or use material electronically from this work, please access www.copyright.
com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood C# and C++ 8
Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and
registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, JavaScript 8
a separate system of payment has been arranged.
COMMON PROGRAMMING CONSTRUCTS
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used
(OR BUILDING BLOCKS) 9
only for identification and explanation without intent to infringe.
Formatting 9
Library of Congress Cataloging‑in‑Publication Data
Variables 12
Names: Huntley, Jarryd, author. | Brady, Hanna, 1987- author.
Title: Game programming for artists / Jarryd Huntley and Hanna Brady. Data Types 13
Description: Boca Raton : CRC Press, [2018] | Includes bibliographical
references. Numeric 13
Identifiers: LCCN 2017016518 | ISBN 9781138106130 (hardback : acid-free paper)
| ISBN 9781138626461 (pbk. : acid-free paper) String and Character 13
Subjects: LCSH: Computer games--Programming. | Computer games--Design.
Boolean 13
Classification: LCC QA76.76.C672 .H86 2018 | DDC 794.8/1526--dc23
LC record available at https://lccn.loc.gov/2017016518
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
v
and the CRC Press Web site at
http://www.crcpress.com
vi ◾ Contents
Operators 14
Addition (+) 14
Subtraction (−) 14
Multiplication (*) 15
Division (/) 15
Modulus (%) 15
Assignment (=) 15
Comparators 16
Increment and Decrement (++ and --) 17
Conditionals 17
Logical Operators 19
And (&&) 20
Or (||) 21
Not (!) 21
Combos 21
Loops 22
while loops 23
do while loops 24
for loops 24
THOSE ARE THE BASICS 26
Chapter 2 ◾ Introduction to Game Engines 27
GAME ENGINES 28
A Brief History… 28
Under the Hood 28
GAME LOOPS 30
The Basics: Update and Render Phases 30
Delta Time 31
The Details: Running Speed 32
The Details: Engine Code 33
Contents ◾ vii
The Details: Update Phase 34
The Details: Render Phase 37
On Graphics Hardware 37
Objects in 3D Games 39
Shaders 39
Rendering Pipeline 42
Optimized Images 46
PLACING OBJECTS IN A GAME 46
PHYSICS 48
DIFFERENT GAME ENGINES 51
Picking a Game Engine 52
Features 52
Familiarity 54
WRITING YOUR OWN GAME ENGINE 55
However 56
Chapter 3 ◾ The Recipes 57
ABOUT THE CODE 57
GAME DESIGN NOTES 57
Scope 58
Prototyping 58
THE TIP OF THE ICEBERG 59
HOW TO USE THE RECIPE CHAPTERS 59
LIST OF RECIPES 60
Building Blocks 60
Introduction to Object-Oriented Programming 60
Introduction to C# 60
Game Recipes 60
Static, Singletons, and Game Managers 60
viii ◾ Contents
Endless Runner 60
Artificial Intelligence (AI) and Non-Player Characters (NPCs) 60
Physics-Based Character Controller 60
Chapter 4 ◾ Introduction to Object-Oriented
Programming 61
OBJECTS 62
CLASSES 64
METHODS 65
CONSTRUCTORS 68
INSTANTIATING OBJECTS 69
ACCESS MODIFIERS 70
Public 71
Private 71
Protected 71
MANIPULATING OUR OBJECTS AND THE DOT
OPERATOR (.) 72
PROPERTIES 73
THAT WAS A LOT 74
Chapter 5 ◾ Introduction to C# 75
ABOUT C# AND UNITY 75
Formatting in C# 76
Unity 76
Unity Flavored C# 76
On Learning the Dark Arts of Unity 76
Getting Started in Unity 77
Methods in Unity: Start() and Update() 77
Integrated Development Environments (IDEs) 78
C# FEATURES 79
Data Types 79
Value versus Reference Types 80
Arrays 80
Contents ◾ ix
Switch 83
Lists 84
THAT’S IT! 87
Chapter 6 ◾ Static, Singletons, and Game Managers 89
STATIC 89
SINGLETONS AND GAME MANAGERS 91
IN THE CODE 91
PROJECT SETUP 92
Chapter 7 ◾ Endless Runner 97
INHERITANCE, PARENT CLASSES, AND CHILD CLASSES 97
Overriding 98
A Few Final Details on Inheritance 99
CHARACTER CONTROLLERS 99
Object Pooling 100
Base 100
IN THE CODE 100
PROJECT SETUP 100
Chapter 8 ◾ Artificial Intelligence (AI) and Non-Player
Characters (NPCs) 119
PROJECT SETUP 120
Chapter 9 ◾ Physics-Based Character Controller 133
PROJECT SETUP 133
APPENDIX, 141
ACKNOWLEDGMENTS, 145
A (MOSTLY SERIOUS) BASIC GAME JARGON GLOSSARY, 147
INDEX, 151