9,99 €
CODERS ARE ROCK STARS Coders are the people who are building the future. You can stake your own claim on the future by learning pro coding techniques. Take a look inside to figure out how and why coders think a bit differently, the basics of building a working application with a professional coding language, and how to test your app to make sure it works. Get a jump on your future as a rock-star coder today! * See the big picture - get a grip on how pro coders start and finish a project * Know the code - get your hands on a pro coding language and put it to work * Make things happen - create a working application you can share with friends
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 145
Veröffentlichungsjahr: 2017
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)
Cover
Introduction
About This Book
Foolish Assumptions
Icons Used in This Book
Where to Go from Here
Chapter 1: Getting Started with Java and Processing
Getting Started with Processing
Save and Reopen Processing Programs
Chapter 2: Writing Your First Program
Getting to Know the Canvas
Calling Methods in Java
Creating Variables
Chapter 3: Drawing with Code
Drawing a Triangle
Drawing a Rectangle
Drawing a Quadrilateral
Drawing an Ellipse
Drawing an Arc
Creating a Unique Drawing
Chapter 4: Adding Color to the Canvas
Adding Color to Your Canvas
Learning about RGB Values
Adding Color to Shapes
Chapter 5: Coding Your First Animation
Learning about Methods
Writing Your First Two Methods
Writing Your First Two Original Methods
Adding Parameters to Your Method
Chapter 6: Allowing User Interaction
Moving with the Mouse
Interacting with Mouse Clicks
Making a Horizontal Slider
Working with Conditional Statements
Moving Shapes with the Keyboard
Chapter 7: Coding Your First Game
Creating a Scene
Adding a Character to Your Game
Adding Movement to Your Videogame
Updating the Background
Adding Gravity to Your Videogame
Making Lava Pits
Chapter 8: Making Artificial Intelligence
Adding an Enemy to Your Videogame
Making the Enemy Move
Creating Your Own Java Class
Creating a Second Java Class
Adding Health to Your Character
Giving Your Enemies Collision Detection
Chapter 9: Planning Your Ninja Bird Game
Making Sense of the Flappy Bird Code
Outlining Your Ninja Bird
Designing Your Java Classes
Chapter 10: Planning Your Ninja Bird Methods
Designing Your Setup Method
Designing Your Draw Method
Designing Your Collision Methods
Adding Helper Methods
Chapter 11: Coding the Ninja Bird Basics
Converting Comments to Code
Chapter 12: Coding the Final Ninja Bird Code
Adding Randomness to Your Pipes
Detecting Collision
Keeping Score in Your Game
Cleaning Up the Code
About the Author
End User License Agreement
Cover
Table of Contents
Begin Reading
i
ii
vi
vii
viii
ix
x
xi
xii
1
2
3
4
5
6
7
8
9
10
11
12
14
13
15
16
17
18
19
20
21
22
23
24
26
25
27
28
29
30
31
32
33
34
35
36
37
38
40
39
41
42
43
44
45
46
47
52
51
53
54
55
56
57
58
59
60
61
62
63
64
65
66
68
67
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
86
85
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
112
111
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
136
135
137
138
139
140
141
142
143
144
145
148
147
149
150
151
152
153
154
155
156
157
158
159
160
161
162
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
192
191
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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:
Typing on a computer and using a mouse:
You may know how to use a Windows system or a Mac; either one will do. This book shows examples on a Mac.
Following instructions:
Coding is very precise. There are a lot of pieces to the puzzle when you’re making an application. It’s very important that you already know how to follow instructions carefully, and compare pictures in the book to what you’re making on your own.
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.
Before you download or install any software on your computer, ask permission from an adult first.
Follow these steps to download and install Processing:
Go to
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.
When your file starts to download, the Processing web page will show an option to become a Processing member (see Figure 1-3). You do not have to become a Processing member in order to be able to use Processing or complete the projects in this book.
After the file downloads, unzip the file by double-clicking it.
The Processing application should be created.
FIGURE 1-1: The Processing website.
FIGURE 1-2: Options for downloading Processing.
FIGURE 1-3: Optional contribution to the Processing application development.
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:
Double-click the Processing file that you extracted.
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).
FIGURE 1-4: A warning you might see when opening Processing the first time.
FIGURE 1-5: A welcome popup you see when opening Processing.
FIGURE 1-6: The Processing window with a new Sketch opened.
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:
Go to
www.processing.org/examples/star.html
(see
Figure 1-7
).
Scroll down to the code that creates these stars, shown in
Figure 1-8
.
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.
Click the Run button in the upper left of the Processing window (see
Figure 1-11
).
Watch the three different stars spin (see
Figure 1-12
)!
FIGURE 1-7: The Star program example on the Processing website.
FIGURE 1-8: The Star program code that you’ll copy.
FIGURE 1-9: Copy the Star program code by highlighting and right-clicking.
FIGURE 1-10: Paste the Star program code by right-clicking in the Sketch.
FIGURE 1-11: Test the Star program by clicking the Run button.
FIGURE 1-12: The canvas with stars spinning.
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.
Before I wrap up this chapter, you need to know how to save your work and then access it again after you’ve closed the Processing application. Follow these steps:
Save your code by choosing File ⇒Save (see
Figure 1-13
).
Rename your file “StarProgram” and click Save (see
Figure 1-14
).
Close the Processing application by choosing Processing⇒Quit Processing (see
Figure 1-15
).
If you want to open this file again, just open the Processing application and choose File ⇒Open (see Figure 1-16).
Processing shows the programs that were saved to the Processing folder, which is where you saved your program in Step 2.
Choose the StarProgram folder and click Open (see
Figure 1-17
).
