WRITE CODE LIKE A PRO – CREATE WORKING APPLICATIONS
Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com
Copyright © 2018 by John Wiley & Sons, Inc., Hoboken, New Jersey
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions
.
Trademarks: Wiley, For Dummies, Dummies.com, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com
. For more information about Wiley products, visit www.wiley.com
.
Library of Congress Control Number: 2017959361
ISBN: 978-1-119-40417-0 (pbk); ISBN 978-1-119-40423-1 (ebk); ISBN 978-1-119-40426-2 (ebk)
So you want to make your very own videogame? And you want to learn how to code in Java? That is an awesome idea!
Writing Java code can be really hard work, especially writing Java code to make a videogame. For example, the original Legend of Zelda took more than 100 people three years to make! But don’t worry, this book will guide your through all the steps to learn the basics of Java programming and to make two videogames of your own. The first game you’ll make is kind of like Mario, where you have a character that runs across the screen and tries to avoid lava pits and enemies. The second, larger game that you’ll code is kind of like Flappy Bird. It’s called Ninja Bird. Keeping track of scores, making pipes move on their own randomly, and even helping the bird fly — you’ll have your friends playing games you make for hours!
This book uses a computer application called Processing that was made to make coding Java video games much easier. As you’ll see, you can easily make drawings with different shapes and colors, you can add images that you find online (with adult permission, of course!) into your drawing and games, and it comes with an entire library of code that you get to just use. It’s free, and available online. In the first chapter, you’ll learn how to download it and begin using it in just a couple of simple steps.
Write Code Like a Pro has three different types of programs that you’ll write. First, you’ll make a simple drawing using a lot of what Processing provides to you in its library. With shapes and colors, the first couple chapters of this book guide you through an art program. The next part of this book teaches you how to turn that drawing into an animation — making shapes and images move around the canvas. Finally, you’ll get a chance to build two different videogames.
Though this book will guide you step-by-step in making these games, you’re highly encouraged to keep coding after you finish this book and make variations of the games you learn here! This is just the beginning of your coding and videogame development career. With this book as a starting point, you never know what you can achieve!
Everybody has to start somewhere, right? I had to start writing this book by assuming that you’re comfortable doing this stuff:
As you read through the projects in this book, you’ll see icons. The icons point out different things:
The Remember icon highlights ideas that you should keep in mind.
The Tip icon marks advice and shortcuts that can make coding easier.
In this book, it’s best if you start from Chapter 1 and move through to Chapter 12. There are some things you learn in early chapters that really help you complete the larger problems in later chapters. The chapters in this book do fall under three categories: Chapters 1 through 6 guide you through the basics of coding in Java with a drawing canvas; you’ll get to learn how to make shapes, add color, make objects move, and even have user interaction — all very important for coding a videogame. Chapters 7 through 8 guide you through making your very first videogame! This game is similar to Mario, but just a preview to videogame making. Finally, Chapters 9 through 12 guide you through making Ninja Bird, which is a game like Flappy Bird.
So, you want to learn how to code in Java. That’s a great goal! In this book, you’ll learn the basics of Java while learning how to make a videogame! Coding can be tricky. Sometimes it’s frustrating. Remember to keep trying, test your code, and double-check that you’ve typed everything correctly. With dedication, you’ll learn a lot and have fun doing it!
In this chapter, you’ll install the Processing application, which will allow you to write Java code. Then you’ll get a chance to test out some real Java code and make sure you understand how the application will work throughout the rest of the book.
To get started with Java, you’ll be using a programming environment called Processing. Processing comes with a library of code that you can use to make objects move around the screen and create a fun gaming experience for your players! That makes writing your first videogame faster and easier.
To get started with Processing, you need to download and install the application on your computer.
Follow these steps to download and install Processing:
www.processing.org
(shown in Figure 1-1) and click the Download Processing button.Find the operating system that you’re using in the list of downloads (see Figure 1-2), and choose the correct version.
If you aren’t sure which operating system you’re using, ask an adult.
A file begins downloading to your computer.
After the file downloads, unzip the file by double-clicking it.
The Processing application should be created.
After you have unzipped the Processing application, you can move it to your Desktop if you want by dragging the file from the Downloads folder to the Desktop folder.
Now that you have Processing installed on your computer, you can start using it! Follow these steps:
The first time you open Processing, you might get an alert that asks if you’re sure you want to open the file (see Figure 1-4). Click Open.
You see a welcome screen (see Figure 1-5).
Uncheck the Show This Welcome Message Each Time box and then click the Get Started button.
A new Sketch opens. A Sketch is what Processing calls a new program file. You’ll notice that the Sketch has Java as the programming language set in the upper right of the screen (see Figure 1-6).
Now that you have Processing open, you can test out some code to make sure everything is working!
In this book, you’re going to be learning how to write your own code from scratch, but when you first set up a programming environment, it’s a good idea to test some code that you know should work to make sure your programming environment is working. Follow these steps:
www.processing.org/examples/star.html
(see Figure 1-7).Highlight all the code. Then right-click the code and choose Copy from the pop-up menu that appears (see Figure 1-9).
You can also highlight the code and then press Command+C if you’re on a Mac or Ctrl+C if you’re on a Windows computer.
Click the Processing application again, right-click in the Sketch window, and choose Paste from the pop-up menu that appears (see Figure 1-10).
You can also click in the Sketch window and press Command+V if you’re on a Mac or Ctrl+V if you’re on a Windows computer.
Congratulations! You’ve tested your first Java program! Don’t worry if the code doesn’t make sense yet. Throughout this book, you’ll learn a lot of Java and have a chance to understand how everything works.